Tag: struct

MPI派生的数据类型适用于浮点数,但不适用于双精度数。 这是对齐问题吗?

我有一个与C结构相关的奇怪问题,它是在MPI派生数据类型的帮助下进行通信的。 以下示例有效; 它只是发送一个由一个integer加上4个float值组成的消息。 Minmum工作示例: #include #include int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); int i, rank, tag = 1; MPI_Status status; MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Array of doubles plus element count typedef struct { int row; float elements[4]; } My_array; // Derived datatype for an array of doubles plus element count MPI_Datatype MY_ARRAY_TYPE; const int […]

结构初始化不适用于malloc

我在C中有一个小背景,我正在努力挖掘,我遇到了一些我无法解释的事情。 假设我有以下结构: struct World { Things* things; }; 以及以下function: void initWorld(World *world); void addThingToWorld(World *world, Thing *thing); 现在,根据initWorld的实现以及我如何使用它,它将工作与否。 但我不明白为什么第二次实施失败。 两种实现如下: 第一(工作)实施 void initWorld(World *world){ world->things = NULL; } // usage World world; initWorld(&world); // will work, because world has been correctly initialized. addThingToWorld(&world, &something); 第二个(失败)实施 void initWorld(World *world){ world = (World*) malloc(sizeof(World)); world->things = NULL; […]

使用malloc作为结构的多维数组

这可能是一个基本问题,但我想为结构的三维数组分配内存。 我正在尝试从文件中读取双打并想要存储在struct中。 第一行是块编号(这里不相关,因为它总是1),第二行分别表示X,Y和Z坐标中的网格点数。 在这种情况下,X中有10个点,Y中有5个,Z方向上有1个。 从第三行开始,每个点的X,Y,Z坐标是我想要阅读的双打。 首先是所有X分量(即10 * 5 * 1 x坐标,然后类似Y和Z)。 文件格式如下: 1 10 5 1 0.000000e+00 1.111111e+00 2.222222e+00 3.333333e+00 4.444445e+00 5.555555e+00 6.666667e+00 7.777778e+00 8.888889e+00 1.000000e+01 0.000000e+00 1.111111e+00 2.222222e+00 3.333333e+00 4.444445e+00 5.555555e+00 6.666667e+00 7.777778e+00 8.888889e+00 1.000000e+01 0.000000e+00 1.111111e+00 2.222222e+00 3.333333e+00 4.444445e+00 5.555555e+00 6.666667e+00 7.777778e+00 8.888889e+00 1.000000e+01 0.000000e+00 1.111111e+00 2.222222e+00 3.333333e+00 4.444445e+00 5.555555e+00 6.666667e+00 7.777778e+00 8.888889e+00 […]

为什么不能在函数中修改结构的成员变量?

我很好奇为什么在作为参数传递给函数时不能修改结构的变量。 我知道参数是按值传递的,但是在传递struct变量时,您将其引用作为值传递。 当然,C不会在堆栈上创建结构的副本,因为传入的结构引用的值与将结构的指针传递给函数的值相同。 #include #include typedef struct String { char* name; int x; } String; /* Func Protos */ void foo(String str); void bar(String * str); void banjo(String str); int main() { String str = *(String *)malloc(sizeof(String)); str.x = 5; /* x == 5 */ foo(str); /* x == 5 still */ printf(“%d\n”, str.x); banjo(str); […]

C结构到Java JNA结构(指向struct的指针)

