简体   繁体   English

是否有任何 function 将字符串传递给 C 中的结构?

[英]Is there any function that pass a string to a struct in C?

(newbie question) (新手问题)

Look at this struct:看看这个结构:

struct Atom_data{
char *name;
char symbol;
double weight;
};

I want to write a function that gets a struct pointer as input, in this function with the usage of scanf() , I want to give each member of struct a value, which one of them is string .我想编写一个 function ,它获取一个结构指针作为输入,在这个 function 中使用scanf() ,我想给结构的每个成员一个值,其中一个是string So far I have written this function:到目前为止,我已经写了这个 function:

void data_entry(struct Atom_data* ptr){

printf("Please write the Atom's Name:\n");
ptr =(struct Atom_data *)malloc(sizeof *ptr);
ptr->name = malloc(20);

scanf("%s", ptr->name);


printf("Please write the Atom's symbol:\n");
scanf("%c", &ptr ->symbol);


printf("please write the Atom's weight:\n");
scanf("%lf", &ptr ->weight);
}

for symbol and weight there was no problem (when I mask the codes related to name).对于symbolweight没有问题(当我屏蔽与名称相关的代码时)。 But for name there is a crash without Error .但是对于name ,没有Error就会崩溃。 these codes inside the function (specially codes related to name ) worked perfectly outside of the function. function 中的这些代码(特别是与name相关的代码)在 function 之外完美运行。 (I tried to test each part of them on the main function) (我尝试在主要功能上测试它们的每个部分)

I learnt to write this line of code ptr =(struct Atom_data *)malloc(sizeof *ptr);我学会了写这行代码ptr =(struct Atom_data *)malloc(sizeof *ptr); from a question about passing values to pointer!从关于将值传递给指针的问题! but for the line after that which is ptr->name = malloc(20);但是对于ptr->name = malloc(20);之后的行, I just did it based on some ideas about creating dynamic memory for name , which worked outside of the function. ,我只是基于一些关于为name创建动态 memory 的想法,它在 function 之外工作。 (Any clarification about it would show your great favour). (任何关于它的澄清都会显示你的大好)。

void data_entry(struct Atom_data* ptr){

printf("Please write the Atom's Name:\n");
ptr =(struct Atom_data *)malloc(sizeof *ptr);

Will do nothing useful for you.不会对你有用。 You are setting ptr to a dynamically allocated piece of memory, but it won't change the pointer you pass into the function:您正在将ptr设置为动态分配的 memory,但它不会更改您传递给 function 的指针:

struct Atom_data *data = NULL;
data_entry(data);
// data will still be NULL

If the desired behaviour is in fact, to set a pointer from within the function you need to use a "pointer to a pointer" like this:如果实际上是所需的行为,要从 function 中设置指针,您需要使用“指向指针的指针”,如下所示:

void data_entry(struct Atom_data **ptr) {
    *ptr = malloc(sizeof(** ptr));
}

And then later:然后后来:

struct Atom_data *data = NULL;
data_entry(&data);
// data will not be NULL (if malloc worked.)

However, this is unecessarily complicated, the common way to do it is like this:但是,这不必要地复杂,常见的方法是这样的:

struct Atom_data *data_entry() {
    struct Atom_data *ptr = malloc(sizeof(* ptr));
    // check for NULL pointer returned by malloc (out of memory)
    if (!ptr) return NULL;

    // initalisation
    ptr->name = NULL;
    ptr->symbol = 'a';
    ptr->weight = 0;
    return ptr;
}

And use it like this:并像这样使用它:

struct Atom_data *data = data_entry();

Of course, if you want to initialize the struct within the function, you'll need to pass those as well:当然,如果你想初始化 function的结构,你还需要传递这些:

struct Atom_data *data_entry(char *name, char symbol, double weight)

It really depends on how you want to initialize the struct.这实际上取决于您要如何初始化结构。

Note: memory allocated with malloc can/should be "released" (or given back to the OS) with free .注意:分配有 malloc 的malloc可以/应该与free一起“释放”(或归还给操作系统)。 Failing to do so, can lead to memory leaks .如果不这样做,可能会导致memory 泄漏

Also, be careful with scanf :另外,小心scanf

Disadvantages of scanf scanf 的缺点

Reading a string with scanf 使用 scanf 读取字符串

Some other resources, that might be of interest to you:您可能感兴趣的其他一些资源:

Difference between pointer and array 指针和数组的区别

Difference between char * and char [] char * 和 char [] 之间的区别

Difference between "." “.”之间的区别and "->" . 和 "->"

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

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