我想傳遞指向指向函數的指針的指針,在函數中分配內存,用字符串填充它並將其取回,但似乎都沒有工作。程序不在功能外打印。有代碼的最重要的部分:使用指向字符數組的指針
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