我想传递指向指向函数的指针的指针,在函数中分配内存,用字符串填充它并将其取回,但似乎都没有工作。程序不在功能外打印。有代码的最重要的部分:使用指向字符数组的指针
struct record ** getRegEx(int *counter, char** keys)
{
*counter = 0;
//get some records, its number is *counter, max lenght of each string is 64
//COUNTER IS NOT 0! ITS VALUE DEPENDS ON OTHER OPERATIONS I HAVENT WRTTEN HERE
//...
keys =(char **) malloc((*counter)*(sizeof(char *)));
for (j = 0; j < *counter; j++)
{
keys[j] = (char*)malloc(64*sizeof(char));
}
strcpy(keys[j],key.dptr);
printf("size %d : \n", sizeof(**keys));//1
printf("size %d : \n", sizeof(*keys));//4
printf("size %d : \n", sizeof(keys[0]));//4
printf("size %d : \n", sizeof(keys));//4
//...
}
/*Out of the function, inside the function OK*/
char** keys;
int count;
results = getRegEx(&count, &keys); //&keys or keys - makes no difference
for(int k=0 ; k< *count;k++) //test
{
printf("keys in db %s: s\n", keys[k]); //nothing!?
}
我做它的工作方式与一些替代函数头像struct record ** getRegEx(int *counter, char*** keys)
(使用*键和*键的[I],而不是键和键[I]里面的功能)。感谢所有!
不,他将垃圾传递给'malloc'。 – 2011-04-25 21:03:15
那么,他设置*计数器为零,然后通过((*计数器)*(sizeof(char *)))到malloc,即0。 – 2011-04-25 21:06:04
我的坏,我错过了那条线。 – 2011-04-25 21:09:25