这个宣言有什么问题?

时间:2012-11-18 06:48:04

标签: c pthreads declaration void-pointers

它是这样的,第四个,第五个:

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

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

在你问之前,论证是void *,因为它被传递给pthread,这就是我被教导的方式,因为这是我第一次穿线(它很痛),我我只是按照我所说的去做,因为我的理解充其量只是微弱。

2 个答案:

答案 0 :(得分:5)

除非你typedef所需的结构:

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

答案 1 :(得分: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. 可以使用

  3. 声明新变量

    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指出的那样。

相关问题