i have written one programm in c using structure to enter name and account number of the emplpyee.the programm shows an error which is
filename.c:11 error:expected identifier or "("before"["token..i am not able to get the error...please clearify it.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 main()
{
	struct wipro
	{
		char name[20];
		char accnum[10];
	};
	struct wipro_employee[5]
	    {
		 int i;
		char j[8];
		for (i = 0;i < 5;i++)
		{
			printf("enter the name of employee");
			gets("employee[i].name");
			printf("%accnum");
			gets("j");
			employee[i].accnum = atoi(j);
		}
		for (i = 0;i < 5;i++)
		{
			printf("the name of the employee is %s\n", employee[i].name);
			printf("his age is %d\n", employee[i].accnum);
			getch();
		}
	}
	}

Dani AI

Generated

The compiler message "expected identifier or '(' before '[' token" comes from the malformed declaration where square brackets are used in the place of a variable name. The intent was an array of the previously defined struct type, but the brackets must follow the variable name (the declaration and the array size belong after the identifier). The posted braces also put the for-loops inside a block that the compiler treats as part of a type/context instead of executable code. 's layout shows the correct structure, and is right that the program should use a proper int main() and return a value.

The corrupted output seen later matches classic undefined behaviour: the code was passing string literals (for example with quotes around employee[i].name) to input functions, which attempts to write into read‑only memory and clobbers nearby data. gets(...) is unsafe and has been removed from modern C; prefer line-based input and sanitize the buffer. A safe read-and-trim pattern is:

fgets(employee[i].name, sizeof employee[i].name, stdin);
employee[i].name[strcspn(employee[i].name, "\n")] = '\0';

For numeric fields use strtol (not atoi) so invalid input can be detected and range errors handled. If scanf appears to "skip" input, the usual cause is mixing scanf (token-based) and fgets (line-based): scanf leaves the trailing newline in stdin, so the next fgets returns an empty line. Either use fgets for all reads and parse tokens from the string, or consume the rest of the line after scanf.

Quick checklist (addresses the issues seen by and suggestions from ):

  • Declare the array of structs properly and keep loops/variables inside main.
  • Never pass quoted literals to input functions.
  • Make accnum an integer type; parse with strtol and check the result.
  • Replace gets with fgets and strip the newline.
  • Compile with warnings enabled (e.g. -Wall -Wextra) and use tools (Valgrind or similar) to catch memory errors.

Recommended Answers

All 7 Replies

A structure is a collection of one or more variable , possibly of different types,grouped together under a single name for convenient handling.

For the first time,I am seeing such a use of structure..
Can you please explain me your code.

possibly what I understand is, you are trying to do this:-

#include<stdio.h>
#include<stdlib.h>
 struct wipro
{
		char name[20];
		int accnum;
};
 main()
{
	struct wipro employee[5];
		 int i;
		char j[8];
		for (i = 0;i < 5;i++)
		{
			printf("enter the name of employee");
			scanf("%s",employee[i].name);
			printf("accnum:-");
			scanf("%s",j);
			employee[i].accnum = atoi(j);
		}
		for (i = 0;i < 5;i++)
		{
			printf("the name of the employee is %s\n", employee[i].name);
			printf("his accno is %d\n", employee[i].accnum);
		}
}

You forgot.

The main function should return an int.

ohh yep..
I should ve written int main() instead..and should have included return 0;
I ll keep this is my mind from the next time..

thanks for replying .yeah i want to do that what u stated but .i cant to use gets().
i will keep in my mind your syntax.thanks but let me know the way to use get in this syntax

thanks for replying .
yeah i want to do that only what you stated through your code .but i want to use gets()function to input data..suggest me the possible syntsax.and what does the error that i have stated mean.

here's the syntax:

char * gets ( char * str );

Don't wrap the parameter with inverted commas.

Btw, if you googl'ed for the syntax, you would've got it in seconds!

Anyway, now that you know about gets(...), know it's pitfalls too...read this

Consider using fgets(...) as suggested.

thanks avinash..i tried ur code but scanf is not working in it ..when i m using gets it is showing the output.
but it is taking 5 respective input that i m giving..and finally it is showing the following
suppose when i enter 5 names and 5 accnum
amit
758768
gaurav
9789689
sanjay
868969857
ashish
8689698
avinash
79868958
it is showing as
the employee name is5786896
his accnum is686862.
the data is not exact..i just tried to explain.please figure out what is the problem behind the programm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.