0

I want to be create a struct, but I also want to write its array or string elements with dynamic memory allocation.

struct st {
    char *name[40];
    int age;
};

For "name" string should I use malloc before struct, or can I use it in struct also.

1)

char *name = malloc(sizeof(char)*40);

struct st {
    char *name;
    int age;
};

2)

struct st {
    char *name = malloc(sizeof(char)*40);
    int age;
};

Are both of them true or is there any mistake? And if both of them are true, which one is more useful for other parts of code?

5
  • There is mistake, you can't initialize struct members in the definition. Commented May 31, 2016 at 13:02
  • 1
    The first structure you show has an array of 40 pointers to char, i.e. an array of 40 strings. The second structure (alternative 1) will just have single pointer, who is totally unrelated to the name variable you define before the structure. The third structure (alternative 2) will not compile. Commented May 31, 2016 at 13:03
  • @JoachimPileborg so can not we use dynamic allocation for struct? Commented May 31, 2016 at 13:07
  • Do you want to allocate the structure dynamically, or memory for the name member? Also, allocating memory for the name member dynamically with a compile-time constant fixed size make no sense, just use an array. Commented May 31, 2016 at 13:12
  • @JoachimPileborg I want to allocate the "name" member dynamically, static arrays forbidden for my homework, and I must use dynamic memory allocation for "name" Commented May 31, 2016 at 13:14

3 Answers 3

2

You need to create an instance of the structure, an actual variable of it. Then you need to initialize the members of the structure instance in a function.

For example, in some function you could do

struct st instance;
instance.name = malloc(...);  // Or use strdup if you have a string ready
instance.age = ...;
Sign up to request clarification or add additional context in comments.

1 Comment

Should instance be an pointer? can we write like "instance.name = malloc(..);" etc.
1

An option would be to have a pointer in the struct however allocate memory outside of the struct within the function you use it.

e.g.

struct st {
    char* n;
    int a;
};

void foo() {
    char* name = malloc(sizeof(char) * 40);
    int age = 0;

    struct st s = (struct st) {
        .n = name,
        .a = age,
    };

    /* TO DO */

    free(name);
}

Comments

1

Declaring a type does not create lvalues (like variable). It defines the format of the lvalue. In 1), you have correctly declared a struct type, but you seem to have assumed the similarity of the "name" variable in struct declaration will associate the pointer "name" to struct member "name". It does not work that way.

2) is syntactically/symantically wrong. You simply cannot assign a malloc() to non-lvalue (as you are declaring a type struct)

So, create a variable out of struct type as you have created in 1) and allocate memory in the struct variable member.

typedef struct st {
    char *name;
    int age;
} st_type;

st_type st_var;
st_var.name = (char *) malloc(sizeof(char) * 40); // This is how you will allocate the memory for the struct variable.

Remember, before you can do dynamic allocation, you need to have a lvalue just like you did for standalone "name" variable.

1 Comment

Can I use "name" in every function without allocate again after this process?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.