Hi
I don't understand why I'm getting wrong result of the following code:
Code:
#include <stdio.h>
int main(void) {
int myarray[5] = {10,20,30,40,50};
int *p = myarray;
printf("pointer points to third element from myarray: %d\n", p[2]);
printf("for convenience print the whole arryy:");
for (int i=0; i<5; i++) {
printf(" %d", myarray[i]);
}
printf("\n");
/* if we want to change value of a certain address position we do:*/
p = &myarray[0]; /* get the address of the first array element */
*p = 11; /* change its value */
p = &myarray[4]; /* get the address of the fifth array element */
*p = 55; /* change its value */
printf("After the changes:");
for (int i=0; i<5; i++) {
printf(" %d", p[i]);
}
printf("\n");
return 0;
}
my result:
pointer points to third element from myarray: 30
for convenience print the whole arryy: 10 20 30 40 50
After the changes: 55 32766 -59239168 -176176675 0
expected result:
pointer points to third element from myarray: 30
for convenience print the whole arryy: 10 20 30 40 50
After the changes: 11 20 30 40 55