[英]How do i call a member of a struct, that has been populated using a struct pointer function
I would like to know how to print the value of char* word
from this structure. 我想知道如何从该结构打印
char* word
的值。
struct word_count_struct {
char *word;
int count;
};
This structure has been given a value thought the function 该结构已赋予功能以价值
struct word_count_struct *new_word_count( char *word )
{
struct word_count_struct *new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct));
new_word_count->word = "Hello";
new_word_count->count = 1;
return new_word_count;
}
When I use word_count_struct new_word_count( &word );
当我使用
word_count_struct new_word_count( &word );
or new_word_count( &word );
或
new_word_count( &word );
to call the function and puts(new_word_count->word);
调用函数并
puts(new_word_count->word);
to print the member from main.c, 从main.c打印成员,
I get the error message 我收到错误消息
error: called object ‘new_word_count’ is not a function or function pointer
and 和
note: declared here
struct word_count_struct *new_word_count;
What I am specifically not sure about is the function call, I know with any other function i would generally use the code function_name(passing arguments);
我具体不确定的是函数调用,我知道对于任何其他函数,我通常都会使用代码
function_name(passing arguments);
but this function returns a struct pointer and there seems to be two names within the function signature. 但是此函数返回一个struct指针,并且函数签名中似乎有两个名称。
If I do not use a function to assign value to the struct members, I am able to print out the value of new_word_count->word within main.c no problem, so I believe my issues must come from the function call. 如果我不使用将值赋给struct成员的函数,则可以在main.c中打印出new_word_count-> word的值,这没问题,所以我认为我的问题一定来自函数调用。 Thank you in advance.
先感谢您。
Minimal, Complete and verifiable example: 最小,完整和可验证的示例:
#include <stdio.h>
#include <stdlib.h>
struct word_count_struct {
char *word;
int count;
};
struct word_count_struct *new_word_count( char *word );
int main()
{
struct word_count_struct *wordcountptr;
char *word = "Hello";
wordcountptr = new_word_count(word);
puts(wordcountptr->word); /* print out the string "Hello" which was assigned to this struct member though the word_count_struct function */
free(wordcountptr);
return 0;
}
/* this function should take the value of the passing argument and assign it to the word member of struct word_count_struct and then return a pointer to that struct */
struct word_count_struct *new_word_count( char *word )
{
struct word_count_struct *new_word_count;
new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct)); /* allocate memory for struct */
new_word_count->word = &word; /* assign the memory address of passing argument to this member */
new_word_count->count = 1;
return new_word_count;
}
You probably want this: 您可能想要这样:
struct word_count_struct *wordcountptr;
char *word;
wordcountptr = new_word_count(word);
puts(wordcountptr->word);
this code compiles and runs but it is more or less useless because the new_word_count
function is not finished. 该代码可以编译并运行,但是它几乎没有用,因为
new_word_count
函数尚未完成。
Question has changed... 问题已经改变...
The problem is now here: 现在的问题在这里:
You need this: 你需要这个:
new_word_count->word = word;
instead of 代替
new_word_count->word = &word;
BTW your compiler should have warned you. 顺便说一句,您的编译器应该已经警告过您。
Complete function: 完整功能:
struct word_count_struct *new_word_count(char *word)
{
struct word_count_struct *new_word_count;
new_word_count = (struct word_count_struct *)malloc(sizeof(struct word_count_struct));
new_word_count->word = word;
new_word_count->count = 1;
return new_word_count;
}
But with this approach you will most likely run into other problems because you only store the address of the word to be counted, but you probably rather want to store the word itself. 但是,使用这种方法,您很可能会遇到其他问题,因为您仅存储要计数的单词的地址 ,而您可能想存储单词本身。 Instead of:
代替:
new_word_count->word = word;
you probably need: 您可能需要:
new_word_count->word = malloc(strlen(word) + 1); // allocate memory for word
strcpy(new_word_count->word, word); // copy the word
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.