我有基于C / C ++结构的JNA结构的问题。 字段nScreenIndex,uVendorID,uProductID,uVersionNumber看起来没问题,但在它们之后我看到奇数字节。 我的主要目标是“提取”pMonitor字段。 pMonitor声明和MONITOR实现是否正确? C / C ++起源: SCREEN* EloGetScreenByIndex (int nScreenIndex); typedef struct SCREEN_TAG { int nScreenIndex; USHORT uVendorID; USHORT uProductID; USHORT uVersionNumber; wchar_t szDevicePath [MAX_PATH]; HANDLE hCalTouchThread; MONITOR* pMonitor; LPVOID pCWndBeamHandler; BOOL bIrBeams; } SCREEN; typedef struct MONITORS_TAG { int elo_mon_num; int x; int y; int width; int height; DWORD […]

涉及链接列表和结构的示例程序不起作用

所以,这是我教授提供的示例程序。 它的function是搜索树的链表并返回天气的结果,它发现用户输入的树。 但是,无论我输入什么,它总是返回false。 它出什么问题了? #include #include #define NL 20 typedef struct tree { char tree_name [NL]; struct tree* next; }tree; int checktree (tree *p, char name[]) { int found = 0; while (p != NULL) { if (strcmp(p -> tree_name, name) == 0) found = 1; p = p -> next; } return (found); } […]

将函数指针转换为void会产生什么影响?

所以我想第64次写一个缓冲库,我开始进入一些非常先进的东西。 以为我会就此问一些专业的意见。 在我的第一个头文件中,我有这个: typedef struct StdBuffer { void* address; } StdBuffer; extern void StdBufferClear(StdBuffer); 在#includes第一个头文件的另一个头文件中,我有这个: typedef struct CharBuffer { char* address; } CharBuffer; void (*CharBufferClear)(CharBuffer) = (void*) StdBufferClear; 声明此函数指针void是否会干扰调用? 它们具有值签名匹配。 我以前从未见过一个声明为void的函数指针,但它是让它干净地编译的唯一方法。 Stackwise它不应该与我在汇编编码中学到的东西有任何区别。 毫无意义的 OMG! 我刚才在StackOverflow上说Stackwise! 嗯..看起来我在这里假设太多了。 请允许我重新说明我是否可以。 我不关心地址中存储的“类型”数据。 我所关心的只是一个“单位”的大小以及该地址有多少单位。 如果您愿意,请查看API的接口协议合同: typedef struct StdBuffer { size_t width; ///< The number of bytes that complete a data […]

我如何在函数内malloc结构数组? 代码另有工作

我正在尝试创建一个创建可变大小的2D funct数组的函数。 我正在使用以下代码,它似乎可以自行工作: typedef struct { //Starter Properties int TypeB; int TypeF; int TypeW; //Randomized Properties int RandB; int RandF; int RandW; //Derived Properties int Speed; } MapTileData; MapTileData **Map; int i, x=5, y=5; //Allocate Initial Space Map = (MapTileData**)calloc(x, sizeof(MapTileData)); for(i = 0; i < x; i++) { Map[i] = (MapTileData*)calloc(y, sizeof(MapTileData)); } 所以上面的代码创建了一个2D结构数组。 […]

fread into struct错误地读取数据

我试图将位图(.bmp)图像标题读入c中的struct 。 typedef unsigned short WORD; typedef unsigned long DWORD; typedef struct _BITMAPFILEHEADER { WORD Type; DWORD Size; WORD Reserved1; WORD Reserved2; DWORD OffBits; } BITMAPFILEHEADER; 我的代码读取位图文件 FILE *fp; BITMAPFILEHEADER header; fp = fopen(file,”rb”); if (fp == NULL) { printf(“cannot open file!\n”); return 1; } fread(&header, sizeof(BITMAPFILEHEADER), 1, fp); printf(“Type: %02x\n”, header.Type); printf(“Size: %04x\n”, header.Size); […]

C如何管理多个源文件之间的#include关系并创建正确的makefile

我再次重新编辑了我的问题,这次是最终的。 注意:该程序正在运行(感谢所有帮助)。 但我仍然对依赖/链接实际如何工作有些困惑。 具体来说,我想了解makefile编译和运行的过程。 (例如,编译器首先查看main.c,从第1行开始,它是main.h,进入main.h,从第1行开始,指向function1.h,依此类推。) 我的主要问题是:编译器/ makefile是否以向后的方式运行,即编译器到达最后一站(不再有链接),它开始以递归方式收集内容并将其放入目标文件中。 如果我们有多个目标文件并且连接是交叉链接会发生什么? 每个目标文件应该相互独立吗? 以下是我的最终结果。 我知道它有很多部分,但我尽力将它们组织起来并添加说明。 FINALE MAKEFILE / DEPENDENCY 文件布局 primary模块:main.c补充模块:builder1.c,builder2.c builder1_function.c builder2_function.c头文件:main.h control.h builder1.h builder2.h builder1_function.h builder2_function.h builder1_shared.h builder2_shared.h 1)main.c使用来自builder1.c和builder2.c的每个主要函数 2)builder1_function,builder2_function存储builder1.c和builder2.c中主要function使用的子function 3)builder1有一组刚刚使用的新结构,builder2有另一组刚刚使用的新结构。 这些结构在builder1_shared.h和builder2_shared.h中声明。 4)函数原型在builder1.h builder2.h main.h中声明 5)main.c,builder1.c,builder2.c共享一些常量,并且都使用标准库。 这些常量在control.h中声明 6)control.h: 声明系统范围的常量 HEADER DEPENDENCY main.c: *include* main.h and *define* system-wide constants (declared in control.h) main.h: *include* all std libs, *include* […]