This question already has answers here:
How to increment a pointer address and pointer's value?
(5 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
void test(int x, int *y) {
*y = 5;
}
int main() {
int *a ,b =2 ;
a = &b;
test(*a, a);
a--; // a++ also give different value
This is the part i very confuse, I know if i dont put the (a--) statement the output will be 5. but what is really behind the meaning of a-- / a++ cause sometimes it give me different value as I test it with different value. I found this accidentally .
cout<<"d"<<*a<<endl;
}
a contains memory pointer to the variable. So it is *a = 5, if you increment/decrement a, then it will point to some other address/location in the memory which has some garbage value. That's what you are getting.
Lets say
a -> [2000] //memory address which contains value 5
if you do a++/a-- then a will point to [2004]/[1996] location in the memory
which has some garbage value.
Related
This question already has answers here:
Are there benefits of passing by pointer over passing by reference in C++?
(7 answers)
Pass by reference vs pass by pointer? [duplicate]
(3 answers)
When pass-by-pointer is preferred to pass-by-reference in C++?
(13 answers)
Performance cost of passing by value vs. by reference or by pointer?
(6 answers)
How to pass objects to functions in C++?
(8 answers)
Closed 6 months ago.
I want to have a explanation with respect to variable created on stack memory, what is the difference between pointer and reference. I understand in the case of calling by pointer, for example, the following things are happening.
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main(){
int i1 = 3, i2 = 7;
swap (&i1,&i2);
return 0;
}
Two variables i1, i2 are created on stack framed to the main function.
at calling 'swap' function, two more variables x, y, are created on stack, framed to the 'swap function. These two variables are initialized at the address of i1 and i2.
a temp variable is created on stack framed to 'swap' function. All of x,y,temp disappear after 'swap'.
Could someone explain in a similar way what happens when I use 'call by reference'? In particular, is there a variable x and y created on stack when calling 'swap'? If so, what are they?
#include <iostream>
using namespace std;
void swap(int &x, int &y){
int temp;
temp = x;
x = y;
y = temp;
}
int main(){
int i1 = 3, i2 = 7;
swap (i1,i2);
return 0;
}
Thank you!
you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the address itself. Sometimes you will need to modify the address (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the address, reference won't
This question already has answers here:
How to increment a pointer address and pointer's value?
(5 answers)
Closed 2 years ago.
I was writing a code related to referencing in C/C++. I made a pointer and put it into a function that incremented it. In the function, I wrote *ptr++ and tried to increment the value of the integer the pointer was pointing at.
#include <iostream>
using namespace std;
void increment(int *ptr)
{
int &ref = *ptr;
*ptr++; // gets ignored?
ref++;
}
int main()
{
int a = 5;
increment(&a);
cout << a;
return 0;
}
Can anyone please tell me why I can't increment the variable? I have tried incrementing it using +=1, and it works but not by using ++ operator?
++ has higher precedence than *, so *ptr++ is treated as *(ptr++). This increments the pointer, not the number that it points to.
To increment the number, use (*ptr)++.
Your code is:
*ptr++;
Which is equivalent to:
*(ptr++);
Which means pointer is incremented first and then dereferenced. this happens because increment operator ++ has higher precedence than dereferance operator * . So you should use:
(*ptr)++;
Here first pointer is dereferenced then incremented.
This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to learn C++, specifically how to declare and initialize variables. I wrote this code, and I don't know why the variable c is giving a value that I have not assigned it yet.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
int a, b;
a = 1;
b = 2;
int d(4);
int result;
auto num = b;
decltype(b) c;
result = a + b - d;
cout << c;
}
The output is -2, but I didn't state c = -2 anywhere!
If you have not initialized the variable, it contains garbage value.
In C/C++, the values declared within a function represent some bytes of main memory on the cpu stack. Those bytes are usually dirty and need initialization. If you don't the values are undefined. That you're always getting '-2' is merely coincidence.
This question already has answers here:
Why are references not reseatable in C++
(17 answers)
Closed 7 years ago.
According to these links: stackoverflow question and C++ FQA references cannot refer to another object/ variable well once they're initialized, but what about the below code?
// Example program in C++
#include <iostream>
using namespace std;
int main()
{
int x=10, z=12;
int &y=x;
++y;
y=z; //valid?
cout << x << ", "<< y <<", " << z << endl; // prints 12, 12, 12
return 0;
}
Below is the C code regarding pointer reseating and it seems valid, am I right?
#include <stdio.h>
int main(int argc, char *argv[])
{
int a=10, b=20;
int *ptr;
ptr = &a;
ptr = &b;
printf("%d\n",a); // prints 10
printf("%d\n",*ptr); // prints 20
return 0;
}
Can someone clear the above concept in the above two codes?
y=z; //valid?
Absolutely! However, it does not mean "y refers to z from now on". It means "set the value of z to whatever y is currently referring", which is x. Hence, y=z is another way of writing x=z.
Below is the C code regarding pointer reseating and it seems valid, am I right?
Unlike references, pointers can be re-pointed, so the re-assignment of the pointer makes it point to a different variable. Your program does not illustrate this, however, because two assignments to ptr happen without any reads of ptr in between, so only the second assignment stays.
In y=z
Its not refering to another variable its just assigning the value of z to y which is 12 and since y is reference to x, x also gets assigned value 12.
so x=y=z=12
But in pointers its valid to change the address it points to:
ptr = &a;
ptr = &b; //valid
This question already has answers here:
What does *& mean in a function parameter
(5 answers)
Closed 10 years ago.
I found this in a final exam:
int a = 564;
int* pa = &a;
int *& pr = pa;
cout << *pr;
According to the multiple choice answer, the code is valid, and displays the value of a.
But I'm confused about evaluation and precedence for line 3. Order of operations for C states that * and & have the same order. So, would it then be int *(&pr)? How can this be described in words?
Thank you.
The third line defines a pointer reference (or a reference to a pointer, if you want). Assigning it to a pointer makes pr to actually be an alias to pa, and when evaluated, it points where pa points to, that is, a.
In the declaration of a variable, * and & don't have the meaning of operators, so precedence doesn't make sense here.
It's a reference to a pointer. In C, you would express that as a pointer to a pointer.
You could write something like this:
// C++ style
void update_my_ptr(int*& ptr) { ptr = new int[1024]; }
// C style
void update_my_ptr_c(int **ptr) { *ptr = malloc(1024 * sizeof(int)); }
int main()
{
int *ptr;
update_my_ptr(ptr);
// Here ptr is allocated!
}
Line three creates a reference (read: alias) of a pointer to an int. If you were to set pr to 0, pa would also be equal to 0 (and vice-versa).