bug using pointers in gcc

Muhammad Waseem Waseem.Muhammad@eurecom.fr
Fri Sep 16 11:09:00 GMT 2005


Hello
            I am facing a problem in a very simple C code using 
pointers: The code is

#############################################################

1. #include <stdio.h>
2. int main(void)
3. {
4.      char *p1;
5.      char *p2;
6.
7.     char a = 65;
8.      p1 = &a;
9.         printf(" %d   %d    %d    %d\n",p1, *p1, *(p1+1), *(p1+2));
10.    p2 = p1;
11.           printf( "%d   %d\n",p1,p2);
12.
13.   *p2 = 98;
14.          printf("%d   %d\n",p1,p2);
15.
16.   *(p2+1) = 98;
17.         printf("%d    %d \n",p1,p2);
18.
19.   *(p2+2) = 98;
20.          printf("%d    %d \n",p1,p2);
21.
22.          printf("%d   %d   %d    %d     %d \n",p1, 
p2,*p1,*(p1+1),*(p1+2));
23.     return 0;
24. }
###############################################################

The program compiles well. But at run time the pointer p2 is changed 
while i assign a value to a location in line #16

The output is very strange like this:


 -1073747649   65    20    10
-1073747649   -1073747649
-1073747649   -1073747649
-1073747649    -1073747710
-1073747649    -1073747710
-1073747649   -1073747710   2    2     -23

 why is it so?? I have checked on Turbo C compiler using window, 
evrything is fine, both pointers remain same

Thanks
Waseem





More information about the Gcc-bugs mailing list