小编Seb*_*Seb的帖子

带有malloc的struct的C内存分配

我试图理解C中的结构的内存分配,但我坚持下去.

struct Person {
    char *name;
    int age;
    int height;
    int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
    struct Person *who = malloc(sizeof(struct Person));
    assert(who != NULL);

    who->age = age;
    who->height = height;
    who->weight = weight;
    who->name = strdup(name);

    return who;
}
int main(int argc, char *argv[])
{
    struct Person *joe = Person_create("ABC", 10, 170, 60);
    printf("Size of joe: %d\n", sizeof(*joe));
    printf("1. Address of joe \t= %x\n", joe);
    printf("2. Address of Age \t= %x\n", …
Run Code Online (Sandbox Code Playgroud)

c memory malloc struct memory-management

5
推荐指数
3
解决办法
1282
查看次数

标签 统计

c ×1

malloc ×1

memory ×1

memory-management ×1

struct ×1