C编程中简单结构的问题 [英] Problem with simple struct in C programming
本文介绍了C编程中简单结构的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用C编程问题。我在以全局方式声明结构时编译问题的程序(我需要这样做)。我得到以下编译错误:
Hi, I am having programming issues with C.The program that I am having compiling issues when declaring the structure in a global manner(I need it in this way).I am getting the following compiling errors:
g_str.c:13:9: error: expected declaration specifiers or ‘...’ before ‘men’
sprintf(men[0].n[0],"hello1");
^~~
g_str.c:13:21: error: expected declaration specifiers or ‘...’ before string constant
sprintf(men[0].n[0],"hello1");
^~~~~~~~
g_str.c:14:9: error: expected declaration specifiers or ‘...’ before ‘men’
sprintf(men[1].n[1],"hello2");
^~~
g_str.c:14:21: error: expected declaration specifiers or ‘...’ before string constant
sprintf(men[1].n[1],"hello2");
^~~~~~~~
g_str.c:15:9: error: expected declaration specifiers or ‘...’ before ‘men’
sprintf(men[2].n[2],"hello3");
^~~
g_str.c:15:21: error: expected declaration specifiers or ‘...’ before string constant
sprintf(men[2].n[2],"hello3");
^~~~~~~~
我尝试过:
What I have tried:
#include<stdio.h>
#include<stdlib.h>
struct str1
{
char n[3][7];
};
struct str1 men[3];
sprintf(men[0].n[0],"hello1");
sprintf(men[1].n[1],"hello2");
sprintf(men[2].n[2],"hello3");
void main() // the main function
{
printf(" %s",men[1].n[1]);
}
推荐答案
您的sprintf()
电话不属于任何功能。你必须将它们移动到一个函数中(在这里使用main()
):
Yoursprintf()
calls are outside of any function. You have to move them into a function (usingmain()
here):
int main()
{
sprintf(men[0].n[0],"hello1");
sprintf(men[1].n[1],"hello2");
sprintf(men[2].n[2],"hello3");
printf(" %s",men[1].n[1]);
return 0;
}
这篇关于C编程中简单结构的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文