How can I do dynamic memory allocation inside an structure?

I'm trying to do a program that read some informations of a person and write them on a binary file, but in the 'name' variable I want to let it free to write an string of any size, then I thought in the use of dynamic memory allocation, and it failed because I can't use it directly in an structure (at least I don't know how to do it)...
1
2
3
4
5
6
7
  typedef struct person {

    char *name;
    int age;
    float height, weight;

} persona;


I left 'name' as an character pointer to read it like an string in the main code but obviously it failed too (this return me (segmentation fault))...

finally my question is : How can I do an dynamic memory allocation inside an structure?

Ps.: Sorry for my english mistakes...
Last edited on
Since you need to read and write a strong of any size, you're better off using the string class instead:

1
2
3
4
5
6
7
#include <string>

typedef struct person {
    std::string name;
    int age;
    float height, weight;
} persona;

@dhayden Since he is using struct and char pointer, I'd guess he's programming C not c++.
Can you show us an example of how you want to use this struct? For instance, how are you using it in your main code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
/*Working with structures and binaries files*/

typedef struct person {

    char *name = (char *)(malloc (sizeof(char)));
    int age;
    float height, weight;

} persona;

//int menu (int *choice)
//{
//   do{
//        
//        puts("=========================");
//        puts("       MAIN MENU         ");
//        puts("=========================");
//        puts ("1- add structure");
//        puts ("2- add ")
//    
//    } while (choose)
//}

int main (void){

    persona pers1, *pers_vet = (persona *)(malloc(sizeof(persona)));
    FILE *pnt;


    pnt = fopen ("registers.bin", "a+b");
    if (pnt == NULL){
        printf ("error to open/create the file!!\n");
        exit(1);
    }

        printf ("Insert your name              : "); gets (pers1.name);
        printf ("Insert your height             : "); scanf ("%f", &pers1.height);
        printf ("Insert your weight            : "); scanf ("%f", &pers1.weight);
        printf ("insert yor age                    : "); scanf ("%d", &pers1.age);
        puts   (""); puts("");

        end_ = fseek (pnt, 0, SEEK_END);
        if ((fwrite (&pers1, sizeof (persona), sizeof(pers1), pnt)) < sizeof(pers1)){
            printf ("Error writing in the file!!\n");
            return 0;
        }

        if ((fread (&pers_vet[0], sizeof(persona), end_, pnt)) != end_)
            printf ("Error reading the file\n");
            return 0;

        printf ("%s", pers_vet->name);
}


here the complete code, (this is working), the error was in another line, I did some changes in the code and later went back to that of the declaration of the char string and tried to use malloc again... in the same way I did before, but now it worked... I still don't know where the error really was but it have to be something really stupid..... Like in the commentary line this is me studying how structures and files works...
My trouble now is with the file.... but to this I'm reading some books
Thanks everyone...
The "gets" function is not safe (it allows you to copy over the allocated size of your char array), while the "fgets" function makes you specify the number of bytes to copy.

I would probably recommend you dynamically allocate your char array to a specified size and use that size in the fgets function. I know that doesn't really get you all the way to what you want, but you at least make sure you aren't overwriting the size of your name character array.
Yeah, I knew this..... (because since I started to study programming in linux I take an warning message about the gets() function... rs), although I like to program in this way because it works exactly like I want, but every time that I define a specified number of elements to an char array I use the fgets() function... anyway thanks for the warning
Topic archived. No new replies allowed.