Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何分配、初始化和重新分配指向char的指针数组?_C_Arrays_String_Pointers_Malloc - Fatal编程技术网

如何分配、初始化和重新分配指向char的指针数组?

如何分配、初始化和重新分配指向char的指针数组?,c,arrays,string,pointers,malloc,C,Arrays,String,Pointers,Malloc,我一直在尝试制作一个搜索结果列表,如果它需要更多的空间,请重新定位它。 我的一个实现几乎可以正常工作,但我不知道如何调整数组大小以容纳更多字符串 原件: 字符*搜索结果[maxResults] 另一个函数将在找到命中时将每个searchResults[i]malloc到适当的大小。以下是几个搜索结果的外观: searchResults[0] = "Result" searchResults[1] = "AnotherResult" searchResults[2] = "Someresulthe

我一直在尝试制作一个搜索结果列表,如果它需要更多的空间,请重新定位它。 我的一个实现几乎可以正常工作,但我不知道如何调整数组大小以容纳更多字符串

原件: 字符*搜索结果[maxResults]

另一个函数将在找到命中时将每个searchResults[i]malloc到适当的大小。以下是几个搜索结果的外观:

searchResults[0] = "Result"
searchResults[1] = "AnotherResult"
searchResults[2] = "Someresulthere"
searchResults[3] = "TheFourthresult"
searchResults[4] = "YetAnotherOne"
在空间不足时尝试重新分配它,但失败后,我转向internet,显然您无法调整阵列的大小。幸运的是,我当时认为我应该这样做,一些网站说:

char **searchResults;

//initialize result list
searchResults = (char **) malloc( maxResults * sizeof(char *) ) ;
for (i = 0; i < maxResults; i++) {
searchResults[i] = NULL ;
而第一个例子是它的外观。我在想我做错了什么,但我不知道是什么

谢谢

编辑:如果你投了反对票,你至少可以礼貌地说出原因,这样我就可以澄清你的分歧点

不管怎么说,这张图片显示了这个问题。除了第一个元素,我似乎无法分配任何结果,我不知道为什么


要了解阵列满后如何增加其大小,请正确阅读
realloc
函数的工作原理。 我认为正确使用
realloc
应该可以解决您的问题

例如:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);

   return(0);
}
#包括
#包括
int main()
{
char*str;
/*初始内存分配*/
str=(char*)malloc(15);
strcpy(str,tutorialspoint);
printf(“字符串=%s,地址=%u\n”,str,str);
/*重新分配内存*/
str=(char*)realloc(str,25);
strcat(str,“.com”);
printf(“字符串=%s,地址=%u\n”,str,str);
自由基(str);
返回(0);
}

好吧,我删除了这个项目,重新制作了它,注释掉了为每个指针赋值的过程,它现在的工作方式和它现在应该的完全一样。问题是什么?不知道,我想是腐败的项目


另一方面,Visual Studio不会在调试器中显示使用char**searchResults方法存储的各种指针的值。

我想您可以详细说明一下。在回答中添加的示例Your
malloc
代码很好(除了不必要的强制转换和缺少错误检查)。显然你有问题。SSCCE可以让我们找到问题所在。说“它不起作用”从来都不是什么好事。
searchResults[i]=(char*)malloc(100)
so,
strcpy(searchResults[0],“DDDDDDDDDD”)
(或使用strncpy)而不是
搜索结果[0]=“dddddddd”
#include <stdio.h>
#include <stdlib.h>

int main()
{
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   free(str);

   return(0);
}