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?
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 struct
s 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.
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.
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
.
使用字符串复制功能代替分配char数组。
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.