+ 1
I cannot understand pointers. Why we need some values like 0×29fee8 (?)? And why we use *things to point some things to variable
2 odpowiedzi
+ 5
https://www.sololearn.com/Discuss/199279/?ref=app
All computers have memory, also known as RAM (random access memory). For example, your computer might have 16 or 32 or 64 megabytes of RAM installed right now. RAM holds the programs that your computer is currently running along with the data they are currently manipulating (their variables and data structures). Memory can be thought of simply as an array of bytes. In this array, every memory location has its own address -- the address of the first byte is 0, followed by 1, 2, 3, and so on. Memory addresses act just like the indexes of a normal array. The computer can access any address in memory at any time (hence the name "random access memory"). It can also group bytes together as it needs to to form larger variables, arrays, and structures. For example, a floating point variable consumes 4 contiguous bytes in memory.
#include <stdio.h>
int main()
{
int i; int *p;
/* a pointer to an integer */
p = &i;
*p=5;
printf("%d %d\n", i, *p);
return 0;
}
+ 3
0x29fee8 is just a different way (hexadecimal) to write the number 2752232 (decimal). And a pointer is just that, a number to a memory adress.
If you have 16GB of RAM on your computer you have 16 000 000 000 different memory addresses, so the numbers get a bit long.
*ptr looks up the value at address ptr, it's a bit like looking up someone's name in a phone book if all you have is their phone number.