I am trying to understand pointers and arrays. After struggling to find pointers online(pun!), I am stating my confusion here..
//my understanding of pointers
int d = 5; //variable d
int t = 6; //variable t
int* pd; //pointer pd of integer type
int* pt; //pointer pd of integer type
pd = &d; //assign address of d to pd
pt = &t; //assign address of d to pd
//*pd will print 5
//*pt will print 6
//My understanding of pointers and arrays
int x[] = {10,2,3,4,5,6};
int* px; //pointer of type int
px = x; //this is same as the below line
px = &x[0];
//*px[2] is the same as x[2]
So far I get it. Now when I do the following and when I print pd[0], it shows me something like -1078837816. What is happening here?
pd[0] = (int)pt;
Can anyone help?
int *pt; is a pointer to a variable of type integer, it is not an integer. By specifying (int)pt you are telling the compiler to typecast pt to be an integer.
To get the variable at pt you use *pt, this returns the integer variable pointed to by the address stored in pt.
It’s a bit confusing since you say pd[0], and pd is not an array.