Initializing a pointer to a structure

前端 未结 6 2222
半阙折子戏
半阙折子戏 2020-12-18 00:32

another linked question is Segmentation fault while using strcpy()?

I have a structure:

struct thread_data{    
    char *incall[10];
    int syscall         


        
6条回答
  •  情深已故
    2020-12-18 01:01

    Here is another possibility. It is unclear to me what you want the values initialized to, so this just grabs a number out of the air, which is almost certainly wrong.

    struct thread_data *td;
    int i;
    // allocate memory for the structure
    td = malloc( sizeof( struct thread_data ));
    // then allocate/initialize the char* pointers.  It isn't clear
    // what you want in them ... pointers to existing data?  Pre-allocated
    // buffer?  This just allocates a fixed size buffer,
    for ( i = 0; i < sizeof( td->incall ) / sizeof( td->incall[0] ); i++ )
       td->incall[i] = malloc( 42 );
    

提交回复
热议问题