Pointers and variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Pointers and variables

I have read and reread the lessons on pointers, both on Sololearn and elsewhere, but I'm just not grasping it well. I understand that the pointer points to the place of a stored variable, but I don't understand how it all works in the memory. If I declare an integer, that integer is stored at a certain address, but where is the name of the variable stored? In the code below, where is the j stored? int j = 63; int *p = NULL; p = &j; Is *p and p the same thing? Where is the pointer name stored? Remember the post where they asked how you would explain something to your granny? Think about that as you help me to understand this. Diagrams or links to good explanations would be much appreciated. Thanks in advance. Heather

15th Mar 2019, 9:11 PM
Heather
Heather - avatar
14 Answers
+ 10
Hey, I'll post the same thing I've posted under similar questions, I think it's a useful way to think about pointers. Imagine you have a big `data` array somewhere in your code where you store some important stuff. If you want to pass the fifth element around in your code you could do either `thing x = data[4];` and use `x`, or, if you so choose, you could simply carry around the index `int ptr_x = 4;`, and you would know to plug it back into the array if you ever need to (`x == data[ptr_x]`). Now imagine that all the memory in your computer is this single big array. `*ptr_x` is the act of looking up a value inside the array, at index `ptr_x`. `&x` is the act of getting the index from the variable. In other words, &x == ptr_x *ptr_x == x `&` and `*` are inverses of each other, like `+` and `-`. In other words, x == *(&x) ptr_x == &(*ptr_x) It's useful to think of variables not as names, but as, say, post boxes. Pointers are post boxes that contain the address of another post box.
16th Mar 2019, 5:25 AM
Schindlabua
Schindlabua - avatar
+ 8
The name of the variable isn't stored. Look at some assembly code to fully understand it, but what basically happens is this: the CPU has about 8/16 (on x86/x86_64) integer registers with which it works. One of those is the stack pointer, the place where variables are usually stored. "i" would be translated as (stack pointer - 4), i.e. (esp - 4), j as (esp - 8), and so on. If you want to "get" computers learn assembly. Don't learn to write, just learn to read it. It's the most enlightening thing. EDIT: p is not *p. A pointer is just an integer. In early versions of C you could even do wanky stuff like p = j or *j (whatever is stored at memory location 63), but now the compiler complains so you don't do it on accident.
15th Mar 2019, 9:33 PM
Vlad Serbu
Vlad Serbu - avatar
+ 6
Hzr I am not surpised that you have difficulty. Read on more basic concepts of computer architecture. A pointer is a MEMORY ADDRESS, so a pointer VARIABLE contains a memory address. It is a very important concept
16th Mar 2019, 9:53 AM
Da2
Da2 - avatar
+ 5
- The name of the variable doesn’t get stored on the computer at all. Memory gets identifed by it’s address not variable name by the computer, variable names are just an abstraction to make it easier for us to make and read code. - "*p and p are the same thing?" no and also what they mean depends on the context. When declaring a variable *p means create a pointer variable p e.g. int a = 6; int *p = &a; and just p would create a normal variable e.g. int p = 42; When actually using the pointer, just p will access the value of the pointer itself which is usually a memory address, in the example below it will print the address of b not 7 since the pointer is storing it’s address... int b = 7; int *p =&b; printf("%d\n", p); // memory address of b *p when using the pointer will give the actual value of the memory address it is pointing to since in this context * is the dereference operator which as I just said grabs the value in the memory address e.g. int b = 7; int *p = &b; printf("%d\n", *p); // value of b (7) If you got any other questions or didn’t understand something be sure to ask.
15th Mar 2019, 9:30 PM
TurtleShell
TurtleShell - avatar
+ 4
Hzr in that case they refer to the same variable. The only reason you need a * when declaring a pointer is to show the difference between a pointer and normal variable.
16th Mar 2019, 12:00 AM
TurtleShell
TurtleShell - avatar
+ 3
Just some Infos to add. As Vlad Serbu said, each variable is in reality an address. Local variables, in functions, are stored on the stack (using stack pointer esp). Global variables are stored in a specific section : section .bss. Constants are stored in the .data segment. Pointers, as you know, are only variables containing the address of an other variable. For the CPU, all are numbers. The variables have got names only for the programmers. If you decompile an executable file, you can easily see that there are no variable name.
15th Mar 2019, 9:53 PM
Théophile
Théophile - avatar
+ 3
Hzr Here's a simple function (not using pointers): int add() { int x, y; return x + y; //Yes, since we haven't initialized anything, this will be garbage, but that's besides the point. } Here's what the computer sees (not as text, but specially encoded): push ebx push ecx ;we'll be using these registers later so we need to preserve their current values sub esp, 8 ; i. e. esp -= 8, 8 bytes is the size of 2 integers, since the stack grows down, we're making room for 2 integers this way. mov ebx, [esp+4] mov ecx, [esp+8] ;get the integers. [] means we get what is at that address add ebx, ecx ;add ecx to ebx mov eax, ebx ;move the result to eax, which holds the return value add esp, 8 pop ecx pop ebx ;clean up the stack ret ;return x += *p is, with x in ebx and p in ecx: add ebx, [ecx] As you can see, names need not be stored.
16th Mar 2019, 8:09 AM
Vlad Serbu
Vlad Serbu - avatar
+ 2
TurtleShell "The name of the variable doesn't get stored... at all." Wow. I need to wrap my head around that. In the example that I posted from the lesson, are *p and p the same? If not, can you explain the difference to me? int j = 63; int *p = NULL; p = &j;
15th Mar 2019, 11:50 PM
Heather
Heather - avatar
+ 2
Think of pointer like this As specifier that point to a particular variable in a dataset or array
16th Mar 2019, 3:57 PM
George S Mulbah II
George S Mulbah II - avatar
+ 2
p is the variable name and *p is a pointer to p that means a pointer pointing at the address of p. &p is the address where value of variable p is stored. Plz correct me if i m wrong :)
16th Mar 2019, 5:04 PM
Suzan
Suzan - avatar
+ 1
Vlad Serbu Thanks for the suggestion to learn to read assembly. The variable name not being stored is really confusing me.
15th Mar 2019, 11:54 PM
Heather
Heather - avatar
+ 1
Heather the C compiler makes a variable table for storing variables and their memory address. These variables are destroyed and replaced by the original memory address during compilation. The assembly code now contains only the memory addresses of the previous variables. So, variables are used just because humans would otherwise find it difficult to insert a memory location without causing any problems. Whenever we deal with variables, the data the variables are pointing to is also carried with them. But special variables caller pointers carry only the address. So, pointers are faster and more efficient for handing arrays and all. Now consider a variable passed to a function. The data in the memory address which we've named 'var' (just for illustration) is stored in a separate memory address and manipulation happens on that address, no the original one (pass by value). Whereas, if you feed the function with a pointer, it gets the "right" (I'm being a bit informal here) to manipulate the original var.
28th Feb 2021, 1:48 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
This process is way faster than passing by value and then after returning from the function, modifying the original variable with the return value, which requires the processor to manipulate several bytes of data, instead of a pointer. Pointers have their own use in C. Since python isn't meant for speed and performance, it lacks pointers; you can get the memory allocation by id(var) but cannot change its contents. Hope this helps, and thank you for asking this wonderful question, which allowed me to search Google and find the answer.
28th Feb 2021, 1:52 AM
Calvin Thomas
Calvin Thomas - avatar
0
Pointer is an variable that points another pointer variable
27th Sep 2019, 2:41 PM
vaishnavi