+ 2

Please tell me i am very confused in pointer .

Int a [5] = {1,2,3,4,5} Then Please tell me what is difference between ? Int *p = a ; Int *p = &a ; And also tell me where which to use ? I am getting very very confused in pointer . Please anyone teach me pointer then it will be very good . Thank you 🙂🙏

19th Mar 2022, 2:48 PM
Abhay mishra
Abhay mishra - avatar
8 odpowiedzi
+ 3
Thank you William Owens Ipang Jayakrishna🇮🇳 . I learned that pointer is tough . I will have to practice more . I will try my best . And if anything not understand then you talented guys are here ☺️☺️
19th Mar 2022, 7:38 PM
Abhay mishra
Abhay mishra - avatar
+ 2
int *p = a; is just assigning a pointer to array 'a'. No need & because all array itself a pointer. Now p points to first index of array a. If 'a' is not an array, like a normal variable then you need to store address by second way Like int a = 5; int *p = &a; now p holds variable a address. *p and a returns same value. p and &a returns same value of 'a' variable address .. Hope it helps...
19th Mar 2022, 2:57 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 sir you always help me . I am very thankful . But this time i couldn't understand your answer . If you can please help me more .
19th Mar 2022, 3:10 PM
Abhay mishra
Abhay mishra - avatar
+ 1
Arrays decay to a pointer referring the address of the array's first element. For this purpose, we don't need to use address-of `&` operator for assigning address in case of array. On the other hand, if we want a pointer to a single value (e.g. a variable, an array's element, a struct member etc.) then we need to use address-of `&` operator to get the respective address. Don't worry too much, pointer is an important topic, but honestly it is also not too easy to understand, especially in the beginning. Just keep learning and practicing 👍
19th Mar 2022, 3:03 PM
Ipang
+ 1
check that *p and *a works same. instead of a[I], you can always write like *(a+I) which is pointer notation of accesing variable like index notation of array way a[I]. Array variable is a pointer type variable implicitly.. So pointer to pointer assigning no need &. p holds address while *p gives data or value. 'a' holds or points to first location or starting address of array a. a[I] gives value at index I. So here p and a both are pointers which holds addresses. So you can directly assign address in address of same types. ( &a returns address of by a[0] , but p=&a leads to incompatible types. p=a is enough ) But for a normal variable (not a array type) int n = 10; n returns value. &n returns address of variable n in which value 10 is stored. So here you need to pass address since pointer hold address. int *p ; p= &n; p holds n address which is same as &n value. *p and n returns 10 same value. *p means return value stored at location p holding. Hope this clears. If not, reply about specific lines.
19th Mar 2022, 3:31 PM
Jayakrishna 🇮🇳
+ 1
http://cslibrary.stanford.edu/102/PointersAndMemory.pdf #include <stdio.h> int main(void){ int a[5] = {1,2,3,4,5}; int *p = a; int *pa = &a; for(int i = 0; i < 5; i++){ printf("%d", a[i]); puts(""); printf("%ld", p + i); puts(""); printf("%ld", pa + i); puts("") } } Would output something similar to: 1 140727103612496 140727103612496 2 140727103612500 140727103612500 3 140727103612504 140727103612504 4 140727103612508 140727103612508 5 140727103612512 140727103612512 int a[5] is an array of five with five address locations each separated size of int bytes. *p is a pointer to a[0] *p = &a is also a pointer to a[0] Why do we need pointers? In simple terms an array is a strait line of data in memory like: block 1:data, block 2:data etc If we built something like a linked list the system may find memory at different locations: link list point 1: data.... some other data in memory ..... link list point 2: data... so on an so forth so to link them we would have to know where they are, here we use pointers (this is really only one small example) now think about char* and strings these are also just arrays but the * next to char points to the first character of the string.
19th Mar 2022, 6:13 PM
William Owens
William Owens - avatar
0
Abhay mishra You're very confused in pointers
20th Mar 2022, 6:46 AM
Yvette
Yvette - avatar