C中带指针的嵌套结构

nested structures with pointer in C

我想在 c 中使用带指针的嵌套结构。我写了这段代码,但我不知道为什么这段代码不起作用。实际上我如何为我的第二个结构分配内存?

#include <stdio.h>
#include <stdlib.h>
struct address
{
    int code;
    char city[10];
};
struct student {
    char name[10];
    struct address *ads;
} *person1;

int main()
{

person1 = malloc(sizeof(struct student));
scanf("%s", person1->name);
scanf("%d", &person1->ads->code);
scanf("%s", person1->ads->city);

printf("%s", person1->name);
printf("%d", person1->ads->code);
printf("%s", person1->ads->city);
return 0;
}

注意:当我使用 "person1->ads = malloc(sizeof(struct address));" 程序时 运行 出现问题并停止工作。


[评论更新:]

我使用 DEV C++ v5.4.2

您还需要为通过指针存储的成员分配内存。

int main()
{  
    person1 = malloc(sizeof(struct student));
    person1->ads = malloc(sizeof(struct address));
    scanf("%s", person1->name);
    scanf("%d", &person1->ads->code);
    scanf("%s", person1->ads->city);

    printf("%s", person1->name);
    printf("%d", person1->ads->code);
    printf("%s", person1->ads->city);
    free(person1->ads);
    free(person1);
    return 0;
}

how can i allocate memory for my second structure?

例如与第一个结构相同的方式:从堆中分配它

#include <stdlib.h>
#include <stdio.h>

struct address
{
  int code;
  char city[10];
};

struct student 
{
  char name[10];
  struct address * ads;
};

int main(void)
{
  struct student * person1 = malloc(sizeof * person1);
  if (NULL == person1)
  {
    perror("malloc() failed for person1");
  }
  else
  {
    person1->ads = malloc(sizeof * person1->ads);
    if (NULL == person1->ads)
    {
      perror("malloc() failed for person1->ads");
    }
    else
    {
      /* scan and print */

      free(person1->ads);
    } 

    free(person1);
  }
}

你有一些问题

  1. 您没有检查 malloc 是否成功。
  2. 您没有 malloc person1->ads 会员。
  3. 您没有检查 scanf 是否成功。

这是您的代码的固定注释版本

#include <stdio.h>
#include <stdlib.h>

struct address
{
    int code;
    char city[10];
};

struct student
{
    char name[10];
    struct address *ads;
};

int main()
{
    /* You don't need te struct to be global, and it's generally a bad idea, not always of course */
    struct student *person;

    /* you should check that malloc succeeded otherwise undefined behavior would happen */
    person = malloc(sizeof(*person));
    if (person == NULL)
    {
        printf("cannot allocate memory\n");
        return -1;
    }

    /* you should check that scanf succeeded too */
    if (scanf("%9s", person->name) != 1)
    /*          ^ prevent buffer overflow */
    {
        printf("Invalid, input\n");
        free(person);
        return -1;
    }

    person->ads = malloc(sizeof(*(person->ads)));
    if (person->ads == NULL)
    {
        printf("cannot allocate memory\n");
        /* on failure free successfuly allocated person */
        free(person);
        return -1;
    }

    /* you should check that scanf succeeded too */
    if (scanf("%d", &person->ads->code) != 1)
    {
        printf("Invalid, input\n");

        free(person->ads);
        free(person);

        return -1;
    }

    /* you should check that scanf succeeded too */
    if (scanf("%9s", person->ads->city) != 1)
    /*          ^ prevent buffer overflow */
    {
        printf("Invalid, input\n");

        free(person->ads);
        free(person);

        return -1;
    }

    printf("Name: %s\n", person->name);
    printf("Code: %d\n", person->ads->code);
    printf("City: %s\n", person->ads->city);

    free(person->ads);
    free(person);

    return 0;
}