0
votes

This is a code to initialize a 2D array of chars in C The array is 3X3 of characters I'm using Eclipse C/C++ IDE for ubuntu and when using this function GCC outputs an error like this: subscripted value is neither array nor pointer nor vector what does tis error means ,can any one help me please , thanks in advance :)

void init(char* ptr)
{
    int i=0;
    int j=0;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            *ptr[i][j]=' ';
        }
    }
}
4

4 Answers

0
votes

Should be defined as char** ptr the argument of init().

0
votes

ou are passing just a single pointer in init. If it is 3x3, then you'll need to change your signature to:

 void init(char **ptr){ ... }

And when accessing your pointer, either:

ptr[i][j] = '';

or

(*(ptr[i])+j) = '';

or

(*(*(ptr+i))+j) = '';
0
votes

ptr is a pointer to char not char[][] . so you should use **ptr or char (*ptr)[size_of_2nd_dimension]

or simple:

void init(char (*ptr)[size2]) <-----
{
    int i=0;
    int j=0;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            ptr[i][j]=' ';
        }
    }
}
0
votes

The function can't guess the size of the array, but since it's c you can do the manipulation yourself (2 options):

void initA(char* ptr, int totalSize)
{
    int i=0;
    int j=0;
    for(i=0;i<totalSize;i++)
    {
        *ptr[i]=' ';
    }
}

void initB(char* ptr, int sizeX, int sizeY)
{
    int i=0;
    int j=0;
    for(i=0;i<sizeX;i++)
    {
        for(j=0;j<sizeY;j++)
        {
            *ptr[(i * sizeY) + j]=' ';
        }
    }
}