当使用void指针指向struct指针时,“使用未声明的标识符”

时间:2016-03-28 13:56:06

标签: c struct

我是C的新手,作为(经典)练习我试图在链表上实现一些操作。我还没有走得太远,但是......当我试图按如下方式声明和初始化根节点时:

#include <stdlib.h>

struct intNode_t {
    int data;
    struct intNode_t* next;
};

int main() {
    struct intNode_t* root = ( intNode_t* ) malloc( sizeof( struct intNode_t ) );
    return 0;
}

编译器(clang)在我试图将malloc返回的void指针强制转换为指向intNode_t的指针的地方给出了错误“使用未声明的标识符”。我意识到这是一个菜鸟问题,但我在其他地方找不到答案。有什么建议吗?

3 个答案:

答案 0 :(得分:4)

C中的结构名称占据了基本类型和typedef名称空间的单独名称空间(有关详细信息,请参阅this question)。因此,结构类型的名称为scrollPane.setFitToWidth(true); scrollPane.setFitToHeight(true); ;没有名为struct intNode_t的类型。

您可以始终将名称拼写为intNode_t,也可以为其创建类型别名。在实际代码中有许多不同的模式:

单独的标签名称,单独声明:

struct intNode_t

这允许您将类型别名放在标题中,并将其作为不透明的API类型发布,而不会泄露实际的类型定义。

分隔标签名称,结构定义中的typedef:

typedef struct foo_t_ foo_t;

struct foo_t_ { /* ... */ };

重用名称:

typedef struct foo_t_ { /* ... */ } foo_t;

此样式允许用户不小心拼写typedef struct foo_t { /* ... */ } foo_t; foo_t

不要命名结构:

struct foo_t

但是,在您的代码中,您实际上不需要重复名称,因为you should not cast the result of malloc而是依赖于从void指针到对象指针的内置隐式转换。您也不应该重复已知的类型,而是使用typedef struct { /* ... */ } foo_t; 的表达形式。所以你想要:

sizeof

(另请注意,int main() { struct intNode_t* root = malloc(sizeof *root); } 隐含return 0。)

答案 1 :(得分:1)

无需重复自己。如果你不重复自己,你可以减少错误。

struct intNode_t *root;
root = malloc( sizeof *root );
  • 不需要演员; malloc()返回void*,可以与每个(非函数)指针类型进行交换。
  • sizeof有两种语法变体:sizeof(type)sizeof expression第一种需要(),第二种不需要*root。大多数人更喜欢第二种形式,因为表达式(在您的情况下为struct intNode_t *root = malloc( sizeof *root ); )总是会产生正确的类型,因此:size。

并且,上面的定义+赋值可以组合成一个定义+初始化,所有都适合一行:

    Number of platforms:                 1
  Platform Profile:              FULL_PROFILE
  Platform Version:              OpenCL 1.2 CUDA 8.0.13
  Platform Name:                 NVIDIA CUDA
  Platform Vendor:               NVIDIA Corporation
  Platform Extensions:               cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts


  Platform Name:                 NVIDIA CUDA
Number of devices:               1
  Device Type:                   CL_DEVICE_TYPE_GPU
  Vendor ID:                     10deh
  Max compute units:                 1
  Max work items dimensions:             3
    Max work items[0]:               1024
    Max work items[1]:               1024
    Max work items[2]:               64
  Max work group size:               1024
  Preferred vector width char:           1
  Preferred vector width short:          1
  Preferred vector width int:            1
  Preferred vector width long:           1
  Preferred vector width float:          1
  Preferred vector width double:         1
  Native vector width char:          1
  Native vector width short:             1
  Native vector width int:           1
  Native vector width long:          1
  Native vector width float:             1
  Native vector width double:            1
  Max clock frequency:               1480Mhz
  Address bits:                  32
  Max memory allocation:             268353536
  Image support:                 Yes
  Max number of images read arguments:       128
  Max number of images write arguments:      8
  Max image 2D width:                16384
  Max image 2D height:               16384
  Max image 3D width:                2048
  Max image 3D height:               2048
  Max image 3D depth:                2048
  Max samplers within kernel:            16
  Max size of kernel argument:           4352
  Alignment (bits) of base address:      4096
  Minimum alignment (bytes) for any datatype:    128
  Single precision floating point capability
    Denorms:                     Yes
    Quiet NaNs:                  Yes
    Round to nearest even:           Yes
    Round to zero:               Yes
    Round to +ve and infinity:           Yes
    IEEE754-2008 fused multiply-add:         Yes
  Cache type:                    Read/Write
  Cache line size:               128
  Cache size:                    16384
  Global memory size:                1073414144
  Constant buffer size:              65536
  Max number of constant args:           9
  Local memory type:                 Scratchpad
  Local memory size:                 49152
  Kernel Preferred work group size multiple:     32
  Error correction support:          0
  Unified memory for Host and Device:        0
  Profiling timer resolution:            1000
  Device endianess:              Little
  Available:                     Yes
  Compiler available:                Yes
  Execution capabilities:                
    Execute OpenCL kernels:          Yes
    Execute native function:             No
  Queue on Host properties:              
    Out-of-Order:                Yes
    Profiling :                  Yes
  Platform ID:                   0x8d3ee00
  Name:                      GeForce GT 520M
  Vendor:                    NVIDIA Corporation
  Device OpenCL C version:           OpenCL C 1.1 
  Driver version:                361.28
  Profile:                   FULL_PROFILE
  Version:                   OpenCL 1.1 CUDA
  Extensions:                    cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_copy_opts

答案 2 :(得分:0)

程序中未声明名称intNode_t。声明了结构标记名intNode_t

所以你需要写

struct intNode_t* root = ( struct intNode_t* ) malloc( sizeof( struct intNode_t ) );

或者您可以通过以下方式引入标识符名称intNode_t

typedef struct intNode_t {
    int data;
    struct intNode_t* next;
} intNode_t;

在这种情况下,您可以写

struct intNode_t* root = ( intNode_t* ) malloc( sizeof( struct intNode_t ) );