user943369
user943369

Reputation: 204

2d array in Struct - C -

Im trying to assign a array to my letter.charData but i get this error:

IntelliSense: expression must be a modifiable lvalue

Im trying to add my array arr to letter.charData

Thanks in advance!

struct _Letter{
    char character;
    int width;
    int charData[8][5];
};

typedef struct _Letter Letter;

Letter *allocLetter(void)
{
    Letter *letter;

    letter = (Letter*) malloc(1 * sizeof(Letter));

    letter->character = NULL;
    letter->width = NULL;

    /* charData? */

    return letter;
}

int main(void)
{ 
    Letter letter = *allocLetter();

    int arr[8][5] = 
    {
        0,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0,
        1,0,0,0,0
    };

    letter.character = '1';
    letter.charData = arr;

    return(0);
}

Upvotes: 1

Views: 1444

Answers (1)

Fred Foo
Fred Foo

Reputation: 363817

_Letter::charData is an array, not a pointer, so you can't just assign another array to it. Either copy arr's contents into it with memcpy, or change its type to a pointer:

typedef struct {
    char character;
    int width;
    int (*charData)[5];
} Letter;

Also,

  1. Identifier names shouldn't start with _ followed by a capital
  2. NULL should only be used for pointers; use '\0' for characters, plain 0 for integers
  3. You don't check the return value of malloc for null
  4. You're not freeing allocated memory.

Upvotes: 5

Related Questions