InitZWeakness
InitZWeakness

Reputation: 17

Can't assign char value to pointer in struct

I can't assign the value into the pointer in struct as below .

. .

struct Matrix
{
    char *A[10];
    char *Psedo[10];
    int depth;
    int startrow;
    int startcolumn;
    int exitrow;
    int exitcolumn;
};
struct Matrix matrixs ; // create new global struct

void createPsedoEmptyMatrixForBackTracking()
{
    for (int i = 0; i < matrixs.depth; i++)
    {
        for (int x = 0; x < matrixs.depth; x++)
        {
            matrixs.Psedo[i][x] = '0';
        }
    }
    printf("DONE");
}

void startMove()
{
    createPsedoEmptyMatrixForBackTracking();
}

int main(){
   startMove();
}

.

This is not the full code , but my matrixs.depth right now is 5 and as my compiler result the printf("DONE") never show up and the value also not assign into the Psedo variable i have no idea why

Then i tried

strcpy(matrixs.Psedo[i][x], '0')

instead of

matrixs.Psedo[i][x] = '0'; .

.

And this shown up

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]

and also the printf("DONE); never show up

Upvotes: 0

Views: 341

Answers (1)

user2736738
user2736738

Reputation: 30936

You need to allocate memory to Psedo variable. It is an array of pointers. Where do they point to? They are not pointing to any valid memory location. Accessing it is undefined behavior.

If you don't need to have array of pointers you can simply have an array too. That will not demand an explicit allocation as in the present case. char *Psedo[10]; will be char Psedo[10][MAXLEN];

Secondly strcpy(matrixs.Psedo[i][x], '0') would be strcpy(matrixs.Psedo[i], "0"). Feeding strcpy what it wants ... it expects to pointers. In your earlier case the integral value of char is considered to be as pointer. That's why the warning. This will also lead to undefined behavior.

Upvotes: 1

Related Questions