Allocate space for struct pointer in subfunction

前端 未结 4 1727
死守一世寂寞
死守一世寂寞 2020-12-22 03:40

How can I allocate memory for a struct pointer and assign value to it\'s member in a subfunction?

The following code will compile but not execute:

#i         


        
相关标签:
4条回答
  • 2020-12-22 03:52

    You are passing s by value. The value of s is unchanged in main after the call to allocate_and_initialize

    To fix this you must somehow ensure that the s in main points to the memory chunk allocated by the function. This can be done by passing the address of s to the function:

    // s is now pointer to a pointer to struct.
    void allocate_and_initialize(struct _struct **s)
    {
            *s = calloc(sizeof(struct _struct), 1); 
            (*s)->str = calloc(sizeof(char), 12);
            strcpy((*s)->str, "hello world");                                                                                                                                                                      
    }
    int main(void)
    {
            struct _struct *s = NULL;  // good practice to make it null ptr.
            allocate_and_initialize(&s); // pass address of s.
            printf("%s\n", s->str);
    
            return 0;
    }
    

    Alternatively you can return the address of the chunk allocated in the function back and assign it to s in main as suggested in other answer.

    0 讨论(0)
  • 2020-12-22 03:56

    you must change your code like that:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
    
        struct _struct {char *str;};
        void allocate_and_initialize(struct _struct **s)
        {
            *s = (_struct*)calloc(sizeof(struct _struct), 1);
            (*s)->str = (char*)calloc(sizeof(char), 12);
            strcpy((*s)->str, "hello world");
        }
        int main(void)
        {
            struct _struct *s;
            allocate_and_initialize(&s);
            printf("%s\n", s->str);
    
            return 0;
        }
    

    The reason is, that you change the adress of the pointer, but not the "content" of the pointer. So, if you code in c, you have to use a "double" pointer. If you code in c++ you can use a reference.

    0 讨论(0)
  • 2020-12-22 03:57

    You can create struct object then pass its address to subfunction, then assign values in subfunction by creating pointer. The exact code is,

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct _struct {char *str;};
    
    void allocate_and_initialize(struct _struct *s)
    {
        s -> str = malloc(12);
        strcpy(s->str, "hello world");
    }
    
    void main(void)
    {
        struct _struct _struct;
        allocate_and_initialize(&_struct);
        printf("%s\n", _struct.str);
    }
    
    0 讨论(0)
  • 2020-12-22 04:03

    In your example:

    void allocate_and_initialize(struct _struct *s)
    {
        s = calloc(sizeof(struct _struct), 1);
        s->str = calloc(sizeof(char), 12);
        strcpy(s->str, "hello world");
    }
    

    Assigning to s here doesn't change s in the caller. Why not return it instead?

    struct _struct *allocate_and_initialize(void) {
        struct _struct *s;
        s = calloc(sizeof *s, 1);
        s->str = calloc(1, 12); /* sizeof(char) is always 1 */
        strcpy(s->str, "hello world");
        return s;
    }
    

    and use it thus:

    struct _struct *s;
    s = allocate_and_initialize();
    /* use s... */
    free(s); /* don't forget to free the memory when you're done */
    
    0 讨论(0)
提交回复
热议问题