Are `x = &v` and `*x = v` equivalent?

前端 未结 6 1030
无人共我
无人共我 2020-12-31 10:21
int * x;
int v = 7;

Given this code, what is the difference between 1. x = &v , and 2. *x = v ? I understand that

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 11:03

    int v=7;

    Let's say the address of v = 00ZXCDFCDGDD2345

    int x=&v;

    Here '&' means the address of and x is used to store the address *(00ZXCDFCDGDD2345)*, and x itself stored at some location say *00254DBHJBDHDW*.

    Now if we want to access the value of v we use the pointer.

    int *x=v;

    In this statement '*x' is a pointer variable which points to v, means it stores the value of v but the x itself is undefined, it stores the garbage value. Hence both are different.

提交回复
热议问题