声明一个指向C中typedef结构的指针。

huangapple go评论62阅读模式
英文:

trying to declare a pointer to a typedef structure in C

问题

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <byteswap.h>

#define WFM_HEADER_SIZE 838

typedef struct WfmInfo {
    uint16_t byte_order;
    //  char version[8];
    uint32_t imp_dim_count;
    uint32_t exp_dim_count;
    uint32_t record_type;
    uint32_t exp_dim_1_type;
    uint32_t time_base_1;
    uint32_t fastframe;
    uint32_t Frames;
    double   tstart;
    double   tscale;
    double   tfrac;
    double   tdatefrac;
    int32_t  tdate;
    uint32_t dformat;
    double   vscale;
    double   voffset;
    uint32_t pre_values;
    uint32_t post_values;
    uint32_t avilable_values;
    uint32_t dpre;
    uint32_t dpost;
    uint16_t bps;
    uint32_t code;
    uint32_t readbytes;
    uint32_t allbytes;
    uint32_t samples;
    uint64_t curve_offset;
    uint32_t available_values;
} WfmInfo;

typedef WfmInfo* WfmInfo_ptr;

int testFuck() {
    WfmInfo_ptr info;
    printf("info address %p\n", info);
    printf("byte order %p\n", info->byte_order);
    cout<<"info declared"<<endl;
    return 0;
}
英文:

converting some python code to C. 'just' want to declare a typedef structure and a pointer to it... this is segfaulting at printf( "byte order %p\n", info->byte_order);
How is this supposed to be done? please help. Trying to follow https://stackoverflow.com/questions/1543713/c-typedef-of-pointer-to-structure but I guess it is outdated.

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdint.h&gt;
#include &lt;inttypes.h&gt;
#include &lt;Byteswap.h&gt;

#define WFM_HEADER_SIZE 838

typedef struct WfmInfo{
  uint16_t byte_order;
//  char version[8];
  uint32_t imp_dim_count;
  uint32_t exp_dim_count;
  uint32_t record_type;
  uint32_t exp_dim_1_type;
  uint32_t time_base_1;
  uint32_t fastframe;
  uint32_t Frames;
  double   tstart;
  double   tscale;
  double   tfrac;
  double   tdatefrac;
  int32_t  tdate;
  uint32_t dformat;
  double   vscale;
  double   voffset;
  uint32_t pre_values;
  uint32_t post_values;
  uint32_t avilable_values;
  uint32_t dpre;
  uint32_t dpost;
  uint16_t bps;
  uint32_t code;
  uint32_t readbytes;
  uint32_t allbytes ;
  uint32_t samples;
  uint64_t curve_offset;
  uint32_t available_values;
} WfmInfo;

typedef WfmInfo* WfmInfo_ptr;

int testFuck(){
    WfmInfo_ptr info;
    printf( &quot;info address %p\n&quot;, info);
    printf( &quot;byte order %p\n&quot;, info-&gt;byte_order);
    cout&lt;&lt;&quot;info declared&quot;&lt;&lt;endl;
    return 0;
}

答案1

得分: 1

这是一个最小的例子。

&gt; 想要声明一个typedef结构和一个指向它的指针...

编译如下:
```text
gcc main.c -o main
./main

看到像这样的输出:

info address 0x7ff7be2e19b8
byte order 0x7ff7be2e19b8 = 3
// main.c
#include &lt;stdio.h&gt;
#include &lt;inttypes.h&gt;

typedef struct WfmInfo{
  uint16_t byte_order;
} WfmInfo;

typedef WfmInfo* WfmInfo_ptr;

int main(){
    // 在堆栈上分配结构
    WfmInfo info = { 3 };
    // 将指针分配给堆栈上的地址
    WfmInfo_ptr info_ptr = &amp;info;

    // 使用结构地址显示地址
    printf( &quot;info address %p\n&quot;, &amp;info);

    // 使用指针显示地址,并使用指针访问结构元素的值
    printf( &quot;byte order %p = %d\n&quot;, info_ptr, info_ptr-&gt;byte_order);
    return 0;
}
英文:

Here is a minimal example.

> want to declare a typedef structure and a pointer to it...

Compile like this:

gcc main.c -o main
./main

See output like this:

info address 0x7ff7be2e19b8
byte order 0x7ff7be2e19b8 = 3
// main.c
#include &lt;stdio.h&gt;
#include &lt;inttypes.h&gt;

typedef struct WfmInfo{
  uint16_t byte_order;
} WfmInfo;

typedef WfmInfo* WfmInfo_ptr;

int main(){
    // allocate the structure on the stack
    WfmInfo info = { 3 };
    // assign pointer the address on the stack
    WfmInfo_ptr info_ptr = &amp;info;

    // display address using structure address
    printf( &quot;info address %p\n&quot;, &amp;info);

    // display address using pointer and display value using pointer to access elements of structure
    printf( &quot;byte order %p = %d\n&quot;, info_ptr, info_ptr-&gt;byte_order);
    return 0;
}

答案2

得分: 0

您的typedef定义是正确的,WfmInfo_ptr 是指向 WfmInfo 结构体的指针类型。

main 函数中的问题在于您定义了一个指针 info 但没有初始化它,因此两个 printf 调用都具有未定义的行为:

  • printf( "info address %p\n", info); 具有未定义的行为,因为 %p 需要一个 void *,而您提供的是一个 WfmInfo *,在传递给可变参数函数时不会隐式转换。
  • info 未初始化,因此即使它被传递为 (void *)info,输出也将毫无意义。
  • printf( "byte order %p\n", info->byte_order) 具有未定义的行为,因为您在未初始化的指针 info 上进行了解引用。
  • cout<<"info declared"<<endl; 是C++代码,而不是C。

此外,将指针隐藏在typedef背后被认为是令人困惑和容易出错的做法。

info 定义为 WfmInfo *info; 非常可读,明显表明 info 是一个指针,它不会被隐式初始化(不像在C++中可能有默认构造函数的结构或类)。

英文:

Your typedefs are correct, WfmInfo_ptr is the type of a pointer to a WfmInfo structure.

The problem in the main function is you define such a pointer info but do not initialize it, so both printf calls have undefined behavior:

  • printf( &quot;info address %p\n&quot;, info); has undefined behavior because you %p expects a void * and you provide a WfmInfo *, which is not implicitly converted when passed to a vararg function
  • info is uninitialized, so even if it was passed as (void *)info, the output would be meaningless.
  • printf( &quot;byte order %p\n&quot;, info-&gt;byte_order) has undefined behavior because you dereference an uninitialized pointer info.
  • cout&lt;&lt;&quot;info declared&quot;&lt;&lt;endl; is C++ code, not C.

Note also that hiding pointers behind typedefs is considered confusing and error prone.

Defining info as WfmInfo *info; is quite readable and makes it obvious that info is a pointer that is not implicitly initialized (unlike a structure or class that might have a default constructor in C++).

huangapple
  • 本文由 发表于 2023年2月16日 08:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定