The following declaration:
int (*x)[10]
is defined as x is an integer pointer to an integer array of 10 elements.
Question 1 : Doesn't this mean that x initially points to the first element of an array of 10 integers? If so, then, how is this different from the simple int x[10]
?
Question 2 : If int (*x)[10]
is different, then how is it different and what are some instances of its practical usage?
Question 3 : I was trying to write a simple program access, write to and print the array elements.
#include <stdio.h>
void main()
{
int (*x)[12], i;
for(i = 0; i <= 11;)
{
(*x)[i] = i;
printf("%d\n", (*x)[i++]);
}
}
I keep getting a segmentation fault when I run it. I understand that a segmentation fault occurs when I try to access memory that I don't have access to. But I am only accessing the 12 elements that I have initialized. Then why does my program exit with a segmentation fault? Also, am I accessing the array ((*x)[i] = i
) correctly and are there other ways to access it?