Tag: 指针

指针未在c中的insert中修改

优先级队列的程序,我对指针有疑问,我正在通过头插入但是它没有在插入中修改,因为你可以看到我在插入中打印头和插入中的主要是打印非零的东西在头部它是零 #include #include struct node{ int data; int priority; struct node* next; }; struct node* getnewnode(int data,int priority){ struct node* newnode=malloc(sizeof(struct node)); newnode->data=data; newnode->next=NULL; newnode->priority=priority; return newnode; } void insert(struct node* head,int data,int priority){ struct node* newnode=getnewnode(data,priority); if(head==NULL){ head=newnode; printf(“head in insert is %d”,head); return; } if(head->priority > newnode->priority){ newnode->next=head; head=newnode; return; } if(head->priority priority […]

便携式标记指针

有没有一种可移植的方法来实现C / C ++中的标记指针,就像一些在平台和编译器中工作的文档化宏? 或者当你标记你的指针时,你处于危险之中? 如果存在这样的辅助函数/宏,它们是任何标准的一部分还是仅作为开源库提供? 对于那些不知道标记指针但感兴趣的人来说,这是一种在普通指针中存储一些额外数据的方法,因为在大多数架构中,指针中的一些位总是0或1,所以你保留你的标志/类型/提示这些额外的位,并在您想要使用指针取消引用某些实际值之前删除它们。 const int gc_flag = 1; const int flag_mask = 7; // aka 0b00000000000111, because on some theoretical CPU under some arbitrary OS compiled with some random compiler and using some particular malloc last three bits are always zero in pointers. struct value { void *data; }; struct value […]

为什么读取结构指针字段无效?

在Valgrind中运行该程序,它表示在结构的转换指针处存在“无效读取大小为8”。 它与calloc有关吗? 如果按原样读取它是(零)。 有一个结构(称为trie),它使用如下: #include #include #include #include const int MAX_SIZE = 20; struct _trie { int maxNode; int nextNode; int** transition; char* fin; }; typedef struct _trie * Trie; Trie createTrie (int maxNode){ Trie trie; trie = (Trie) malloc(sizeof(Trie)); printf(“size of trie: %lu, size of the struct: %lu, size of _trie: %lu\n”,sizeof(trie),sizeof(Trie), sizeof(struct _trie)); […]

c和objective-c – const char *和char *

