1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

In this context, I was reading: "osdever.net/tutorials/view/implementing-basic-paging". The section "Setting up the Page Table" declares an unsigned long *page_table, and then uses page_table[i] inside a for-loop. But page_table is not an array, so I am confused why is it used as such. Can you please explain?
Arrays essentially are pointers to sequential memory, with each element of the array being the size of the variable type of the pointer. In that example they created an unsigned long pointer (4 bytes) starting at a particular point in memory, and then looped 1024 times to basically cover 4096 bytes of memory. Each increment of the array was the next unsigned long in memory.
Sorry, but I am confused that how the unsigned long pointer variable is used as an array. You say "Each increment of the array was the next unsigned long in memory", but there was no array declared here. Thanks for the help.
Technically an Array is not a data type, an array is a pointer. It points to the first location in memory, and then allows the array syntax to tell it how to step through memory. The data type of the variable determines the step size. Example: char* str = new char[20]; allows for str[0], str[1], str[2], etc. str is a pointer to a variable of type char, but you can use array syntax. Arrays are a different way to represent it to be easier to use. You never specifically have to tell the compiler it is an array to use the syntax. Using it may point to invalid memory though!
But the example only declares the pointer and does not use the "new unsigned long[1024], so does the compiler allocate infinite size? Is it implicit?
|
1

You've told the compiler to interpret the bytes of the pointer pt as if it were an int, so you will get a seemingly random result representing wherever pt happens to be located in memory.

I think you want

pd[0] = pt[0] 

or

pd[0] = *pt;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.