Search code examples
carraysstructinitializationstatic-array

how to initialize static array within struct


I have this struct:

typedef struct {
    int start;
    int end;
    char board[10][10]; 
} move;

when I try initializing it this way:

char new_board[10][10]
move struct new_move = {0, 0, new_board}

I get this error:

warning C4047: 'initializing' : 'char' differs in levels of indirection from 'char (*)[10]'

any suggestions?


Solution

  • If you want to initialize the array by zeroes then you can write simply

    move new_move = { 0 };
    

    If you want that the structure array would contain values of array char new_board[10][10] then you have to copy its element in the structure array.

    for example

    char new_board[10][10] = { /* some initializers */ };
    
    move new_move = { 0 };
    
    memcpy( new_move.board, new_board, sizeof( new_board ) );
    

    If board is an array of strings then you could also to copy using a loop each string in the structure array using standard C function strcpy.