[英]dynamic char pointers array to strings
in this code I want to get a number of friends and then get the names i want the strings will be allocated dynamically with the lengh of the user input i have used with 2 functions: 在这段代码中,我想得到很多朋友,然后得到我想要的名称,这些字符串将随着我使用过的两个函数的用户输入的长度而动态分配:
void getNum(char** names, int* num)
{
//somecode
names = (char*)malloc(*num * sizeof(char));
//check
}
void getNames(char** names, int* num)
{
int i = 0;
int len = 0;
char name[LEN] = { 0 };
getchar(); //buffer cleaning
for (i = 0; i < *num; i++)
{
printf("enter #%d friend name: ", i+1);
myFgets(name, LEN); //getting name and cleaning "\n" at end
len = strlen(name)+1; // getting the size of string include "/0"
*(names + i) = (char*)malloc(len * sizeof(char));
if (*(names[i]) == NULL)
{
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
}
strncpy(*names, name, len);
}
}
the second dynamic allocation doens't work for me, overflow eror: "Access violation writing location". 第二个动态分配对我不起作用,溢出错误:“访问冲突写入位置”。 If the first allocation will be in the second function it will work fine.
如果第一个分配在第二个函数中,它将正常工作。 Can u explain that?
你能解释一下吗? and what I need to do for it will work in that way?
我需要做的将以这种方式起作用? thank you in advance...
先感谢您...
In function getNames
, you used the wrong pointer to check for NULL
, names[i]
is *(names+i)
, not the same as *(names[i])
, also, don't cast malloc
's return value. 在函数
getNames
,您使用了错误的指针来检查NULL
, names[i]
是*(names+i)
,与*(names[i])
,而且,请勿转换malloc
的返回值。 No need to use sizeof(char)
, it's always 1. 无需使用
sizeof(char)
,始终为1。
*(names + i) = (char*)malloc(len * sizeof(char));
if (*(names[i]) == NULL) // compare to the wrong pointer
{
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
}
strncpy(*names, name, len); // copy to the wrong buffer
Try the following: 请尝试以下操作:
names[i] = malloc(len);
if (names[i] == NULL)
{
printf("Error allocating memory!\n");
return 1; //return with failure
}
strncpy(names[i], name, len);
Also, in getNum
, to allocate an array for char pointers, use 另外,在
getNum
,为char指针分配一个数组,请使用
void getNum(char ***names, int *num) {
*names = malloc(*num * sizeof(char*));
}
You will call it by 你会打电话给
char **names;
getNum(&names, &num);
You could also return it by doing char **getNum(...)
. 您也可以通过执行
char **getNum(...)
将其返回。
Assuming the first function should allocate an array of pointers, and that the second should allocate individual char arrays to store the individual names, you are lacking an indirection level in first function: 假设第一个函数应该分配一个指针数组,而第二个函数应该分配单个char数组来存储各个名称,那么您在第一个函数中缺少一个间接级别:
char**
(C pass parameters by copy) char**
的副本(C参数通过副本传递) It should be: 它应该是:
char** getNum(int num) /* no need to pass num by reference */
{
char **names;
//somecode
names = malloc(num * sizeof(char*)); /* do not cast resul of malloc in C */
//check
return names
}
And in second function, you should consistenly allocate memory for names[i]
(or *(names + i)
), test it for NULL and copy the string there: 在第二个函数中,您应该始终为
names[i]
(或*(names + i)
)分配内存,测试它是否为NULL并在其中复制字符串:
names[i] = malloc(len * sizeof(char));
if (names[i] == NULL)
{
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
}
strncpy(names[i], name, len);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.