Initializing a pointer to a structure

前端 未结 6 2120
半阙折子戏
半阙折子戏 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 00:47

    i think malloc(sizeof(struct thread_data)); should work, does it not?

    0 讨论(0)
  • 2020-12-18 00:48

    The corresponding struct initialiser can look like this:

    struct thread_data a = {
      .incall = {"a", "b", "c", "d", "e"},
      .arg_no = 5,
      .client_socket = 3
    };
    

    Then you can assign the address of this to a pointer:

    struct thread_data *b = &a;
    
    0 讨论(0)
  • 2020-12-18 00:55

    It depends if you need your variable to be temporary or not:

    struct thread_data data; // allocated on the stack
    // initialize your data.* field by field.
    
    struct thread_data* data = malloc(sizeof (struct thread_data)); // allocated on the heap
    // initialize your data->* field by field.
    

    In both cases, you have to allocate your structure first to be able to access its fields.

    0 讨论(0)
  • 2020-12-18 00:56

    Here's the answer to the question I think you're asking:

    /**
     * Allocate the struct.
     */
    struct thread_data *td = malloc(sizeof *td);
    
    /**
     * Compute the number of elements in td->incall (assuming you don't
     * want to just hardcode 10 in the following loop)
     */
    size_t elements = sizeof td->incall / sizeof td->incall[0];
    
    /**
     * Allocate each member of the incall array
     */
    for (i = 0; i < elements; i++)
    {
      td->incall[i] = malloc(HOWEVER_BIG_THIS_NEEDS_TO_BE);
    }
    

    Now you can assign strings to td->incall like so:

    strcpy(td->incall[0], "First string");
    strcpy(td->incall[1], "Second string");
    

    Ideally, you want to check the result of each malloc to make sure it was successful before moving on to the next thing.

    0 讨论(0)
  • 2020-12-18 00:58

    You can write something just like this:

    #define ARRAY_DIMENSION(a) (sizeof(a)/sizeof((a)[0]))
    
    void init_func(void)
    {
        struct thread_data arg_to_thread;
        int i;
        char buffer[100];
    
        buffer[0] = '\0';
    
        for ( i = 0; i < ARRAY_DIMENSION(arg_to_thread.incall); i ++ )
        {
             /* Do something to properly fill in 'buffer' */
    
             arg_to_thread.incall[i] = strdup(buffer);
        } 
    }
    
    0 讨论(0)
  • 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 );
    
    0 讨论(0)
提交回复
热议问题