2

Since this morning i try to find how to edit particular cells from a two dimensional array but i'm stuck by trying to make my code cleaner (Want to make functions and make them work together).

This is of course not my entire program but a piece of it (Simplified of course).

I got the following error On "char *TableauAfficher[][]" when i try to compile:

"error array type has incomplete element type"

#include <stdio.h>
#include <stdlib.h>

void AffTableau(int *i, int *j, char *TableauAfficher[][], int *LongLargMap) //ERROR
{
   for (i=0; i<LongLargMap; i++) {
    for(j=0; j<LongLargMap; j++) {
        printf("Tableau[%d][%d] = %c\n", i, j, TableauAfficher[i][j]);
    }
    printf("\n");
}

int main()
{
    int i, j;
    int LongLargMap = 5;

    char SaisieDirection;
    int SaisieNombre;

    char TableauAfficher[5][5];

    int *Pointeuri = &i;
    int *Pointeurj = &j;
    int *PointeurLongLargMap = &LongLargMap;
    char *PointeurTableauAfficher = &TableauAfficher;

    TableauAfficher [0][0] = 'V';
    TableauAfficher [0][1] = 'V';
    TableauAfficher [0][2] = 'A';
    TableauAfficher [0][3] = 'V';
    TableauAfficher [0][4] = 'V';

    TableauAfficher [1][0] = 'V';
    TableauAfficher [1][1] = 'V';
    TableauAfficher [1][2] = 'A';
    TableauAfficher [1][3] = 'V';
    TableauAfficher [1][4] = 'V';

    AffTableau(Pointeuri, Pointeurj, PointeurTableauAfficher, PointeurLongLargMap);
}
5
  • Your code seems to have a lot of problems with incompatible types. Compiler should throw warnings or errors. If it doesn't, check your compiler manual and raise the warning level to max. Commented Oct 31, 2014 at 9:25
  • 1
    Passing i and j to function doesn't make any sense. You could declare those locally in funcition. Commented Oct 31, 2014 at 9:28
  • Also, passing the pointer int *LongLargMap to function serves no purpose. Passing by value int LongLargMap, is faster and simpler. Commented Oct 31, 2014 at 9:29
  • You seem to assign values to first 2 rows of TableauAfficher, but in function you seem to try to read all rows. Reading uninitialized variables is undefined behaviour. Commented Oct 31, 2014 at 9:31
  • @Dinodzo Where are you hurry?! See at first my answer. Commented Oct 31, 2014 at 9:36

3 Answers 3

4

Type of expression

&TableauAfficher

is

char ( * )[5][5]

while you are trying to assign this expression to a pointer of type char *

char *PointeurTableauAfficher = &TableauAfficher;

And in the function the corresponding parameter is defined as

char *TableauAfficher[][]

The compiler needs to know the right most value of the size. Without this information the element type of array TableauAfficher is incomplete.

Whether you have to define the parameter as

char TableauAfficher[][5]

or as

char ( *TableauAfficher )[5][5]

depends on how you are going to use it in the function.

The other way is to use a Variable Length Array (if your compiler supports VLA) Here is a demonstrative program

#include <stdio.h>

void AffTableau( size_t n, char TableauAfficher[][n] )
{
    for ( size_t i = 0; i < n; i++ ) 
    {
        for ( size_t j = 0; j < n; j++ ) 
        {
            printf( "Tableau[%zu][%zu] = %c\n", i, j, TableauAfficher[i][j] );
        }
        printf("\n");
    }       
}

int main(void) 
{
    char TableauAfficher[5][5] = { { '\0' } };

    TableauAfficher [0][0] = 'V';
    TableauAfficher [0][1] = 'V';
    TableauAfficher [0][2] = 'A';
    TableauAfficher [0][3] = 'V';
    TableauAfficher [0][4] = 'V';

    TableauAfficher [1][0] = 'V';
    TableauAfficher [1][1] = 'V';
    TableauAfficher [1][2] = 'A';
    TableauAfficher [1][3] = 'V';
    TableauAfficher [1][4] = 'V';

    AffTableau( 5, TableauAfficher );

    return 0;
}

The output is

Tableau[0][0] = V
Tableau[0][1] = V
Tableau[0][2] = A
Tableau[0][3] = V
Tableau[0][4] = V

Tableau[1][0] = V
Tableau[1][1] = V
Tableau[1][2] = A
Tableau[1][3] = V
Tableau[1][4] = V

Tableau[2][0] = 
Tableau[2][1] = 
Tableau[2][2] = 
Tableau[2][3] = 
Tableau[2][4] = 

Tableau[3][0] = 
Tableau[3][1] = 
Tableau[3][2] = 
Tableau[3][3] = 
Tableau[3][4] = 

Tableau[4][0] = 
Tableau[4][1] = 
Tableau[4][2] = 
Tableau[4][3] = 
Tableau[4][4] = 

Also I do not see any sense to declare function parameters int *i and int *j

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

Comments

2

You have a lot of errors, let me point out a few, to help you start.

You cannot do this:

char *PointeurTableauAfficher = &TableauAfficher;

PointeurTableauAfficher is a pointer to a char and &TableauAfficher is a pointer to an char array of [][5] dimensions.

This would be correct

char (*PointeurTableauAfficher)[][5] = &TableauAfficher;

If you pass &TableauAfficher to a function it has to accept the correct type:

void AffTableau(int *i, int *j, char (*TableauAfficher)[][5], int *LongLargMap) {

Note that the 5 is hardcoded so only pass arrays with inner size of 5.

Since TableauAfficher is a pointer to an array not the actual array, you have to first dereference it to get to the array.

(*TableauAfficher)[1][1] = 'a' ;


There also a missing closing parenthesis in the for loop in that function.

Comments

2

You cannot pass multidimensional array to function, without specifying some of the dimensions.

2D arrays are stored in 1D memory. How can function know how to calculate offset to next row, if it doesn't know the length of the previous row? It can't. So you need to add the length of the row to parameter declaration.

Also, type of array element seems to be char, not char *.

Thus, function argument should be: char TableauAfficher[][5].

You are also calling the function incorrectly. Line:

char *PointeurTableauAfficher = &TableauAfficher;

is incorrect, and pointless. You can simply pass TableauAfficher directly to function.

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.