0

Refering to https://stackoverflow.com/a/22250138/5783540 it says:

when you have

int a = 10;

and you do

&a

it turns out it is: int*

But why a pointer? I know &a will give you a memory address but it doesn't hold an address but a number 10 or am I wrong?

Please read: I know what a pointer is, I know what & actually does. I'm just confused with:

To get the address of a, you do: &a (address of a) which returns an int* (pointer to int)

Again, why should int a be a pointer?

Edit: Thanks guys. I was reading wrong. There is no hidden magic behind pointers. Just my bad reading...

Community
  • 1
  • 1
int80
  • 21
  • 6
  • You need to re-read the book, I guess. It can be a bit tricky when it comes to pointers. :) – Sourav Ghosh Jan 16 '16 at 21:34
  • Yeah, pointers are just aweful. Especially when it comes to double pointers... – int80 Jan 16 '16 at 21:39
  • @int80: If you don't understand pointers, use a language which does not have them. C was (and is) designed to allow shooting your foot - for good and for bad. That's because higher level languages exist for higher-abstraction problems. – too honest for this site Jan 16 '16 at 21:54
  • yeah i know. but I'm just having fun to go low level, without the claim to become a kernel developer. i think pointers seem to be easy, but there are not! i also think the least people have really understand them... whatever... i started the topic because of my bad reading and it sounded like i really dont know what pointers are... how embarrassing. :) – int80 Jan 16 '16 at 21:58
  • But the questions what a pointer is and what `&` does must be asked thousands of times?!? So I asked to delete this topic – int80 Jan 16 '16 at 22:12
  • ..._when you have int a = 10; and you do &a it turns out it is: int*_ ... Not until you assign the one to the other: `int a, *pA;` Once you _make the assignment_ `pA = &a;` the address of each are the same, not until. (Important distinction). – ryyker Jan 16 '16 at 22:18
  • @ryyker yea, that's the point, I was reading if you do int a = 10 it's a pointer, but it only become a *int when you assign it to a pointer... – int80 Jan 16 '16 at 22:22
  • 1
    yes. I am saying this. It was foolishness to think there is a deeper meaning down low... – int80 Jan 16 '16 at 22:36
  • Okay, just wanted to make sure. Pointers are a difficult concept... – ryyker Jan 16 '16 at 22:37
  • pointers are the crux in system programming, also... – int80 Jan 16 '16 at 22:38

6 Answers6

1

&a literally means address of a. It is the address in memory where the space containing the integer a starts. Like the address of your residence, you do not live in the address, but in the space pointed to by that address.

Say you printed the address of a:

printf("%p" &a);

and get the value of 18fec0

This value, (a memory location) would look like this:

|18fec0 - Starting point of a  (assuming sizeof int is 4 bytes:)|18fec4 - next memory location
|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0...
^                                                               ^

int *a; is special variable that contains a pointer to int. In 32 bit memory, the sizeof(a) will also be 4 bytes.

In 64 bit memory space, the sizes of int and int * are not the same:

`int a;`
`sizeof(a) == 4`

`int *a;`
`sizeof(a) == 8` 

One additional illustration. Consider:

int a, *pA;//create an int, and a pointer to int

main(void)
{
    pA = &a;//setting pointer to address of integer.
    ...     //now, if you were to print each, the value should be the same
    printf("%p\n", &a);
    printf("%p\n", pA);
    return 0;
}

On my system I get:

18fec0
18fec0

ryyker
  • 22,849
  • 3
  • 43
  • 87
1

A pointer is a variable that stores a address. for example when int a = 10; int *b = &a; here b is a pointer that stores an address ,the address of where 10 is stored. &a means address of something. For example 10 is stored in address 1234567, and this address is stored in b, why?because of &. pointer is a variable too and something stored in it(some address) so we can have another pointer that points to it(stores the address of that). so why int*:

1-because values are stored in memory and we should know how many bytes read from it and how to read it(int ,float,...).

2-and when you want to increment pointer it should know where to go (how many bytes to jump). It could not read from middle of an int value!. so it should jump from that value and go to next value.

Habib Kazemi
  • 2,172
  • 1
  • 24
  • 30
0

But why a pointer? I know &a will give you a memory address but it doesn't hold an address but a number 10 or am I wrong?

int *p = &a -> p will hold the address of the memory location containing 10, not the value 10 itself.

Say, a is at memory address 0x00123456, thus at that location (usually) 4 bytes will be reserved for a and will contain the integer value 10 (assuming 32-bit for simplicity).

Now if we take a pointer int *p = &a (int pointer p is address-of a), p will have the value 0x00123456, which points to the location where a is stored (i.e. the value 10).

int **p_ptr = &p; is essentially the same. It's a pointer to the pointer pointing to a :)

