英文:
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 <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;
}
答案1
得分: 1
这是一个最小的例子。
> 想要声明一个typedef结构和一个指向它的指针...
编译如下:
```text
gcc main.c -o main
./main
看到像这样的输出:
info address 0x7ff7be2e19b8
byte order 0x7ff7be2e19b8 = 3
// main.c
#include <stdio.h>
#include <inttypes.h>
typedef struct WfmInfo{
uint16_t byte_order;
} WfmInfo;
typedef WfmInfo* WfmInfo_ptr;
int main(){
// 在堆栈上分配结构
WfmInfo info = { 3 };
// 将指针分配给堆栈上的地址
WfmInfo_ptr info_ptr = &info;
// 使用结构地址显示地址
printf( "info address %p\n", &info);
// 使用指针显示地址,并使用指针访问结构元素的值
printf( "byte order %p = %d\n", info_ptr, info_ptr->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 <stdio.h>
#include <inttypes.h>
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 = &info;
// display address using structure address
printf( "info address %p\n", &info);
// display address using pointer and display value using pointer to access elements of structure
printf( "byte order %p = %d\n", info_ptr, info_ptr->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( "info address %p\n", info);
has undefined behavior because you%p
expects avoid *
and you provide aWfmInfo *
, which is not implicitly converted when passed to a vararg functioninfo
is uninitialized, so even if it was passed as(void *)info
, the output would be meaningless.printf( "byte order %p\n", info->byte_order)
has undefined behavior because you dereference an uninitialized pointerinfo
.cout<<"info declared"<<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++).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论