【问题标题】:What's wrong with this declaration?这个声明有什么问题?
【发布时间】:2012-11-06 10:44:42
【问题描述】:

是这样的,第四,第五:

void * copyFile( void * arg )
{
    struct dirent *ent = (dirent *)arg;
}

GCC 告诉我'dirent' undeclared (first use in this function)

在你问之前,参数是void *,因为它被传递给 pthread,这正是我被教导这样做的方式,而且因为这是我第一次线程化(很痛),我只是做我被告知的事情,因为我在这里的理解充其量是薄弱的。

【问题讨论】:

  • GCC 已经告诉你'dirent' undeclared,然后声明它?
  • 您需要包含定义dirent的标头:#include <dirent.h>
  • 我已经包含了它。代码的其他部分使用它,它没有抱怨它们。

标签: c pthreads declaration void-pointers


【解决方案1】:

除非你typedef你需要的结构:

struct dirent *ent = (struct dirent *)arg;

【讨论】:

    【解决方案2】:

    正如 mux 所解释的,您需要使用

    struct dirent *ent = (struct dirent *)arg;

    我只想说明为什么你必须这样做。当您声明一个结构时,我们会执行以下操作之一

    1.没有 typedef

    struct my_struct         // <----- Name of the structure(equivalent to datatype)
         {
        int x;
        int y;
         } hello;          // <------Variable of type my_struct
    

    现在您可以使用:

    1. hello.x=100;

    2. 声明一个新变量可以使用

    struct my_struct new_variable; (new_variable is new variable of type my_struct)

    2。现在使用 typedef

    typedef struct my_struct
      {
         int x;
         int y;
       } hello;        //<----- Hello is a typedef for **struct my_struct**
    

    所以当你这样做时

    hello new_var; //  <-------- new_var is a new variable of type my_struct
    
    hello.x      //  <-------- Here hello refers to the datatype and not the variable
    

    因此,变量 dirent 在不同的上下文中可能意味着不同的东西:-

    如果你没有提到结构体,编译器会假设它在 typedef 上下文中,因此会发现变量是未声明的。

    因此,您需要直接提及 mux 所指的内容。

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多