Home > Back-end >  Understanding Pointer
Understanding Pointer

Time:06-16

I have this problem and I don't understand what this means. I ask my questions in the comments.

int x = 3; 
int *y = &x;     

int z = *y;
printf("%d", z); //why does this give me the value and not the adress? I thought this means z =*y -> *y =&x -> &x outputs the adress of x

int *u= (y 1);  //what does this mean? 
printf("%d", *u); //why does this give the output of 3? 

CodePudding user response:

Pointers store an address.
The asterisk *y de-references the pointer to get the value the pointer points to.

#include <stdio.h>

int main ( void) {
    int x = 3;
    int *y = NULL;
    y = &x; // store the address of x in the pointer
            // y now points to x

    printf ( "address of x:        %p\n", (void *)&x);
    printf ( "address y points to: %p\n", (void *)y);
    printf ( "address of y:        %p\n", (void *)&y);

    int z = 0;
    z = *y; //*y de-references the pointer to get the value at the address
            //stored in the pointer.

    printf ( "value of z:          %d\n", z);

    int *w = NULL;
    w = y; // store the address y points to in the pointer w
           // w and y now point to x

    printf ( "address w points to: %p\n", (void *)w);
    printf ( "address of w:        %p\n", (void *)&w);
    printf ( "value w points to:   %d\n", *w);

    return 0;
}

CodePudding user response:

y is a pointer that points to the integer variable x.

int x = 3; 
int *y = &x;     

So dereferencing the pointer y you can get a direct access to the the variable x pointed to by the pointer and extract its value.

Thus in this declaration

int z = *y;

the variable z is initialized by the value of the variable x by means of using the pointer y that points to x.

Correspondingly this statements

printf("%d", z);

outputs the value 3 by which the variable z was initialized.

In this declaration

int *u= (y 1);

in the initializing expression there is an expression with the pointer arithmetic. The expression y 1 has the type int * and points in the memory after the variable (object) x pointed to by the pointer y.

Dereferencing the pointer u that does not point to a valid object in this statement

printf("%d", *u);

invokes undefined behavior.

CodePudding user response:

int z = *y;
printf("%d", z);

Q: why does this give me the value and not the adress?
A: y points to the x variable. Therefore dereferencing the y pointer fives 3.*y is the value where y points to which is the value of x which is 3. The chapter dealing with pointers in your C text book explains it.

int *u= (y 1);

Q: what does this mean?
A: y points to x (as seen before). y 1 points to the int that is in memory right after x, but this is garbage, because there is nothing valid at that address, it's an invalid memory address.

printf("%d", *u);

Q: why does this give the output of 3?
A: as expained above u doesn't point to a valid address, therefore the value you read at that address is garbage, it could be anything. Dereferencing pointers that point to invalid memory is undefined behavior (google that term).

  • Related