附加列表中指针的指针追加

我通常在python中编程。 为了提高我的模拟性能,我正在学习C.在向链表实现追加函数时,我有一个问题需要理解指针指针的使用。 这是我的书(由Kanetkar理解C指针)中的代码的摘录。 #include #include struct node{ int data; struct node *link; }; int main(){ struct node *p; //pointer to node structure p = NULL; //linked list is empty append( &p,1); return 0; } append( struct node **q, int num){ struct node *temp, *r; //two pointers to struct node temp = *q; if(*q == NULL){ […]

为什么C中的字符串需要以null结尾?

只是想知道为什么会这样。 我渴望了解更多有关低级语言的知识,而且我只是进入C的基础知识,这已经让我感到困惑。 像PHP这样的语言会在解释和/或解析时自动为null终止字符串吗?

传递2D数组结构

我从数组中处理卡片类型结构。 struct card deck[DECKSIZE]; //The deck is an array of cards structures 我正在使用2Darrays。 一组卡片类型结构的数组 struct card allHands[hands][cards]; 我使用此函数将deck和数组作为指针传递给数组的参数。 我还改变了甲板指针的位置,以模拟在传递给玩家时丢失牌的牌组。 void dealHands(struct card *deck, struct card **handArray, int hands, int cards){ int players; int cardCount; int passCard = 0; struct card * thisDeck; thisDeck = deck; for(players = 0; players < hands; players++){ for(cardCount = 0; […]

为什么’fopen’返回NULL指针?

我正在使用C编程语言编写一个简单的文件分割器/合并程序。 问题是,由于某种原因, fopen返回NULL,因此,我的程序在fwrite语句中崩溃。 我该如何解决? 这是C文件: int SplitFile(char* filename, char* output, size_t size) { char current_file_name[256]; int file_count = 0, i = 0; FILE *file = fopen( filename, “rb” ); printf(“split %s into chunks of %d named\n”, filename, size); if (!file) return E_BAD_SOURCE; else { output = (char *) malloc(size * sizeof(char)); if (output == NULL) […]

C函数const多维数组参数中的奇怪警告

我对这段代码有一些奇怪的警告: typedef double mat4[4][4]; void mprod4(mat4 r, const mat4 a, const mat4 b) { /* yes, function is empty */ } int main() { mat4 mr, ma, mb; mprod4(mr, ma, mb); } gcc输出如下: $ gcc -o test test.c test.c: In function ‘main’: test.c:13: warning: passing argument 2 of ‘mprod4’ from incompatible pointer type test.c:4: note: […]

访问超出C和C ++限制的数组

int data[8]; data[9] = 1; c ++标准对此有何评论? 这是未定义的行为吗? 至少C编译器(gcc -std = c99 -pedantic -W -Wall)对此没有任何说明。 谢谢。

Cuda C – 链接器错误 – 未定义的引用

我很难编译一个只包含两个文件的简单cuda程序。 main.c看起来像这样: #include “my_cuda.h” int main(int argc, char** argv){ dummy_gpu(); } cuda.h看起来像这样: #ifndef MY_DUMMY #define MY_DUMMY void dummy_gpu(); #endif 并且my_cuda.cu文件像这样松散: #include #include “my_cuda.h” __global__ void dummy_gpu_kernel(){ //do something } void dummy_gpu(){ dummy_gpu_kernel<<>>(); } 但是,如果我编译我总是收到以下错误: gcc -I/usr/local/cuda/5.0.35/include/ -c main.c nvcc -c my_cuda.cu gcc -L/usr/local_rwth/sw/cuda/5.0.35/lib64 -lcuda -lcudart -o md.exe main.o my_cuda.o main.o: In function `main’: main.c:(.text+0x15): undefined […]

如何使用printf格式化unsigned long long int?

#include int main() { unsigned long long int num = 285212672; //FYI: fits in 29 bits int normalInt = 5; printf(“My number is %d bytes wide and its value is %ul. A normal number is %d.\n”, sizeof(num), num, normalInt); return 0; } 输出: My number is 8 bytes wide and its value is 285212672l. A […]

在x86和x64上读取同一页面内的缓冲区末尾是否安全?

如果允许在输入缓冲区末尾读取少量数据,那么在高性能算法中发现的许多方法都可以(并且被简化)。 这里,“少量”通常意味着超过结尾的W – 1个字节,其中W是算法的字节大小(例如,对于处理64位块中的输入的算法,最多7个字节)。 很明显, 写入输入缓冲区的末尾通常是不安全的,因为您可能会破坏缓冲区1之外的数据。 同样清楚的是,将缓冲区的末尾读取到另一页面可能会触发分段错误/访问冲突,因为下一页可能不可读。 但是,在读取对齐值的特殊情况下,页面错误似乎是不可能的,至少在x86上是这样。 在该平台上,页面(以及因此内存保护标志)具有4K粒度(较大的页面,例如2MiB或1GiB,可能,但这些是4K的倍数),因此对齐的读取将仅访问与有效页面相同的页面中的字节缓冲区的一部分。 这是一个循环的规范示例,它对齐其输入并在缓冲区末尾读取最多7个字节: int processBytes(uint8_t *input, size_t size) { uint64_t *input64 = (uint64_t *)input, end64 = (uint64_t *)(input + size); int res; if (size = 0) { return input + res; } // align pointer to the next 8-byte boundary input64 = (ptrdiff_t)(input64 + 1) & ~0x7; for […]

scanf和scanf_s之间的区别

所以我想问一下这两者有什么区别。如果有的话。在大学里,我接受过教学,我正在使用scanf,但在我的个人电脑上,视觉工作室不断发出警告。 error C4996: ‘scanf’: This function or variable may be unsafe. Consider using scanf_s instead. 我必须将所有scanf更改为scanf_s,否则程序将无法构建。 (我从2013年开始使用ms visual studio的最新版本)