我有一个function: -(void)ADPCMDecode:(char *)indata : (short *)outdata :(long)len { indata是一个char,函数做指针算术迭代一段len,修改outdata,这是一个简短的,我需要做指针运算来从中获取值。 我试图使用以下方法调用该函数: const char *modulatedBytes1 = [modulatedAudio bytes]; char *modulatedBytes [] = modulatedBytes1; unsigned int moduleatedLength = [modulatedAudio length]; short *decompressedBytes = NULL; [self ADPCMDecode:modulatedBytes :decompressedBytes :moduleatedLength]; DLog(@”%hi”,decompressedBytes[1]); 我在这一行得到了一个BAD ACCESS错误: *outp++ = valprev; 在函数内,因为我传递一个constant char *而不是char * 我应该如何调用该函数,以及如何从中获取输出? 我没有C的背景,这就是为什么我不明白如何去做这个。 以下是同一问题的C唯一版本: https : //pastee.org/d3y3z

无法理解如何从C中的函数返回字符串

void add() { char name[50], surname[50], usern[50]; int id, birth, amount; printf(“Enter your name, surname and your year of birth:\n”); scanf(“%s %s %d”, &name, &surname, &birth); printf(“Enter your ID, username and your total amount:\n”); scanf(“%d %s %d”, &id, &usern, &amount); const char *pass1=function(usern); } const char *function (char usern[50]) { char temp; int i=0; int […]

txt文件中的第一个字符不能用C打印

我是C的初学者。我正在用c做一个简单的游戏。 我有一个.txt文件存储玩家的分数,如 gse 12 CKY 8 然后我在C中有这个function,该function根据上面的.txt文件输出得分。 int n = 0; int c; int p=0; char name[NAME_LENGTH] = { 0 }; char score[NAME_LENGTH] = { 0 }; FILE *fp = fopen(“scoreBoard.txt”, “r”); if (fp == NULL) { printf(“no score available\n”); fflush(stdin); getchar(); return; } system(“cls”);//clears the screen if (fp){ while((c=getc(fp)!=EOF)){ if(n%2==0){ fgets(name,NAME_LENGTH,fp); printf(“%d “,n/2+1); //index […]

内存布局:2D N * M数据作为指向N * M缓冲区的指针或作为指向数组的N指针数组

我对如何组织2D数据的内存布局犹豫不决。 基本上,我想要的是N * M 2D doublearrays,其中N~M是数千(并且来自用户提供的数据) 我看到它的方式,我有两个选择: double *data = new double[N*M]; 要么 double **data = new double*[N]; for (size_t i = 0; i < N; ++i) data[i] = new double[M]; 第一个选择是我倾向于。 我看到的主要优点是更短的新/删除语法,如果我正确安排访问,连续内存布局意味着在运行时相邻内存访问,并且可能更好地实现矢量化代码(自动矢量化或使用矢量库,如vDSP或vecLib) 另一方面,在我看来,与分配一堆较小的连续内存相比,分配大量连续内存可能会失败/花费更多时间。 并且第二种方法还具有与data[i*M+j]相比较短的语法data[i][j]的优点 最常见/更好的方法是什么,主要是如果我尝试从性能角度来看它(尽管那些将是小改进,我很好奇看哪哪个会更好)。

处理C中的指针参数

我正在使用一个具有以下签名function的库: void LED_stop_blink_task ( void * callback_parameter ); void指针所代表的实际参数是指向uint32_t的指针,uint32_t是电路板上led的编号。 有没有办法调用此函数而不使用变量来保存数据? 在我的想象中,它会像 LED_stop_blink_task(&35); 或唯一的方法是这样的: uint32_t led_num = 35; LED_stop_blink_task(&led_num); 如果你问我为什么要抛弃变量,那么,我只是好奇它是否可能……

为什么我没有得到SIGSEGV?

我想了解这里发生了什么,更准确地说,为什么我在写入内存位置时没有收到分段错误,根据我的理解,没有分配。 假设我想定义一个int的二维数组( testptr )。 一维( 4 )静态分配(作为“数组”),第二维( 2 )动态分配(作为“指针”)。 // First dimension 4 rows static int *testptr[4]; for (i=0; i<4; i++) testptr[i] = calloc(2, sizeof(int)); // Second dimension 2 columns "dynamically" (in this example it is really just a constant) 现在我写信给一些地方: testptr[0][0] = 5; testptr[1][0] = 6; testptr[2][1] = 7; testptr[3][1] = 7; 以上所有我希望工作正常,因为它们在4×2“arrays”内。 现在我写一个不应该分配的位置: […]

malloc和free动态变化的结构

我在动态变化的结构中移动指针时遇到了麻烦。 我已经创建了我的代码,你可以在malloc中获得更多的内存,这看起来很有效。 我遇到的问题是如何添加到结构,如何释放内存以及如何从结构移动到结构并打印所有项目。 我正在尝试测试添加和打印(删除function,似乎没有工作,segfaults) 当我添加到结构然后打印结构时,我从我添加的值中获得了一个段错误。 我不知道我是否正确地从第一个结构转移到下一个结构。 #include #include #include “pointer.h” /******************************************** Creates more memory for size (strut * rec+1) *********************************************/ employee *create(int record){ employee *new_employee = malloc(sizeof(employee) * (record+1)); return new_employee; } /******************************************** Copies the data from one structure to a new structure with size “structure” multipled by rec+1 ***********************************************/ employee *copy(employee *data, int record){ […]