syntax error before '[' token - c

Here is the code
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<pthread.h>
typedef struct std_thread
{
char name[20];
int hallno;
int empid;
char dept[5];
}std[5];
void *print(void *args)
{
struct std_thread *data=(struct std_thread *)args;
printf("My thread id is %ld\n",pthread_self());
printf("Thread %d is executing\n",args);
printf("Name\tHall No\tEmployee ID\tDepartment\n");
printf("--------------------------------------------------------");
printf("%s\t%d\t%d\t%s\n",data->name,data->hallno,data->empid,data->dept);
}
int main()
{
pthread_t th[5];
int empid=2020;
int hall=1;
char dept[2]="IT";
char *names[]={"dinesh","vignesh","pradeep","prasath","mohan"};
int t;
int i;
int status;
for(i=0;i<5;i++)
{
std[i].name=names[i]; //Getting error from this line
std[i].hallno=hall; //Error at this line
hall++;
std[i].empid=empid; //Error at this line
empid++;
std[i].dept=dept; //Error at this line
status=pthread_create(&th[i],NULL,print,(void *)&std[i]);
if(status)
{
printf("Error creating threads\n");
exit(0);
}
}
pthread_exit(NULL);
}
While compiling this code, I'm getting "syntax error before '[' token". What is the reason for this?

This declaration does not do what you think it does:
typedef struct std_thread
{
...
}std[5];
This declares a struct named std_thread, and then it creates a typedef named std to mean "an array of 5 struct std_thread objects".
You probably wanted one of these two definitions, in order to declare a global object named std as an array of 5 struct std_thread's:
typedef struct std_thread
{
...
} std_thread;
std_thread std[5];
// OR
struct std_thread
{
..
} std[5];
In the first case, we also create the typedef named std_thread as an alias for struct std_thread; in the second case, we do not.
Furthermore, as others have stated, you cannot copy character arrays by assignment. You must copy them using a function such as strcpy(3) or strncpy(3). When using strcpy, you must ensure that the destination buffer is large enough to hold the desired string. Also keep in mind that strncpy does not necessarily null-terminate its destination string, so use it with caution.

I don't think you mean to typedef up the top.
What you've written there makes std an equivalent type as five of the structs in an array, but then you use std as though it is itself an array.
Remove the word typedef from line five and it should be closer to working.

Use string copy function instead of assigning char array.

You are trying to copy a string using assignment:
std[i].name=names[i];
and
std[i].dept=dept;
that does not work. Use a strcpy or better strncpy instead.
You have a typo in:
std[i].empid=empdid; //Error at this line
You don't have a var named empdid.

This code:
typedef struct std_thread
{
char name[20];
int hallno;
int empid;
char dept[5];
} std[5];
declares the type std to be an array of 5 struct std_thread structures.
This code:
int main()
{
[...]
int i;
int status;
for (i = 0; i < 5; i++)
{
std[i].name = names[i];
assumes that std is a variable with array or pointer type.
You need to remove the keyword typedef to make the code consistent with the use of the variable.
Then you can start running into problems with string assignments where you need to use string copy functions, etc.

Related

Pass a string in a struct to a function and return it

I want to return the name of the smallest city population-wise, if it is the second city. (Please don't mind the if statement, I know it's bland), the missing return is what bothers me.
I assume I should declare a pointer inside the function rSmallestCity, like *rtrn but I guess the source variable is destroyed before it is used?
typedef struct Coordinate{
int x,y;
}Coordinate;
typedef struct city{
char name[20];
int population;
Coordinate coordinates;
}city;
char *rSmallestCity(city **cl, int n)
{
char *rtrn = NULL;
if(cl[n-2]->population>cl[n-1]->population)
{
rtrn = &cl[n-1]->name;
}
return rtrn;
}
int main()
{
city c1 ={.name="Mumbai", .population=310, .coordinates.x=3, .coordinates.y=4};
city c2 ={.name="Delhi", .population=300, .coordinates.x=3, .coordinates.y=2};
city *clist[2];
clist[0]=&c1;
clist[1]=&c2;
printf("\n%s is smallest\n",rSmallestCity(clist,2));
}
warning: assignment to 'char ' from incompatible pointer type 'char ()[20]' [-Wincompatible-pointer-types]|
I assume I should declare a pointer inside the function rSmallestCity, like *rtrn but I guess the source variable is destroyed before it is used?
A good question. And your assumption is correct. Creating a variable inside a function it's existence ends upon leaving the function. But in this case, because the struct member name is already a char * you do not need to create another variable. Just return c1.name. (see code example below.)
A few other suggestions:
In the struct declaration:
typedef struct Coordinate{
int x,y;
}Coordinate;
You've used the same symbol (Coordinate) for the struct name, and for it's typedef. This is not a good practice. If you need both a struct name and a typedef, pick different symbols. BTW, in this this example, only one or the other is needed. Say you pick the typedef, then the struct is completely defined by:
typedef struct {
int x,y;
}Coordinate;
That suggestion applies to both struct declarations in your example code.
The signatures for the main function do not include int main(){...} rather
int main(void){..., return 0;} and int main(int argc, char *argv[]){..., return 0;}
The following code example illustrates some of the other suggestions for improvements in comments under your post,
typedef struct {
int x,y;
}Coordinate;
typedef struct {
char name[20];
int population;
Coordinate coordinates;
}city;
//return char * rather than char to allow for full null terminated char array (string)
char * rSmallestCity(city c1[],int cityCount)//generisize function prototype to
{ //to easily accommodate bigger arrays if needed
long long size, sizeKeep = 8e9; //index and population. initialize larger than possible population
int indexKeep = 0;
//note you do not need to define a char *, the struct already contains one
for(int i=0; i<cityCount; i++)//use a loop rather than a single comparison, keep the smalles
{
size = c1[i].population;
sizeKeep = (size < sizeKeep) ? indexKeep = i, size : sizeKeep;
}
printf("\n%s\n",c1[indexKeep].name);
return c1[indexKeep].name;
};
int main(void)//use minimum signature for main, and call return before leaving.
{
//combining your original declarations and assignments for struct
//into a single declaration/definition.
city c1[] = {{.name="Mumbai", .population=310, .coordinates.x=3, .coordinates.y=4},
{.name="Delhi", .population=300, .coordinates.x=3, .coordinates.y=2}};
int cityCount = sizeof(c1)/sizeof(c1[0]);
printf("\n%s is smallest",rSmallestCity(c1, cityCount));
return 0;
};
The solution that I originally left in comment under OP (remove & in the line &cl[n-1]->name;) needs some explanations to avoid problems later.
(It is an educational answer not a full answer on pointers, array decay, ... And many examples can be found on stackoverflow. I tried to simplify)
Try this simple code.
int main()
{
char myString1[25]="Toulouse" ; // French City
printf("%p\n",myString1);
printf("%p\n",&myString1);
}
The output is the same, but an array name and the address of an array name are not the same. The array name is evaluated to the address of its first element. So it works in your case but a warning is issued during compilation and it is very important. Firstly, do not remove compilation warnings.
Now, try this code :
int main()
{
char myString1[25]="Toulouse" ; // French City
printf("%p\n",myString1+1);
printf("%p\n",&myString1+1);
}
The outputs are different. Because myString1 is evaluated to char* and &myString1 to char [25]. So +1, in the first, case adds one (sizeof char) to the pointer and in the other case, it adds 25.
Delete the "&" in the line:
rtrn = &cl[n-1]->name;
To extremely simplify, you assigned an "address of char[]" to a char*, but array syntax makes it work regardless.

How to find out the amount of elements in array created for a struct?

I created a certain struct and then I went on to create an array for the struct in the following manner:
struct members
{
char name[32];
intmax_t personalID;
}typedef struct members Member;
Member array_member[100];
Later on, I want to know how many elements there are in the array, according to some answers I have read, this should be enough
int nrofmembers = sizeof(array_member) / sizeof(array_member[0]);
But due to my experience, I know that this is not possible if the array itself is a parameter. So I tried this:
int nrofmembers = sizeof(*array_member) / sizeof(array_member[0]);
Unfortunately, this has turned out to be wrong. The value of nrofmembers after this is 1, but that's not true.
Any advice on how to do this?
If you have the pointer you cant use this simple compile time method. You need to pass the size of the array to the function
In C you always pass the pointer even if your declaration is ... foo(Member arr[]) or ... foo(Member arr[100])
struct members
{
char name[32];
intmax_t personalID;
}typedef struct members Member;
Member array_member[100];
int foo(Member *arr, size_t size)
{
/* .... */
}
int main()
{
/* correct */
foo(array_member, sizeof(array_member) / sizeof(array_member[0]));
printf("Hello World");
return 0;
}

how do i get values stored in struct?

I am trying to retrieve values from an array of structs. I do not know the correct ways to retrieve them.
Here is my struct:
struct entry{
char name[NAME_SIZE];
int mark;
};
typedef struct entry Acct;
Acct dism2A03[MAX_ENTRY];
How i assigned values:
void add_new(char *name,int mark){
printf("%s,%d",name,mark);
int v=0;
v=entry_total;
strcpy(dism2A03[v].name,name);
dism2A03[v].mark = mark;
}
What i tried (DOES NOT WORK):
int m=0;
for(m=0;m<MAX_ENTRY;m++){
char name[NAME_SIZE] = dism2A03[m].name;
line 75 >> int mark = dism2A03[m].mark;
printf("\nEntry %d",m);
printf("%s",name);
printf("%d",mark);
}
ERROR:
p9t2.c: In function ‘main’:
p9t2.c:75:5: error: invalid initializer
Your first attempt implies existence of getfield function that takes a struct and a multicharacter char literal and gets the field; there is no such function in C.
Your second attempt is much closer: rather than trying to assign the name to an array, assign it to a char pointer, like this:
int m=0;
for(m=0;m<MAX_ENTRY;m++){
// Since you aren't planning on modifying name through pointer,
// you can declare the pointer const to make your intentions clear.
const char *name = dism2A03[m].name;
int mark = dism2A03[m].mark;
printf("\nEntry %d",m);
printf("%s",name);
printf("%d",mark);
}

What am i doing wrong in this code?

#include <stdio.h>
main()
{
typedef struct{
char *name;
int age;
}person[5];
int i;
for (i=0;i<5;i++){
printf ("name:");
scanf("%s",person[i].name);
printf("\nage:");
scanf("%d",&person[i].age);}
for (i=0;i<5;i++){
printf ("person:%d",i);
printf ("name:%s",person[i].name);
printf ("age:%d",person[i].age);
}
}
this is the sample program i have. But while compiling i keep getting the error "expected expression before person in line 10,12,16 and 17? What am i doing wrong?
To fix the syntax error, remove the typedef keyword (you're trying to declare a variable, not a type).
Better yet, change to:
typedef struct{
char *name;
int age;} Person;
Person person[5];
Also, the following is wrong:
scanf("%s",person[i].name);
You need to first allocate memory for person[i].name (for example, using malloc()).
Lastly, the %s format specifier in the following line is not correct:
printf ("age:%s",person[i].age);
person is a type, not an object. You cannot "scanf() into a type: person".
I'd simply remove the typedef and just leave the struct definition outside the body of main; and create an object inside
struct person { /* ... */ };
int main(void) {
struct person person[5];
/* ... */
return 0;
}

C programming: Dereferencing pointer to incomplete type error

I have a struct defined as:
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
and an array of pointers to those structs:
struct stasher_file *files[TOTAL_STORAGE_SIZE];
In my code, I'm making a pointer to the struct and setting its members, and adding it to the array:
...
struct stasher_file *newFile;
strncpy(newFile->name, name, 32);
newFile->size = size;
newFile->start = first_free;
newFile->popularity = 0;
files[num_files] = newFile;
...
I'm getting the following error:
error: dereferencing pointer to incomplete type
whenever I try to access the members inside newFile. What am I doing wrong?
You haven't defined struct stasher_file by your first definition. What you have defined is an nameless struct type and a variable stasher_file of that type. Since there's no definition for such type as struct stasher_file in your code, the compiler complains about incomplete type.
In order to define struct stasher_file, you should have done it as follows
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
Note where the stasher_file name is placed in the definition.
You are using the pointer newFile without allocating space for it.
struct stasher_file *newFile = malloc(sizeof(stasher_file));
Also you should put the struct name at the top. Where you specified stasher_file is to create an instance of that struct.
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
How did you actually define the structure? If
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
is to be taken as type definition, it's missing a typedef. When written as above, you actually define a variable called stasher_file, whose type is some anonymous struct type.
Try
typedef struct { ... } stasher_file;
(or, as already mentioned by others):
struct stasher_file { ... };
The latter actually matches your use of the type. The first form would require that you remove the struct before variable declarations.
the case above is for a new project. I hit upon this error while editing a fork of a well established library.
the typedef was included in the file I was editing but the struct wasn't.
The end result being that I was attempting to edit the struct in the wrong place.
If you run into this in a similar way look for other places where the struct is edited and try it there.
The reason why you're getting that error is because you've declared your struct as:
struct {
char name[32];
int size;
int start;
int popularity;
} stasher_file;
This is not declaring a stasher_file type. This is declaring an anonymous struct type and is creating a global instance named stasher_file.
What you intended was:
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
};
But note that while Brian R. Bondy's response wasn't correct about your error message, he's right that you're trying to write into the struct without having allocated space for it. If you want an array of pointers to struct stasher_file structures, you'll need to call malloc to allocate space for each one:
struct stasher_file *newFile = malloc(sizeof *newFile);
if (newFile == NULL) {
/* Failure handling goes here. */
}
strncpy(newFile->name, name, 32);
newFile->size = size;
...
(BTW, be careful when using strncpy; it's not guaranteed to NUL-terminate.)
The reason is you did not declare type struct stasher_file, you define a struct variable stasher_file
instead.
In C, the declaration of structure:
struct structure-tag {
member1
member2
...
};
structure-tag is an optional name following the keyword struct.
After declaration, you can define a variable:
struct structure-tag var1, *var2;
Also, you can do both declaration and definition like:
struct structure-tag {
member1
member2
...
} var1, *var2;
So in your case, you could try this:
struct stasher_file {
char name[32];
int size;
int start;
int popularity;
} *files[TOTAL_STORAGE_SIZE];
struct stasher_file *newFile = malloc(sizeof(struct stasher_file));
... other code ...
That's all.

Resources