【发布时间】:2011-12-13 06:11:26
【问题描述】:
这里是代码
#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);
}
编译此代码时,我收到“'[' token 之前的语法错误”。这是什么原因?
【问题讨论】:
-
您的示例包含太多代码来演示问题,请在以后缩短(例如为什么定义了
print函数?)还要注意std用于指示标准C++ 中的命名空间。因此,如果您想将代码集成到 C++ 代码库中,将变量或类型命名为std将变得非常困难......您应该避免这种情况(以及命名class等) -
好的,HostileFork。谢谢你的建议。以后我不会再犯这样的错误了。
标签: c struct syntax-error typedef