繁体   English   中英

分配一个新结构,使用提供的参数对其进行初始化

[英]Allocate a new structure initializes it with the provided parameters

我想创建一个代表一个人的结构。 所以我开始声明一个包含 3 个字段的结构 People: - 名字:指向代表名字的字符串的指针 - 姓氏:指向代表姓名和年龄的字符串的指针:代表年龄的 integer。

现在我必须编写一个 function,它分配一个新的 People 结构并使用提供的参数的副本对其进行初始化。

我遇到的问题是程序总是崩溃。 我不太明白如何在不崩溃的情况下填充结构(那是我学校的在线编译器)。 有人可以解释并纠正我吗?

这是它的样子:

阵列.h:

#ifndef _PEOPLE_H_                                           
#define _PEOPLE_H_                                           
                                             
typedef struct {                                             
    char* firstname;                                             
    char* lastname;                                          
    int age;                                             
} People;                                            
                                             
People* createPeople(char* firstname, char*  lastname, int age);                                             
                                             
#endif                                            

阵列.c:

#include "People.h"                                          
People* createPeople(char* firstname, char*  lastname, int age) {                                            
    People people1;                                          
                                             
    people1.firstname = firstname;                                           
    people1.lastname = lastname;                                             
    people1.age = age;                                           
}                                            

您不能返回指向局部变量的指针,因为它在返回 function 时超出 scope。这意味着您需要传入 Person 的实例,或者像这里一样使用malloc()动态分配实例。 我还在两个字符串上调用了strdup() ,在这个例子中它不是必需的,但是createPeople() function 不知道它们指向的对象何时不在 scope 中。小问题,People 是复数,但你只创建一个人。

#include <stdlib.h>
#include <string.h>

typedef struct {
    char* firstname;
    char* lastname;
    int age;
} Person;

Person* createPerson(char* firstname, char*  lastname, int age) {
    Person *p = malloc(sizeof *p);
    if(!p) return NULL;
    p->firstname = strdup(firstname);
    p->lastname= strdup(lastname);
    p->age = age;
    return p;
}

int main() {
    Person *p = createPerson("Clark", "Kent", 42);
    free(p->firstname);
    free(p->lastname);
    free(p);
}

您首先必须包含stdlib.h才能使用mallocfree 然后你分配足够的 memory 来保存struct People using malloc

如果 memory 分配失败,则返回NULL

如果 memory 分配顺利,则分配结构值的每个成员并返回它。

#include "People.h"
#include <stdlib.h>
                                          
People* createPeople(char* firstname, char*  lastname, int age) {                                            
    People *people1 = malloc(sizeof(*people1));
    if (!people1)
         return (NULL);                             
    people1->firstname = firstname;                                           
    people1->lastname = lastname;                                             
    people1->age = age;

   return (people1);                                           
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM