“如何打印字段的所有值”;单元格->;pos";此结构在c中的位置:
如何打印单元格->位置的所有值 为什么会出现错误以及如何解决此错误 “错误:“单元格”前应包含表达式” 我试着这样做“如何打印字段的所有值”;单元格->;pos";此结构在c中的位置:,c,struct,C,Struct,如何打印单元格->位置的所有值 为什么会出现错误以及如何解决此错误 “错误:“单元格”前应包含表达式” 我试着这样做 typedef struct cell { int pos[6][9]; struct cell* next; }cell; void print_cell() { int col,row; for (row = 0; row < 6; row++) { for (col = 0; col < 9; col++)
typedef struct cell
{
int pos[6][9];
struct cell* next;
}cell;
void print_cell()
{
int col,row;
for (row = 0; row < 6; row++) {
for (col = 0; col < 9; col++)
{
printf("%d " , cell->pos[row][col]);
}
printf("\n");
}
}
int main(){
print_cell();
return 0;
}
typedef结构单元
{
国际邮品[6][9];;
结构单元*下一步;
}细胞;
无效打印单元()
{
int col,世界其他地区;
用于(行=0;行<6;行++){
用于(列=0;列<9;列++)
{
printf(“%d”,单元格->位置[行][col]);
}
printf(“\n”);
}
}
int main(){
打印单元();
返回0;
}
它给出了一个错误
错误:“单元格”前应包含表达式。- 您刚刚声明了type
,但没有创建具有该数据类型的变量。创建一个cell
用于处理指针。使用->
直接访问struct的成员- 您应该包括
或声明stdio.h
以使用该函数printf()
#include <stdio.h> /* include proper header */
typedef struct cell
{
int pos[6][9];
struct cell* next;
}cell;
cell cell_value; /* create a variable */
void print_cell()
{
int col,row;
for (row = 0; row < 6; row++) {
for (col = 0; col < 9; col++)
{
printf("%d " , cell_value.pos[row][col]); /* use the variable */
}
printf("\n");
}
}
int main(void){
print_cell();
return 0;
}
在您的代码中没有名为
cell
的变量。只有一种类型的名称。不清楚您真正打算做什么,因为也没有初始化结构中的值的代码。在编码风格方面,最好避免直接在打印单元
的主体中使用像6
和9
这样的“神奇常量”。两者都可以分别替换为sizeof c->pos/sizeof c->pos[0]
和sizeof c->pos[0]/sizeof c->pos[0][0]
或更清晰的符号常量,如#define pos_WIDTH 6
和#define pos u HEIGHT 9
。切勿自行声明标准库的函数。始终使用标准标题!这不仅是因为在对标头进行更好的测试时,可能会引入输入错误或错误,更糟糕的是,因为实现可能会添加不打算由最终客户使用的属性等。他们还可能为函数使用宏包装器。请特别不要向初学者推荐!
#include <stdio.h> /* include proper header */
typedef struct cell
{
int pos[6][9];
struct cell* next;
}cell;
cell cell_value; /* create a variable */
void print_cell()
{
int col,row;
for (row = 0; row < 6; row++) {
for (col = 0; col < 9; col++)
{
printf("%d " , cell_value.pos[row][col]); /* use the variable */
}
printf("\n");
}
}
int main(void){
print_cell();
return 0;
}
#include <stdio.h>
typedef struct cell
{
int pos[6][9];
struct cell* next;
}cell;
void print_cell(const cell* c)
{
int col,row;
for (row = 0; row < 6; row++) {
for (col = 0; col < 9; col++)
{
printf("%d " , c->pos[row][col]);
}
printf("\n");
}
}
int main(void){
cell c = {{{0}}, NULL};
print_cell(&c);
return 0;
}