【问题标题】:syntax error before '[' token'[' 标记前的语法错误
【发布时间】: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


【解决方案1】:

这个声明并没有像你想象的那样做:

typedef struct std_thread
{
  ...
}std[5];

这声明了一个名为std_threadstruct,然后它创建了一个名为stdtypedef,表示“5 个struct std_thread 对象的数组”。

您可能需要这两个定义之一,以便将名为 std 的全局对象声明为由 5 个 struct std_thread 组成的数组:

typedef struct std_thread
{
  ...
} std_thread;
std_thread std[5];

// OR

struct std_thread
{
  ..
} std[5];

在第一种情况下,我们还创建了名为std_threadtypedef 作为struct std_thread 的别名;在第二种情况下,我们没有。

此外,正如其他人所说,您不能通过赋值复制字符数组。您必须使用strcpy(3)strncpy(3) 等函数复制它们。使用strcpy 时,您必须确保目标缓冲区足够大以容纳所需的字符串。还要记住strncpy does not necessarily null-terminate its destination string,所以请谨慎使用。

【讨论】:

    【解决方案2】:

    我不认为你是想把typedef 顶上去。

    您在那里写的内容使std 与数组中的五个structs 等效type,但随后您使用std,就好像它本身就是一个数组一样。

    从第五行删除typedef这个词,它应该更接近工作。

    【讨论】:

      【解决方案3】:

      使用字符串复制功能代替分配char数组。

      【讨论】:

        【解决方案4】:
        1. 您正在尝试使用赋值复制字符串:

          std[i].name=names[i]; 
          

          std[i].dept=dept;
          

          那行不通。请改用strcpy 或更好的strncpy

        2. 你有一个错字:

          std[i].empid=empdid;  //Error at this line 
          

          您没有名为 empdid 的 var。

        【讨论】:

          【解决方案5】:

          这段代码:

          typedef struct std_thread
          {
              char name[20];
              int  hallno;
              int  empid;
              char dept[5];
          } std[5];
          

          std 类型声明为由5 个struct std_thread 结构组成的数组。

          这段代码:

          int main()
          {
          [...]
              int i;
              int status;
              for (i = 0; i < 5; i++)
              {
                  std[i].name = names[i]; 
          

          假设std是一个数组或指针类型的变量。

          需要去掉关键字typedef,使代码与变量的使用一致。

          然后你就可以开始遇到字符串赋值的问题了,你需要使用字符串复制函数等。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-02
            • 1970-01-01
            • 2013-09-04
            相关资源
            最近更新 更多