0

Can someone please simplify pointers in C?

I have been learning the C programming language. I have a very basic understanding but I am having some trouble fully understanding pointers. If someone can simplify them as much as possible that would be cool.

12th Aug 2019, 5:25 AM
Kevin
Kevin - avatar
6 Respuestas
+ 4
A pointer is a variable that holds the address of some variable. int a = 5; Now 'a' is an integer which has been allocated 4 bytes in the memory. Now to see at what address/location the variable lies in the memory we use pointers. Before moving on, remember that '&' operator is called address operator. When using address operator with variable (&a), we are referencing the variable. In simple words, we are talking about the address of variable. Similarly, '*' operator is called dereference operator. Dereferencing means to get the value at specific memory address. Also this operator is used to create pointer variables. int *ptr = &a; In above statement, we are assigning the address of 'a' to the pointer variable 'ptr'. Now 'ptr' holds the address of 'a'. To view the address of 'a' (value of 'ptr') printf("\x", ptr); OR printf("\x", &a); Both will have same result. Now to see the value at memory address pointed by 'ptr' we will dereference 'ptr'. printf("%d", *ptr); Hope it helps.
12th Aug 2019, 8:40 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
A pointer will contain the address of an object in memory. When the pointer is deterrenced, it will point to the value stored there.
12th Aug 2019, 5:56 AM
Paul Grasser
Paul Grasser - avatar
+ 1
So the pointer is just an address of where the object is in the memory?
12th Aug 2019, 6:00 AM
Kevin
Kevin - avatar
+ 1
Yes.
12th Aug 2019, 6:16 AM
Paul Grasser
Paul Grasser - avatar
+ 1
Okay thank you
12th Aug 2019, 6:16 AM
Kevin
Kevin - avatar
0
My pleasure. 😁
12th Aug 2019, 6:17 AM
Paul Grasser
Paul Grasser - avatar