Sounds somewhat strange, but it is no different than the first example, except the type it is pointing to is different (another pointer in this case).

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • @int80 every variable has an address. a "pointer" is a variable which stores the address of another variable. – M.M Jan 16 '16 at 21:33
  • @int80 - a is the variable containing 10 and is stored at a certain location. p is a pointer to that location. I updated the post a bit. – Danny_ds Jan 16 '16 at 21:36
0

To get the address of p do:

int **pp = &p;

and you can go on:

int ***ppp = &pp;
int ****pppp = &ppp;
...

or, only in C++11, you can do:

auto pp = std::addressof(p);

To print the address in C, most compilers support %p, so you can simply do:

printf("addr: %p", pp);

otherwise you need to cast it (assuming a 32 bit platform)

printf("addr: 0x%u", (unsigned)pp);

In C++ you can do:

cout << "addr: " << pp;
Sakib Ahammed
  • 2,452
  • 2
  • 25
  • 29
0

Every variable you create is stored somewhere in memory. The place that it is stored is called it's address. At the address, the value of the variable is held. So if you wrote int a = 10 the address of a is going to be some number. The value of a is what you assigned, 10. Run this code to see what I mean.

#include <stdio.h>

int main() { int a = 10; printf("The address of a is: %d\n", &a); printf("The value of a is: %d\n", a); return 0; }

EDIT: int a should be a type int* because the number it's holding is an address. int* tells the compiler that the number stored there is an address in memory and not just some value you declared.

Hippity Hoopla
  • 68
  • 1
  • 10
0

The former answer is correct, but I think that doesn't help to solve your doubts.
On a typical system you have a memory done of an array of cells able to hold values. Each cell is accessed by CPU issuing on bus the address of the required cell.
When you declare a variable the compiler reserves an area in the memory chunk allocated to your process.
From now on the CPU can access to that variable through its address. From now on the only way to access the variable is using the address.
When you refer to a variable the compiler automatically use the address to address the correct memory location, then loads the value to a CPU register to operate on it.
But there are many cases where you need the address of a variable not its value.
I.e. if you want modify a variable defined in the caller function:

void bar(int *pointer)
{
    *pointer = 1;
}
void foo(void)
{
    int a = 0;
    bar(&a);
    printf("a modified in bar() to %d\n", a);
}

In IT a variable that holds the memory address of another variable of some type is called a pointer to that type.
Why pointer of pointer of pointer.... ?
Example:

void bar(int **pointer)
{
    **pointer = 1;
}
void foo(int **a)
{
    bar(a);
    printf("a modified in bar() to %d\n", *a);
}

Of course there are more useful use of this ;-)
The following is a more realistic example, We call a function that dynamically creates an array.

void bar(int **pointer, int nElems)
{
    //We got the address of a variable that is a pointer to int
    //note that we use a single dereference to access to the
    //variable that holds the pointer to int not to the pointed int.
    *pointer = malloc(sizeof(int) *nElems) ;
}
void foo(void)
{
    int *MyIntArray;    //Holds the address where our array starts
    bar(&MyIntArray, 10);   //Pass the address of our variable so
                            //bar() can update it with the allocated address
    int i;
    for(int i=0; i<10; i++)
        MyIntArray[i] = i+1;

    for(int i=0; i<10; i++)
        printf("MyIntArray[%d] = %d\n", i, MyIntArray[i]);

    free(MyIntArray);
}
Frankie_C
  • 4,764
  • 1
  • 13
  • 30
  • ok, my bad reading suggested that Im not aware of pointers and what they do. so now, it says if you want to change a pointer given as a function argument you have to use a double pointer. why not a simple pointer? can you explain me? I'm refering: http://stackoverflow.com/a/26004257/5783540 – int80 Jan 16 '16 at 22:18
  • See the updated answer. – Frankie_C Jan 16 '16 at 22:29
  • well, but you didn't answered my question. never mind. And `In IT the memory address of a variable is called a pointer.` is also wrong. a pointer is a variable which contain an address. The memory address itself is a variable. IMHO – int80 Jan 16 '16 at 22:35
  • Yes you're right :-). It should have been in IT the variable holding the memory address of another variable is called a pointer. And yes I haven't answered your question, I'll do now. If you want to change a variable of type pointer you must pass the address of that variable. In plain words the first `*` means that the variable is a pointer to the type, the following `*` means that it is the pointer address. In the same way usinge the variable name with one `*` make you access the address, the second `*` will access the type. – Frankie_C Jan 16 '16 at 22:42
  • hey, mhm i dont get it what you trying to say. what are you talking about? double pointers? – int80 Jan 16 '16 at 22:55
  • A pointer is a variable as any other variable. As you can have a pointer to an integer or float you can also have a pointer to a pointer. I.e. `int *p` means that att address that the compiler reserved for `p` is stored the address of an integer. Then `int **pp` means that `pp` is a variable that holds the address of a variable that holds the address of an `int` (you can see it as `int *(*pp)` ). You can have triple, quadruple,.... n-uple pointers: `int ******************************pointer` is perfectly legal. – Frankie_C Jan 16 '16 at 23:03