I think my pointer points wrongly into some random address that's forbidden, so I'm getting a seg-fault. I'll describe what I am trying to do here:
I made an array of 10 elements, and the array looks like this:
|0|1|2|3|4|5|6|7|8|9|
I made a function, which uses a pointer to alter these 10 elements. The function map2x will first use a pointer, and points it to the first element.
It is done by p = x; Or so I think. Since l[10] is an array, l would just be a pointer points to the first element. In another word, l == &l[0]; would be true.
Then I basically times the content of that address by 2, move the pointer to the next one, times that by 2, untill it reaches the end.
But no. The plan fails and I get segfault, but I'm not sure where the bug is T_T So help?
+ Show Spoiler [codehere] +
#include <stdio.h>
int l[10];
int *p;
void map2x(int haha[]);
main()
{
int i;
for (i=0;i<10;++i)
{
l[i] = i;
}
map2x(l);
for (i=0;i<10;++i)
{
printf("%d\n", l[i]);
}
}
void map2x(int *x)
{
int i;
p = x; //experiment by changing *p=*x or something;;
for (i=0; i<9; ++p)
{
*p = *p * 2;
}
}
so I tried to change line14 from map2x(l) to map2x(&l[0]) which should be equivalent, but that doesn't solve the problem TT