如何将结构数组传递给 C 中的函数?
How to pass a structure array to a function in C?
我是C新手。我对以下代码感到困惑,它旨在打印结构数组中的所有元素。我知道它可以直接在 main() 中完成,但是当我将 printf(....) 放入一个函数并调用该函数时,我未能传递结构数组。
有谁知道为什么。我很难过..谢谢
我的结构包括关键字及其计数。初始化包含常量名称集及其计数 0.
#include <stdio.h>
#include <stddef.h>
#define NKEYS (sizeof keytab/ sizeof (Key))
void traversal(Key tab[], int n); // define the function
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key; //define struct key as Key
Key keytab[]={ // initiate the struct array
"auto",0,
"break",0,
"case",0,
"char",0,
"const",0,
"continue",0,
"default",0,
"void",0,
"while",0,
};
int main()
{
traversal(keytab,NKEYS);
return 0;
}
void traversal(Key tab[], int n){
int i;
for (i=0; i<n; i++){
printf("%s\n",keytab[i].word);
}
}
在使用前而不是在
之后声明任何结构或函数
#include <stdio.h>
#include <stddef.h>
#define NKEYS (sizeof keytab/ sizeof (Key))
// define struct first
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key;
//then the functions that uses it
void traversal(Key *tab, int n);
Key keytab[]={
"auto",0,
"break",0,
"case",0,
"char",0,
"const",0,
"continue",0,
"default",0,
"void",0,
"while",0,
};
int main()
{
traversal(keytab,NKEYS);
return 0;
}
void traversal(Key* tab, int n){
int i;
for (i=0; i<n; i++){
printf("%s\n",tab[i].word);
}
}
在 traversal
函数中,您有一个名为 tab
的参数,但实际上您并未使用该参数。相反,您直接使用 keytab
。所以该函数将始终打印 keytab
,即使您传递了其他内容。
此外,您可以通过使用 sentinel value 标记数组的结尾来避免 computing/passing 数组的大小。当您的结构包含指针时,值 NULL
可以作为一个很好的哨兵,例如word
。
#include <stdio.h>
#include <stddef.h>
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key; //define struct key as Key
Key keytab[]={ // initiate the struct array
{ "auto",0 },
{ "break",0 },
{ "case",0 },
{ "char",0 },
{ "const",0 },
{ "continue",0 },
{ "default",0 },
{ "void",0 },
{ "while",0 },
{ NULL,0 } // sentinel value to mark the end of the array
};
void traversal(Key tab[]){
int i;
for (i=0; tab[i].word != NULL; i++){
printf("%s\n",keytab[i].word);
}
}
int main( void )
{
traversal(keytab);
return 0;
}
我是C新手。我对以下代码感到困惑,它旨在打印结构数组中的所有元素。我知道它可以直接在 main() 中完成,但是当我将 printf(....) 放入一个函数并调用该函数时,我未能传递结构数组。 有谁知道为什么。我很难过..谢谢
我的结构包括关键字及其计数。初始化包含常量名称集及其计数 0.
#include <stdio.h>
#include <stddef.h>
#define NKEYS (sizeof keytab/ sizeof (Key))
void traversal(Key tab[], int n); // define the function
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key; //define struct key as Key
Key keytab[]={ // initiate the struct array
"auto",0,
"break",0,
"case",0,
"char",0,
"const",0,
"continue",0,
"default",0,
"void",0,
"while",0,
};
int main()
{
traversal(keytab,NKEYS);
return 0;
}
void traversal(Key tab[], int n){
int i;
for (i=0; i<n; i++){
printf("%s\n",keytab[i].word);
}
}
在使用前而不是在
之后声明任何结构或函数#include <stdio.h>
#include <stddef.h>
#define NKEYS (sizeof keytab/ sizeof (Key))
// define struct first
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key;
//then the functions that uses it
void traversal(Key *tab, int n);
Key keytab[]={
"auto",0,
"break",0,
"case",0,
"char",0,
"const",0,
"continue",0,
"default",0,
"void",0,
"while",0,
};
int main()
{
traversal(keytab,NKEYS);
return 0;
}
void traversal(Key* tab, int n){
int i;
for (i=0; i<n; i++){
printf("%s\n",tab[i].word);
}
}
在 traversal
函数中,您有一个名为 tab
的参数,但实际上您并未使用该参数。相反,您直接使用 keytab
。所以该函数将始终打印 keytab
,即使您传递了其他内容。
此外,您可以通过使用 sentinel value 标记数组的结尾来避免 computing/passing 数组的大小。当您的结构包含指针时,值 NULL
可以作为一个很好的哨兵,例如word
。
#include <stdio.h>
#include <stddef.h>
struct key // create the structure
{ char *word;
int count;
};
typedef struct key Key; //define struct key as Key
Key keytab[]={ // initiate the struct array
{ "auto",0 },
{ "break",0 },
{ "case",0 },
{ "char",0 },
{ "const",0 },
{ "continue",0 },
{ "default",0 },
{ "void",0 },
{ "while",0 },
{ NULL,0 } // sentinel value to mark the end of the array
};
void traversal(Key tab[]){
int i;
for (i=0; tab[i].word != NULL; i++){
printf("%s\n",keytab[i].word);
}
}
int main( void )
{
traversal(keytab);
return 0;
}