how do pointers in c work here?
#include <stdio.h>
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf("\n\n");
for (i = 0; i < 6; i++)
{
printf("my_array[%d] = %d ",i,my_array[i]); /*<-- A */
printf("ptr + %d = %d\n",i, *(ptr + i)); /*<-- B */
printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i开发者_C百科, *(++ptr));
}
return 0;
}
ptr++ should print ptr value than increment in it while ++ptr do 1st increment in ptr than print the value of ptr.. but when i compile the code it give me same result and what is void pointer. what is the use of it.
The expression *ptr++
is parsed as *(ptr++)
, and is evaluated as follows:
- Retrieve the current value of
ptr
- Dereference this pointer value to obtain an integer value
- At some point before the next sequence point, add
1sizeof *ptr
toptr
.
It's roughly equivalent to writing
x = *ptr;
ptr = ptr + sizeof *ptr;
except that the side effect of updating ptr
may happen at any time before the next sequence point (never assume that side effects are applied in a particular order); IOW, the sequence could be
tmp = ptr;
ptr = ptr + sizeof *ptr;
x = *tmp;
In this case, the sequence point is at the end of the printf
function call.
The expression *(++ptr)
is parsed as written, and is evaluated as follows:
- Retrieve the current value of
ptr + sizeof *ptr
- Deference this pointer value to obtain an integer value
- At some point before the next sequence point, add
1sizeof *ptr
toptr
which is roughly equivalent to writing
tmp = ptr + sizeof *ptr;
x = *tmp;
ptr = ptr + sizeof *ptr;
where again, the side effect of updating the value of ptr
can happen at any point before the next sequence point; IOW, the sequence could be
tmp = ptr + sizeof *ptr;
ptr = ptr + sizeof *ptr;
x = *tmp;
Here's a hypothetical memory map at program startup (assume 32-bit addresses and 16-bit ints):
Item Address 0x00 0x01 0x02 0x03 ---- -------- ---- ---- ---- ---- my_array 0x08000000 0x00 0x01 0x00 0x17 0x08000004 0x00 0x11 0x00 0x04 0x08000008 0xff 0xfb 0x00 0x64 ptr 0x0800000C 0x00 0x00 0x00 0x00 i 0x10008000 0x?? 0x?? 0x?? 0x??
0x??
indicates a random byte value. Since ptr
is declared at file scope, it has static extent and is implicitly initialized to NULL. Since i
is declared auto
, it is not initialized and contains random garbage.
After executing the statement ptr = &my_array[0]
, the value of ptr
is 0x08000000
.
When you execute the line
printf("ptr + %d = %d", i, *ptr++);
the expression *ptr++
dereferences the memory at address 0x08000000
, which contains the value 1, so the output is
ptr + 0 = 1
The side effect of the ++
operator updates the value of ptr
to 0x08000002
.
When you execute the line
printf("ptr + %d = %d, i, *(++ptr));
the expression *(++ptr)
deferences the memory at 0x08000004
, not 0x08000002
, since the expression ++ptr
evaluates to ptr + sizeof *ptr
. So the output of this line is
ptr + 0 = 17
and the side effect of the ++
operator updates the value of ptr
to 0x08000004
.
So as you loop, your output will look like
my_array[0] = 1 /* ptr = 0x08000000 */
ptr + 0 = 1
ptr + 0 = 1 /* ptr = 0x08000002 after */
ptr + 0 = 17 /* ptr = 0x08000004 after */
my_array[1] = 23
ptr + 1 = 4
ptr + 1 = 4 /* ptr = 0x08000006 after */
ptr + 1 = -5 /* ptr = 0x08000008 after */
These lines:
printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr));
modify the pointer value. So at each iteration, you are adding 2 offsets beyond your current position i. By the time your for loop hits the 6th iteration, you will have tried to print memory locations from the end of the array to somewhere 6(iterations)*2(offsets)*4(int size)=48 bytes beyond.
After a few times through that loop, you're going to start referencing memory beyond the end of the array; except for that, your program looks like it makes sense.
You are probably only look at the last part of the output because, when I compile it it shows each value of the array and then 0s after you have gone through the entire part of the code.
And as for void pointers:
They are also called Generic Pointers because you can assign them to any data type.
First of like Ignacio pointed out, this is all C code. Not c++. C++ is a superset of C.
1) I want to point out that your for loop is not safe. Cause your are incrementing the pointer twice inside the loop in each iteration. So, you will end up outside the allocated array at the 4th iteration.
2) Any C questions please refer to the excellent c faq and c++ faq. here is a relevante answer to your question. http://c-faq.com/ptrs/unopprec2.html . Its got to do with operator precedence. *ptr++ is equivalent to *(ptr++).
The output of your first iteration should be 1 1 1 17
void pointer is generic pointer. you cannot do arithmetic on it. it is just used to pass around location, crudely put. Also, importantly you cannot do arithmetic on it. because, you dont know what its pointing to.
精彩评论