Tag: 指针

c中链表中的升序

我试图通过链接和地址而不是值来改变链表中的升序 struct node { char name[30]; int percent; struct node *link; }; int main { clrscr(); randomize(); struct node *st; st=NULL; for(int i=0;ipercent display(st); AscMarks(&st); //Changing the order of links and addresses to arrange them in ascending order printf(“\nAscending order list…\n”); display(st); getch(); return 0; } /*Adds a node at the end of a linked […]

为什么’c ‘编译而’c ‘没有?

此示例编译时没有警告/错误(gcc 4.8.2 -Wall): #include int main() { char c; int i; printf(“%p %02x\n”,&i,c[&i]); printf(“%p %02x\n”,&c,c[&c]); // important to note that this line DOESN’T compile: //printf(“%p %02x\n”,&i,c[i]); // which makes sense as c is NOT an array, it is a char. return 1; } 为什么语法c [&i]编译? 是故意还是意外? 语法c [&i]在语义上是否有效? (它有什么有用的意义吗?) 我的输出示例:(指针每次都改变) 0xbfb2577c b7718cea 0xbfb2577b 08 这个问题起源于这里有一个奇怪的代码’ch2 […]

使用指针或对struct的引用

我和我的朋友在我们的代码中使用结构(我们的代码彼此分开)。 让我们看看以下示例: struct Book { char title; int pages; } void setBook(struct Book *tempBook) { tempBook->title = “NiceTitle”; tempBook->pages = 50; } 上面的代码很简单。 问题是,拥有这两个主电源是否有任何区别: int main() { struct book obj1; setBook(&obj); } 要么 int main() { struct Book *obj2; setBook(obj2); } 编辑:我的发言中并不清楚。 我已经将品酒初始化为 struct Book *obj2 = malloc(sizeof(struct obj2));

指针和二进制搜索树

我正在开发一个使用二叉搜索树的程序(作为练习)。 我的问题是,当我尝试添加一个客户时 (在我的代码行65-69中间)我得到一个BS_node未声明的错误,虽然我在此函数中插入了struct BST_node * root ..我的部分代码如下,只是为了让读者更容易阅读,如果要求我可以上传完整的代码! 谢谢! #include #include #include #define MAX_STRING 50 void flush(); struct customer { char *name; char *address; char *email; }; struct double_linked_list { struct customer *data; struct double_linked_list *previous; struct double_linked_list *next; }; struct double_linked_list *customers_head=0; struct BST_node { struct double_linked_list *data; struct BST_node *left; struct BST_node *right; }; […]

如何连接指针数组?

可能重复: 在C中连接char数组 如何连接: char *a=”abcd”; char *b=”1234″; 使用strcat()? 当用户输入* a和* b时,回答相同的问题? EDIT错过了这个:没有使用另一个arrays。

c – 尝试编写修改后的结构时出现分段错误

这是一个旨在使用ppm图像文件的程序。 我在尝试修改结构然后将其写入新文件时遇到编译错误。 这是全局结构(在ppmIO.c和ppmIO.h中声明): ppmIO.c: struct Image *instance; ppmIO.h: struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance; 这是我的imageManip.h文件: #include void ImageInvert(struct Image **toInvert); void ImageSwap(struct Image **toSwap); 这些是我的imageManip.c文件的相关部分: #include #include #include #include #include void ImageInvert(struct Image **toInvert) { int i; int pix = (*toInvert->width) * (*toInvert->height); *toInvert = realloc(*toInvert, […]

c / c ++指向数组的指针vs指向指针的指针

我认为数组和指针基本上是一回事,直到我运行这个程序: int main() { int* a = new int(19); int b[1]; b[0] = 19; printf(“*a: %d\na: %p\n &a:%p\n”, *a, a, &a); printf(“*b: %d\nb: %p\n &b:%p\n”, *b, b, &b); delete a; } 输出是: *a: 19 a: 0x7f94524000e0 &a:0x7fff51b71ab8 *b: 19 b: 0x7fff51b71ab4 &b:0x7fff51b71ab4 有人可以解释为什么&b的输出与b相同? 谢谢! -Erben

指针指针的内存泄漏

我在这个网页上使用Dijkstra算法和邻接列表。 由于我想多次构建不同的图形并计算最短路径,因此我添加了一些“自由”命令来释放内存。 但是,在多次迭代后,内存使用量仍会增加 这是我修改过的代码: // C / C++ program for Dijkstra’s shortest path algorithm for adjacency // list representation of graph #include #include #include // A structure to represent a node in adjacency list struct AdjListNode { int dest; int weight; struct AdjListNode* next; }; // A structure to represent an adjacency liat struct AdjList […]

将指针从function传递到function

我对理解指针有点新意,所以我希望有人可以检查一下,以帮助我确保我对指针的工作原理有了正确的认识。 我正在尝试简化指针是什么以及如何使用它们的想法 functA(int *numb){ functB(numb); } functB(int *numb){ functC(numb); } functC(int *numb){ *numb+=1; } int main (){ int testNumber = 0; functA(&testNumber); . . . } 所以在main中,我发送testNumber的地址到functionA.所以当我将这个地址发送到functA时,没有问题,对吗? functionA有一个可以保存地址的指针参数,当我发送地址时它不会遇到问题,对吧? 然后,由于麻木(在functA中)是指针,我可以将它发送到functB,因为functB可以接受一个地址。 另外,我仍然发送testNumber所在的地址,对吗? 再次,我发送麻木(从functB)到functC和functC,我正在derefencing这个位置(有更好的方式说这个?)并将计数增加1.所以当这样做时,testNumber应该是1,正确? 我知道这可能听起来很愚蠢,但我只是想从概念上把握这个。 我花了更多的时间,而不是我愿意承认试图在我刚刚提交的项目中实现这个概念(我做了,但是花了太长时间)

(gcc)用于无警告编译的多点arrays或双指针

我有一个函数,有时会调用常规的,有时是动态的数组。 如果我将函数定义为 function_name(int[10][10] a) 并发送int **作为参数,我得到一个警告。 相反,如果我宣布 function_name(int** a) 并发送int [] []作为参数(在转换后)我无法访问函数内部的数组元素。 最正确的方法是什么?