Address and pointer

Let's say i have an int variable

int number = 5

Does the expression "&number" returns a type int*?
Meaning the address is a pointer to an int?
Since it can be dereferenced *(&number) to return value of number?
Yes, a pointer essentially is a memory address.

Though, on some platforms, pointers can be a bit more complicated than just a simple "linear" address:
https://en.wikipedia.org/wiki/Far_pointer

In C/C++, a pointer can be typed, so that the type of the pointer reflects what the pointer is pointing to. For example, an int* pointer is pointing to an int value (or to an int[] array).

There also are untyped void* pointers that have no information on what they are pointing to.

And yes, the &-operator ("address-of-operator") gives you the address of (i.e. a pointer to) its operand:
https://docs.microsoft.com/en-us/cpp/cpp/address-of-operator-amp?view=msvc-170

Also, the *-operator ("indirection operator") dereferences a pointer, i.e. it converts a pointer to an l-value:
https://docs.microsoft.com/en-us/cpp/cpp/indirection-operator-star?view=msvc-170

Example:
1
2
3
4
5
6
7
8
int number = 5;
int *ptr = &number;      // <-- create pointer to number
printf("%p\n", ptr);     // <-- print the pointer (i.e. the *address* of 'number')
printf("%d\n", *ptr);    // <-- print the int value that ptr is pointing to
number = 42;             // <-- change the number
printf("%d\n", *ptr);    // <-- print the int value that ptr is pointing to (again)
*ptr = 666;              // <-- change the int value via the pointer
printf("%d\n", number);  // <-- print the updated int value 

0xbf8ce99c
5
42
666
Last edited on
Thanks
Topic archived. No new replies allowed.