Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]);

}
Posted
Updated 25-Apr-18 3:12am

1 solution

Your sprintf() calls are outside of any function. You have to move them into a function (using main() 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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900