1

I tried to set values in 2D array and Print it with Function.

but I got trouble to send the Array to the Function.

#include <stdio.h>

void SetArr(int (*arr)[], int n);
void PrintfArr(int (*arr)[], int n);

int main()
{ 
    int arr[100][100];

    for(int i=0; i<100; i++)
        for(int j=0; i<100; j++)
            arr[i][j]=0;         //initiallize Array

     int size;

    printf("input Size : "); scanf("%d", &size);

    SetArr(arr, size);
    PrintfArr(arr, size);

    return 0;
}

void SetArr(int (*arr)[], int n)
{
   int i, j;

    for(i=0; i<n; i++)
   {
       for(j=0; j<n; j++)
            arr[i][j]=i+j;    // Error! "Subscript of Pointer to imcomplete~
   }
}

void PrintArr(int (*arr)[], int n)
{
   int i, j;

   for(i=0; i<n; i++)
   {
       for(j=0; j<n; j++)
           printf("%d", arr[i][i]);  // Error! "Subscript of Pointer to imcomplete~
   }
   printf("\n");
}

As you see, both of functions got same problem while using "arr[][]"

5
  • if not for anything else: the printf("%d", arr[i][i]); call in PrintArr should be modified to printf("%d", arr[i][j]);... Commented Dec 21, 2014 at 18:58
  • 1
    int (*arr)[] doesn't specify the size of the array pointed to... so there's no way for the compiler to know how big the rows are in your 2D array inside the functions. If you'd used int (*arr)[100], it should work... though then your row size is fixed at 100 ints. Commented Dec 21, 2014 at 18:59
  • @Dmitri Wow thanks... I feel really stupid haha. i remembered it Thanks! Commented Dec 21, 2014 at 19:01
  • With C99, you could also do void SetArr(int n, int (*arr)[n]) Commented Dec 21, 2014 at 19:07
  • @Eureuerung In my opinion you marked as the best a non-correct answer because relative to your code the array has no dimensio n * n. See my answer. For example try to print it in main after setting its elements. Commented Dec 21, 2014 at 19:15

2 Answers 2

1

In the declaration of the function, array must be mentioned with its size (length). This is what the compiler means when it says "incomplete type": it needs the size to understand how to access its elements.

In order to make size available to the compiler, it should go first in the function's declaration:

void SetArr(int n, int (*arr)[n])

Here arr is technically a "pointer to an array of n elements", but in C it's customary to treat a pointer as an array, so arr can be treated as "an array of arrays of n elements", which is the same as a 2-D array.

You might want to mention both dimensions of the array:

void SetArr(int n, int arr[n][n])

This is equivalent to the first declaration, but seems clearer.

Sign up to request clarification or add additional context in comments.

Comments

0

Declare the functions like

void SetArr( int ( *arr )[100], int n );
void PrintfArr( int ( *arr )[100], int n );

You may not declare the functions as it is shown in the answer that you marked as the best

Consider the following code where instead of 100 I am using 3 for the array dimensions. If you will print the array in function PrintArr and in main you will get different results!

#include <stdio.h>

void SetArr( size_t n, int (*arr)[n] )
void PrintArr( size_t n, int (*arr)[n] )


int main(void) 
{
    int arr[3][3];

    size_t n = 2;

    SetArr( n, arr );
    PrintArr( n, arr );


    printf( "\n" );

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

    return 0;
}

void SetArr( size_t n, int (*arr)[n] )
{
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) arr[i][j] = i * n + j;
    }
}

void PrintArr( size_t n, int (*arr)[n] )
{
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
        printf( "\n" );
    }
}

The program output is

 0 1
 2 3

 0 1
 3 1

As you see the first output does not coincide with the second output. You would get the correct result if the functions were declared as I pointed in the beginning of the post. For example

#include <stdio.h>

void SetArr( int (*arr)[3], size_t n );
void PrintArr( int (*arr)[3], size_t n );


int main(void) 
{
    int arr[3][3];

    size_t n = 2;

    SetArr( arr, n );
    PrintArr( arr, n );


    printf( "\n" );

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

    return 0;
}

void SetArr( int (*arr)[3], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) arr[i][j] = i * n + j;
    }
}

void PrintArr( int (*arr)[3], size_t n  )
{
    for ( size_t i = 0; i < n; i++ )
    {
        for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
        printf( "\n" );
    }
}

The program output is

 0 1
 2 3

 0 1
 2 3

As you can see in this case the both outputs coincide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.