I need help understanding the proper usage of "pointer to array of n integers" initialization

Viewed 34

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?

1 Answers

For starters according to the C Standard the function main without parameters shall be decalred like

int main( void )

In the shown program you declared an uninitialized pointer

int (*x)[12], i;

that has an indeterminate value. So dereferencing the pointer

(*x)[i] = i;

results in undefined behavior.

Instead you could write for example

#include <stdio.h>

int main( void )
{
    enum { N = 12 };
    int a[N];

    int (*x)[N] = &a;

    for ( size_t i = 0; i < N; i++ )
    {
        (*x)[i] = i;
        printf( "%d\n", (*x)[i] );
    }
}

Though the program will look simpler if to write it like

#include <stdio.h>

int main( void )
{
    enum { N = 12 };
    int a[N];

    int *x = a;

    for ( size_t i = 0; i < N; i++ )
    {
        x[i] = i;
        printf( "%d\n", x[i] );
    }
}

In this declaration

int *x = a;

is implicitly converted to a pointer to its first element of the type int *.

In this declaration

int (*x)[N] = &a;

the initializing expression &a is already a pointer of the type int ( * )[N].

So in the expression

(*x)[i] = i;

the subexpression *x yields lvalue of the array a. So in fact the above expression is equivalent to

a[i] = i;

Pay attention to that in the both declarations

int *x = a;

and

int ( *x )[N] = &a;

the pointers x store the starting address of the memory extent occupied by the array a but have different types. Dereferencing the first pointer you will get the first element of the array a. Dereferencing the second pointer you will get the whole array itself.

Related