Tag: 指针

使用指针

书中的C程序: #include int *addition ( int a , int b) { int c = a + b; int *d = &c; printf(“%d %d, “,c, &c); printf(“%d %d, “,d, *d); return d ; } int main (void) { int result = *(addition(1,2)); int *result_ptr = addition(1,2); //// printf(“result_ptr = %d\n” , *result_ptr ) ; printf(“result = […]

这个C(strcpy)代码有什么问题?

int main() { char *w; strcpy(w, “Hello Word”); printf(“%s\n”, w); return 0; } 在上面的代码中使用char指针的方式有什么问题?

c circular double linked-list:rev traverse为同一节点提供不同的列表指针地址

相关post1: c循环双链表list_node – 删除后第一遍遍历删除节点 相关post2: c循环双链表:遍历结尾节点的fwd / rev给出不同的指针地址 在使用循环双链表时,我在stackoverflow的帮助下创建了一个delete_node函数,该函数使用列表上的正向或反向迭代来到达要删除的节点。 该函数将链接列表的地址作为参数来实现删除(而不是对列表的指针引用)。 正向和反向迭代的混合仅仅是为了有效地防止遍历整个列表以在向前方向迭代时或在反向迭代时开始到达接近结束的节点。 full source: http://www.3111skyline.com/dl/dev/prg/src/testlld.c.txt compile with: gcc -Wall -o tlld testlld.com void delete_node (rec **list, int node) { // test that list exists if (!*list) { fprintf (stdout,”%s(), The list is empty\n”,__func__); return; } // get size of list int szlist = getszlist (*list); // […]

将char数组转换为字符串

我有一个char数组,我试图将其转换为指向字符串的char指针。 我相信这涉及获取指向char数组的第一个元素的指针,并在char数组的末尾添加一个空字符。 这样做的原因是我试图将它传递给Pebble智能手表的SimpleMenuItem ,其中.title需要获取一个char* ,指向一个字符串。 虽然我已经能够填充char数组并且(我认为)添加了null字符并获得了指针,但我无法在我的卵石上看到标题。 我不确定这是一个卵石问题还是我的C理解问题,但我觉得它可能是前者。 卵石代码(C): void in_received_handler(DictionaryIterator *received, void *context) { dataReceived = dict_read_first(received); APP_LOG(APP_LOG_LEVEL_DEBUG, “read first”); while (dataReceived != NULL){ if (dataReceived->key == 0x20) { //original is of the format “# random string”, ie “4 test name” char* original = dataReceived->value->cstring; APP_LOG(APP_LOG_LEVEL_DEBUG, original); char originalArray[strlen(original)]; //copy over to originalArray for (unsigned […]

将char *拆分为char * Array

我正在尝试将char *拆分为C中的char *数组。我习惯用Java / PHP OO编程。 我知道在这些语言中有几种简单的方法,但在C语言中……我完全迷失了。 我经常有几个小时的段错误x) 我正在使用TinyXML并从XML文件中获取信息。 这是我们找到数组的结构。 const int MAX_GATES = 64; typedef struct { char *name; char *firstname; char *date; char *id; char *gates[MAX_GATES]; } UserInfos; 这是我填充此结构的地方: UserInfos * infos = (UserInfos*)malloc(1024); infos->firstname = (char*)malloc(256); infos->name = (char*)malloc(128); infos->id = (char*)malloc(128); infos->date = (char*)malloc(128); sprintf(infos->firstname, “%s”, card->FirstChild(“firstname”)->FirstChild()->Value()); sprintf(infos->name, “%s”, card->FirstChild(“name”)->FirstChild()->Value()); sprintf(infos->date, […]

使用带指针的2 D数组的4种不同方法,这是对的吗? 解释会有很大帮助

下面的四个程序输入一个二维数组,然后打印出来。 第一个打印出垃圾值,并发出一些警告(我不明白)。 第二个工作正常,因为我相信二维数组线性存储在内存中,它也有意义。 第3个工作正常,但我不知道为什么它的工作原理。 第四个也适用。 因此,如果有人能够解释每种方法的运作方式,那将会很有帮助 我担心我对指针如何工作的理解并不像我想象的那么好。 int main(){ int n; int a [3][4]; int i=0,j=0,count=0; for(i=0;i<3;i++){ for(j=0;j<4;j++){ scanf("%d",(a+4*i+j)); // Output-514623632 514623648 514623664 514623680 514623696 514623712 514623728 514623744 514623760 514623776 514623792 514623808 } } for(i=0;i<3;i++){ for(j=0;j<4;j++){ printf("%d\t",*(a+4*i+j)); } } return 0; } Warnings -solution.c:15:21: warning: format '%d' expects argument of type 'int *', but argument […]

* list和** list之间的区别

如果我有一个结构: typedef struct A { char c[100]; }A; 然后我创建一个 sizeOfA = 5000; A *list = (A*) malloc(sizeOfA * sizeof(A)); list[i]是指向结构的指针吗? 或者,如果我想要一个指向结构的指针,我应该这样做 A **list = (A**) malloc (sizeOfA * sizeof(A*); [编辑] 现在让我们说我使用A *list创建了A *list (我已经这样做了)。 我如何创建5000个指针并使它们指向列表中的元素? p0 -> list[0] p1 -> list[1] .. .. p[n] -> list[n] 来回几次后,我注意到对于指针的排序有很大帮助。 公平地说,我将上面的编辑作为一个单独的问题发布。

使用c中的指针将2d数组传递给函数

这是我第一次不得不在这里问一个问题通常不需要,但这次我只是想不出来。 基本上我正试图在控制台中创建1和0(随机位置的1和0)的网格。 1s是木屑,0s代表空白区域。 要做到这一点,我必须使用此函数初始化和填充二维数组并返回指向已完成数组的指针 int* createEnvironment(int width, int height, int numWoodChips) 然后我需要使用此function打印2d数组 void printEvrionment(int* environment, int width, int height) 这就是我到目前为止所拥有的 int enviroWidth = 20; int enviroHeight = 20; int* createEnvironment(int width, int height, int numWoodChips); void printEvrionment(int* environment, int width, int height); void freeArray(int width, int* environment); int main(int argc, char **argv) { int *arr […]

C中的二维数组指针访问分段故障

我正在尝试用C语言编写Conway的生命游戏代码。我有一些包含我的宇宙的2D数组的问题。 我将向您展示一些导致我出现问题的代码。 实际上有一个我无法处理的问题。 #include #include #include “file2ppm.h” typedef enum { false, true } bool; void *allocateMemoryFor2DArray(int x, int y); //allocate memory for 2D array void initializeUniverseFromFile(char* path, int x, int y, int array[x][y]); //set values of cells from a file void initializeUniverseRandomly(int x, int y, int array[x][y]); //set random values int getNumberOfColumns (FILE *file); //get […]

为另一个结构内的结构数组分配内存

我真的不知道这里的问题是什么。 编译器说它没关系,但是当我运行它时,可执行文件崩溃了(我相信这是一个分段错误问题)。 他们说两个人总是比一个人好,所以我希望你们能找到我错过的任何东西。 好的,所以就是这样:我有2个结构: struct _Color { unsigned char r,g,b; char *pchars; }; //typedef to Color in header file struct _XPM { unsigned int width; unsigned int height; unsigned char cpp; unsigned int ncolors; Color *colta; //collor table unsigned int *data[]; }; //typedef to XPM in header file 然后,在一个函数中,我为XPM结构分配内存,然后为XPM结构中的Color数组分配内存(我不会在这里编写故障安全代码): XPM *imagine; //received as a parameter […]