+ 4

Plz explain the output of this code.

int a = 40 int* ptr = &a; a++; cout<<*ptr; //indirectly I'm asking about the working of a pointer...(since it's not given in very detail in the lesson )...plz explain. //thnx for the answers.

14th Feb 2018, 9:38 AM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar
5 Respostas
+ 10
int a = 40 //assign 40 to a int* ptr = &a; //ptr is pointer which point at the address of 'a' and assign that value to the pointer ptr which is 40 a++; // this will increment the value of 'a' now a become 41 cout<<*ptr; // since ptr is pointing to address of 'a' so it will assign the current value present at address of 'a' which is 41 and print that value so the output is 41
14th Feb 2018, 10:00 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
if you wanna know about pointers well here is the explanation of it: pointers are container of an address of a variable, imagin that "a" variable you have which is also an integer the address of "a" goes to "ptr" so now ptr is pointing to the address of the "a" variable but we can't say the amount is 40 now, no that's not true the amount of ptr is the address of the "a" but if you print it out c is smart enough to not to give you the address instead of the value and mess up your result. now you ask why we use them? well here's the deal: sometimes you want to work with registers or memory well you can't just directly add the value to them so you have to use the pointer to point the address of your variable so that later on you wanted to use that piece of memory it actually contains the same amount of your variable. by the way, the result of your program is 41. I hope it's clear enough, I tried to explain as easy as I could. :)
14th Feb 2018, 10:13 AM
kanaan
kanaan - avatar
+ 2
You have a var that contains an adress of another var... int i= 10; int* p= &i; now what is 'p'? its a var and values of this var is an ADRESS.. Thats all? No no no... You adress can be DEREFERENCED (as opposed to reference an var, you obtain value from adress)... but as do this if 'p' contain an adress? well you can use the * operator.. cout << p; // adress here cout << *p; // dereferenciation here... value of pointed var is printed Now another example int ai[]= [1,2,3,4]; int* p= ai; whats?? why we not referenced 'ai' with & operator? because being 'ai' an array, his value will be an ADRESS... When declare an array, wil be allocated memory for array and the ADRESS OF FIRST ITEM is returned (eg. 'ia' will contain the adress of first item array) and because we assign to 'p' pointer, latter will contains same value (first item array adress)... In this sense pointer and array are like ( not completely but share most behaviours)
14th Feb 2018, 2:31 PM
KrOW
KrOW - avatar
+ 1
the value of int a=40 and the memory address of "a" is stored in *ptr. When *ptr is called/displayed it shows the value of the memory address it stored. so, Cout <<*ptr <<endl displays the value of a. a++ increased the value by 1 so it will display 41 Just think *ptr=a
14th Feb 2018, 10:00 AM
Akib
Akib - avatar
0
41
21st Oct 2018, 11:28 PM
mohamed magdy
mohamed magdy - avatar