更改函数指针的地址值

我在C中有以下代码: int addInt(int n, int m) { return n + m; } int (*functionPtr)(int, int); functionPtr = &addInt; functionPtr是一个函数指针,它指向函数addInt的特定地址。 我想改变它的1位值,但我无法弄清楚如何。 假设functionPtr在最后一个语句之后指向0xABC0 (假设一个16位地址)。 我想将其值更改为0xABC1 。 我试图将值与0x1进行OR运算,但我猜操作数转换有问题: functionPtr = &addInt | 0x00000001; // addresses are of 32 bits 我知道乱搞指针是有风险的,但我必须更改地址的LSB才能进入ARM Cortex-M4 MCU的Thumb状态。

.BMP文件无法打开

我正在尝试创建一个bmp文件。 该文件已创建,我可以打开它,但它说,该文件已损坏,无法正常打开。 这是我如何保存文件: void createBMP(char* pixelData, long xRes, long yRes){ BITMAPFILEHEADER fheader; BITMAPINFOHEADER iheader; COLORREF_RGB rgb; //file header werte setzen fheader.bfType = 0x4D42; fheader.bfSize = sizeof(BITMAPFILEHEADER); fheader.bfReserved1 =0; fheader.bfReserved2=0; fheader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); //info header werte setzen iheader.biSize = sizeof(BITMAPINFOHEADER); iheader.biWidth = xRes; iheader.biHeight = yRes; iheader.biPlanes = 1; //farbtiefe iheader.biBitCount = 24; iheader.biCompression = 0; […]

避免多类型结构的memset

我想避免在这样的结构上使用memset() : typedef struct { int size; float param1; } StructA; typedef struct { StructA a; unsigned int state; float param2; } Object; 我可以做这样的事情(伪代码,我现在无法检查)? Object obj; int *adr = (int*)&obj; for (int i; i < sizeof(obj); i++) { *adr++ = 0; } 它会将obj的每个成员设置为零吗? 编辑 :回答有关评论的问题。 我一直在研究一些情况(使用uni-type结构),其中memset比手动初始化慢两倍。 所以我会考虑尝试初始化多类型结构。 避免使用memcpy()也会很好(避免使用 lib)。

递归反向链表

我试图使用指针指针递归地执行反向链表,但问题是在第二个循环中脚本崩溃。 你能帮助我解决我的问题吗? 这是我的代码: void reverseNumber(Mynbr** start){ Mynbr *header; Mynbr *current; if ((*start)){ header = (*start); current = (*start)->next; if (current && current->next!= NULL) { reverseNumber(current->next); header = current; current->next->next = current; current->next = NULL; } } }

C空字符导致程序行为问题

我遇到的问题是这个程序是菜单驱动的。 当我输入字母时,“i”被输入到input数组中,其大小为MAX_LENGTH+1 。 通过GDB,我发现input数组的第0个索引输入“i”,其余空格输入NULL_CHAR字符。 无论如何,当我按下“i”作为插入菜单时,我向一个字段打招呼,告诉我输入一个值。 我输入任何整数并按inter。 它没有受到“命令?:”字段的欢迎,而是给我一个输入条目的机会,它立即告诉我输入无效并告诉我再次输入命令。 这就是我的意思: Commands are I (insert), D (delete), S (search by name), V (search by value), P (print), Q (quit). Command?: i 45 Command?: Invalid command. Commands are I (insert), D (delete), S (search by name), V (search by value), P (print), Q (quit). Command?: 我发现发生这种情况的原因是因为当再次调用safegets函数时,由于某种原因,函数safegets中的局部变量c的值为NULL_CHAR ,可能是因为输入char数组中的所有其他值将所有其他条目作为NULL_CHAR 。 我不明白为什么c会自动分配NULL_CHAR的值,因为在while循环中,因为有一个语句c […]

如何循环getprotent()函数

我相信getprotoent()函数从/etc/protocols获取值。 我做了以下示例程序:test.c. 表明 proto.p_name = ip * proto.p_aliases = IP proto.p_proto = 0 这些值与/etc/protocols的第一个条目相同。 我想知道如何获取第二个条目的值,如循环,评估一些指针,不使用getprotobyname()或getprotobynumber() 。 TEST.C #include #include #include int main(int argc,char *argv[]) { protEnt(); } int protEnt() { setprotoent(2); struct protoent proto; proto = *getprotoent(); printf(“proto.p_name = %s\n”,proto.p_name); printf(“*proto.p_aliases = %s\n”,*proto.p_aliases); printf(“proto.p_proto = %d\n”,proto.p_proto); proto = *getprotoent(); endprotoent(); }

将此代码行转换为C.

我有以下代码行: for ( int i = index; i size; ++i ) //i,index and size are integers.al is an arraylist 当我在C中编译它时,我得到错误: ‘for’ loop initial declarations are only allowed in C99 mode 我不知道如何解决这个问题。 谢谢!

在c中声明可变长度的字符串 – 显然是动态的?

我试图理解一些关于如何在c中定义“字符串”的基本知识。 char s[2]; scanf(“%s”, s); printf(“%s”, s); printf(“sizeof s %d”, sizeof(s)); 我不是程序员。 我知道使用scanf获取用户输入存在问题,我甚至没有检查其返回值等。这只是为了理解声明字符串的基本内容。 鉴于上面的代码,如果我输入’helloworld’,它的作用是print’helloworld’。 好。 但我想通过说char s [2]我说的是’s是一个长度为2的数组,其中每个元素都是char’类型。 因此,我期待看到’他’打印。 不是’helloworld’。 因为我的arrays只有2个字符的空间。 sizeof仍然返回2.但看起来我的数组已经增长到用户输入的大小。 怎么了?

printf()中的任意数量的参数。 在C编程中这叫什么?

当我们使用不同数量的参数的printf()时 printf(“Hello”); printf(“%d”,a); printf(“%d%d”,b,c); 为什么这不称为“重载”(使用相同的方法执行不同的任务)? 如果它是重载,那么为什么C不被认为是面向对象的编程语言?

在GCC中编译C文件时出错(多个文件合二为一)

该程序certificate了一串文本。 这是我的代码(main是justify.c): justify.c #include #include “line.h” #include “word.h” #define MAX_WORD_LEN 20 int main(void) { char word[MAX_WORD_LEN+2]; int word_len; clear_line(); for(;;) { read_word(word, MAX_WORD_LEN+1); word_len = strlen(word); if(word_len ==0) { flush_line(); return 0; } if (word_len >MAX_WORD_LEN) word[MAX_WORD_LEN]=’*’; if(word_len + 1 > space_remainding()) { write_line(); clear_line(); } add_word(word); } } line.c #include #include #include “line.h” #define […]