Hi, all:I want to record some memory pointer returned from malloc, is possiblethe code like below?int memo_index[10];int i,j;char *tmp;for (i=0;i<10;i++){tmp = malloc(10);memo_index[i] = tmp;}what I want to realize is "use the memo_index [10] to record eachusable memory pointer, then I just usememo_index[i] to refer to each stuff installed in the allocated memory/Is it possible or some else way?Thanks for any comments.bin 解决方案malloc will return a void pointer so type cast it and assign it tomemo_index.for (i=0;i<10;i++)memo_index[i]=(int *)malloc(10);type casting depends on the way you want to use pointer.No, no, no.malloc() returns a result of type void*, which will be implicitlyconverted to any pointer-to-object type. Casting the result isunnecessary, and can mask certain errors such as forgetting to"#include <stdlib.h>" or using a C++ compiler.--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. you want to record the address of a variable in memo_index so declare it as a pointer. int *memo_index[10]. malloc will return a void pointer so type cast it and assign it to memo_index. for (i=0;i<10;i++) memo_index[i]=(int *)malloc(10); type casting depends on the way you want to use pointer.since tmp is a char, it should really be:char *memo_index[10];That is, an array of 10 char pointers. Although pointers are all thesame size, don''t point to different typed data from different typedpointers unless you really need to. 这篇关于有可能构建一些指针数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 07:00