C/C中带字符串的双指针指向带malloc的数组
有人能解释一下为什么我的输出中缺少第一个指针stringarray[0]以及如何解决它吗。我还想知道如何释放所有malloc指针的内存C/C中带字符串的双指针指向带malloc的数组,c,arrays,string,pointers,free,C,Arrays,String,Pointers,Free,有人能解释一下为什么我的输出中缺少第一个指针stringarray[0]以及如何解决它吗。我还想知道如何释放所有malloc指针的内存 int main(int argc, char **argv) { [...] char ** stringarray; if (( stringarray = (char **)malloc(counter*sizeof(char)))== NULL){exit(0);} int k = 0; for (i = 0; i < len; i =i+1
int main(int argc, char **argv) {
[...]
char ** stringarray;
if (( stringarray = (char **)malloc(counter*sizeof(char)))== NULL){exit(0);}
int k = 0;
for (i = 0; i < len; i =i+1){
if((strcmp(countries, districts[i]) == 0) && ( argvIntPeoplelimit <= people[i])) {
if (( stringarray[k] = (char * ) malloc(100*sizeof(char)))== NULL){exit(0);}
snprintf(stringarray[k],100,"The Country %s has %d people.",countries[i],people[i]);
printf("%d %d %s %s %d\n",i,k,stringarray[k], countires[i],people[i] ); //here stringarray[k] k==0 has a value
k=k+1;
}
}
write_file(stringarray,counter);
for (int f = 0; f < k; ++f)
{
// if i call stringarray[0] nothing shows up
//ERROR can't free(stringarray);
}
return 0;
}
非常感谢任何帮助、提示和提示!谢谢大家! 为什么要把事情弄得这么复杂 如果您的目的是将数据转储到文件中,请避免内存分配
for (i = 0; i < len; i =i+1){
if((strcmp(countries, districts[i]) == 0) && ( argvIntPeoplelimit <= people[i])) {
char country_inf[256] = {0};
int info_len;
info_len = snprintf(country_inf,sizeof(country_inf),"The Country %s has %d people.",countries[i],people[i]);
write_file(country_inf,info_len);
}
为什么要把事情弄得这么复杂 如果您的目的是将数据转储到文件中,请避免内存分配
for (i = 0; i < len; i =i+1){
if((strcmp(countries, districts[i]) == 0) && ( argvIntPeoplelimit <= people[i])) {
char country_inf[256] = {0};
int info_len;
info_len = snprintf(country_inf,sizeof(country_inf),"The Country %s has %d people.",countries[i],people[i]);
write_file(country_inf,info_len);
}
我相信这是为字符串数组分配内存的正确方法:
// number of pointers/strings; don't have to cast the result of malloc
char** stringArray = malloc(sizeof(char*) * 2);
// allocate however many chars for each pointer
for(i = 0; i < 2; ++i)
stringArray[i] = malloc(sizeof(char) * 10);
strcpy(stringArray[0], "Hello");
strcpy(stringArray[1], "world!");
printf("%s %s\n", stringArray[0], stringArray[1]);
要释放上述内容,您可以这样做:
for(i = 0; i < 2; ++i)
free(stringArray[i]);
free(stringArray);
我相信这是为字符串数组分配内存的正确方法:
// number of pointers/strings; don't have to cast the result of malloc
char** stringArray = malloc(sizeof(char*) * 2);
// allocate however many chars for each pointer
for(i = 0; i < 2; ++i)
stringArray[i] = malloc(sizeof(char) * 10);
strcpy(stringArray[0], "Hello");
strcpy(stringArray[1], "world!");
printf("%s %s\n", stringArray[0], stringArray[1]);
要释放上述内容,您可以这样做:
for(i = 0; i < 2; ++i)
free(stringArray[i]);
free(stringArray);
你能再帮我一次吗?让我们遗憾地说,我不能附加到文件中——只需在一次调用中写入所有数据——内存分配也是一项要求:关键是理解指针。int计数器告诉我在这里再次运行的strcmp函数中找到了多少个结果。。。在使用中等于k。可能是分配stringarray和stringarray[0]的地址相同的问题吗?您能再帮我一次吗?让我们遗憾地说,我不能附加到文件中——只需在一次调用中写入所有数据——内存分配也是一项要求:关键是理解指针。int计数器告诉我在这里再次运行的strcmp函数中找到了多少个结果。。。在使用中等于k。分配stringarray和stringarray[0]可能是同一地址的问题吗?能否分别向代码中添加它从snprintf获取的值?能否分别向代码中添加它从snprintf获取的值?