By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

Pointer In C

P: 35
Hi all,
What will the following piece of code do:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&q[i];
i++;
}
i am bit confused with the 5 line of the code.
May 30 '07 #1
Share this Question
Share on Google+
6 Replies


Savage
Expert 100+
P: 1,764
Hi all,
What will the following piece of code do:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&q[i];
i++;
}
i am bit confused with the 5 line of the code.

q = (int*)&q[i];

this tells to the compiler that q is a pointer to adress of *(q+i).

print it out and u will see,hexadecimal adresses all.

Savage
May 30 '07 #2

P: 35
q = (int*)&q[i];

this tells to the compiler that q is a pointer to adress of *(q+i).

print it out and u will see,hexadecimal adresses all.

Savage
so for example p = 1000 and *p = 200 then q = 1000 i.e address of 200.The code is as follows:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&p[i];
i++;
}
is p acting as pointer to array of ints.When i try to print q and p as:
printf("q = %u\np = %u\n", q, &p[i]);
i am getting following results:
q = 176
p = 180
q = 180
p = 184
q = 184
p = 188
q = 188
p = 192
q = 192
p = 196
q = 196
p = 200
May 30 '07 #3

P: 35
so for example p = 1000 and *p = 200 then q = 1000 i.e address of 200.The code is as follows:
int* p;
int* q;
int i = 0;
while(i < 20){
q = (int*)&p[i];
i++;
}
is p acting as pointer to array of ints.When i try to print q and p as:
printf("q = %u\np = %u\n", q, &p[i]);
i am getting following results:
q = 176
p = 180
q = 180
p = 184
q = 184
p = 188
q = 188
p = 192
q = 192
p = 196
q = 196
p = 200
ok ok i got, but i have declared p as pointer to int how come i can give it a index and make it as pointer to array of ints.When i try tp print p[i] i get segmentation fault.
May 30 '07 #4

100+
P: 147
ok ok i got, but i have declared p as pointer to int how come i can give it a index and make it as pointer to array of ints.When i try tp print p[i] i get segmentation fault.
You never declared an array, only two pointers to single integers.
The notation p[i] is the same as *p+i.
That makes sense if you realize that the elements of an array are adjacent to each other in memory.
However, because your pointer points to only a single integer, not an array of integers, trying to access the memory at p+i is a segmentation fault.

If p had been passed to a function as a pointer to the first element of a 20-element array, your code would work fine.
May 30 '07 #5

P: 35
You never declared an array, only two pointers to single integers.
The notation p[i] is the same as *p+i.
That makes sense if you realize that the elements of an array are adjacent to each other in memory.
However, because your pointer points to only a single integer, not an array of integers, trying to access the memory at p+i is a segmentation fault.

If p had been passed to a function as a pointer to the first element of a 20-element array, your code would work fine.
But i have heard that pointer to an int same as pointer to an arrray of ints!!!
May 31 '07 #6

Savage
Expert 100+
P: 1,764
But i have heard that pointer to an int same as pointer to an arrray of ints!!!
char a; // allocates space for a single char

char * b; // creates space for a pointer to a char ( or an array of char)

char c [SZ]; // similar to b, must have a constant integer substitued for SZ
// however, you also allocate space for the char, c is an address like b.

char * d [SZ]; // this is interesting... like c above, its a pointer... but with a twist

char ** e; // is the eqivalent of d above... and its called a pointer to a pointer to a char

Do u know understand?

Savage
May 31 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.