I have a question...How can I use an array of structs?I managed to create it,but I cannot use it in scanf and printf...I will post here only the part of my code that is necessary...
我有一个问题......我如何使用结构数组?我设法创建它,但我不能在scanf和printf中使用它...我将在这里只发布我的代码的一部分是必要的...
The main function is:
int main()
{
int r;
struct word *s;
s=(struct word*)malloc(sizeof(struct word));
struct word hashtable[100];
s->name=(char*)malloc(20*sizeof(char));
scanf("%s",s->name);
r=hashnumber(s->name);
char *name="example.txt";
if(hashtable[r]->name==NULL)
treecreation(&hashtable[r],s);
else
hashtable[r]=s;
printf("%s",hashtable[r]->name);
printresults();
system("pause");
return(0);
}
struct word is:
struct position
{
char *filename;
int line;
int place;
struct position *next;
};
struct word
{
char *name;
struct word *right;
struct word *left;
struct position *result;
};
And function treecreation is like:
而功能树创建就像:
void treecreation(struct word **w1,struct word *w2)
Do not bother with the rest of my functions...I believe that they work...The main problem is how to use that array of structs...Right now,my program does not compile,because of the "if" statement,the "treecreation" and the printf..What should I do?Any help would be appreciated...
不要理会我的其余功能...我相信它们有效...主要问题是如何使用该结构数组...现在,我的程序无法编译,因为“if”语句,“树创造”和printf ..我该怎么办?任何帮助将不胜感激...
3 个解决方案
#1
3
Your program is not compiling beause your variable hashtable
has the wrong type.
您的程序未编译因为您的变量哈希表具有错误的类型。
You want to store s
in it. s
is a pointer to word. Therefore, you hashtable
has to be an array of pointers to word:
你想把s存储在里面。 s是指向word的指针。因此,哈希表必须是指向word的指针数组:
struct word *hashtable[100];
Now, when you call treecreate
you just need to pass the word:
现在,当你调用treecreate时,你只需要传递这个词:
treecreation(hashtable,s);
#2
0
The ->
operator is for selecting a field from a struct via a pointer to that struct. hashtable[r]
is a struct, not a pointer to one. You use the ordinary .
operator to select a member, just as if you were operating on a scalar struct word
(which you are):
- >运算符用于通过指向该结构的指针从结构中选择一个字段。 hashtable [r]是一个结构,而不是指向一个结构的指针。你使用普通的。运算符选择一个成员,就像你在一个标量结构单词(你是)上操作一样:
if (hashtable[r].name == NULL) {
...
#3
0
The type of hashtable[r]
is struct word
. The type of &hashtable[r]
is struct word*
. That explains why you should not use &hashtable[r]
as an argument to treecreation
.
哈希表[r]的类型是结构词。 &hashtable [r]的类型是struct word *。这就解释了为什么你不应该使用&hashtable [r]作为树创建的参数。
What you need to pass to treecreation
depends on what you are doing with the argument w1
in the function.
你需要传递给treecreation的是什么取决于你在函数中使用参数w1做什么。
If you are allocating memory and assigning to *w1
, then, you need to use:
如果要分配内存并分配给* w1,则需要使用:
struct word* hashtable;
treecreation(&hashtable, s);
#1
3
Your program is not compiling beause your variable hashtable
has the wrong type.
您的程序未编译因为您的变量哈希表具有错误的类型。
You want to store s
in it. s
is a pointer to word. Therefore, you hashtable
has to be an array of pointers to word:
你想把s存储在里面。 s是指向word的指针。因此,哈希表必须是指向word的指针数组:
struct word *hashtable[100];
Now, when you call treecreate
you just need to pass the word:
现在,当你调用treecreate时,你只需要传递这个词:
treecreation(hashtable,s);
#2
0
The ->
operator is for selecting a field from a struct via a pointer to that struct. hashtable[r]
is a struct, not a pointer to one. You use the ordinary .
operator to select a member, just as if you were operating on a scalar struct word
(which you are):
- >运算符用于通过指向该结构的指针从结构中选择一个字段。 hashtable [r]是一个结构,而不是指向一个结构的指针。你使用普通的。运算符选择一个成员,就像你在一个标量结构单词(你是)上操作一样:
if (hashtable[r].name == NULL) {
...
#3
0
The type of hashtable[r]
is struct word
. The type of &hashtable[r]
is struct word*
. That explains why you should not use &hashtable[r]
as an argument to treecreation
.
哈希表[r]的类型是结构词。 &hashtable [r]的类型是struct word *。这就解释了为什么你不应该使用&hashtable [r]作为树创建的参数。
What you need to pass to treecreation
depends on what you are doing with the argument w1
in the function.
你需要传递给treecreation的是什么取决于你在函数中使用参数w1做什么。
If you are allocating memory and assigning to *w1
, then, you need to use:
如果要分配内存并分配给* w1,则需要使用:
struct word* hashtable;
treecreation(&hashtable, s);