When I try to compile the following code I receive an error: "Type error in argument 1 to 'allocate'; found 'char * *', expected 'char *" at the line indicated (<<<<<). Explanations would be appreciated.
当我尝试编译以下代码时,我收到一个错误:“在参数1中键入错误'分配';在指示的行中找到'char * *',期望'char *”(<<<<<)。解释将不胜感激。
#include <stdio.h>
#include <string.h>
void allocate(char *dt);
int main(void)
{
char *data[3];
allocate(data); <<<<<
return 0;
}
void allocate(char *dt)
{
int i;
char buf[] = "A test string";
for (i = 0; i < 3; i++){
strcpy(&dt[i], buf);
printf("%s\n", dt[i]);
}
}
My understanding is that I should call allocate thus: allocate(&data) but with this I receive the following error: "Type error in argument 1 to 'allocate'; found 'char * (*)[3]', expected 'char *'".
我的理解是我应该这样调用allocate:allocate(&data)但是我得到以下错误:“在参数1中输入错误'分配';找到'char *(*)[3]',期望'char * “”。
It should be obvious that I am trying to make the contents of *data[] == buf.
很明显,我正在尝试制作* data [] == buf的内容。
3 个解决方案
#1
It looks like allocate is trying to allocate three strings dynamically and assign them to each member of an array of three char*
pointers.
看起来分配试图动态分配三个字符串并将它们分配给三个char *指针数组的每个成员。
Arrays decay to pointers when you pass them as function arguments which is what you want, so the declaration of allocate
needs to be void allocate(char**)
. Passing in data
will pass in a pointer to the first element of the array, i.e. a pointer to a char*
.
当你将它们作为你想要的函数参数传递时,数组会衰减为指针,因此allocate的声明需要为void allocate(char **)。传入数据将传入指向数组的第一个元素的指针,即指向char *的指针。
In allocate you will need to allocate some memory for the new strings. I'm presuming that as this is a test example you really do want separate copies of the strings for each member of the array.
在分配中,您需要为新字符串分配一些内存。我假设这是一个测试示例,你真的想要为数组的每个成员分别创建字符串副本。
Of course, at this point you will probably want a deallocate
function and make sure that this is always called to perform the corresponding free
for the new malloc
s.
当然,此时您可能需要一个deallocate函数并确保始终调用它来为新的malloc执行相应的free。
void allocate(char** dt)
{
int i;
size_t len;
char buf[] = "A test string";
len = sizeof buf;
for (i = 0; i < 3; i++)
{
dt[i] = malloc(len);
if (dt[i] != NULL)
{
memcpy(dt[i], buf, len);
printf("%s\n", dt[i]);
}
}
}
#2
The problem is in the declaration of the variable data. You have specified both a pointer (*) and an array ([]). The actual type of data is an array of pointer values.
问题出在可变数据的声明中。您已指定指针(*)和数组([])。实际的数据类型是指针值数组。
For the purpose of this particular overload, the array acts as another pointer so you have a char pointer pointer in data (char**
). The function allocate is expecting a single pointer value.
出于此特定重载的目的,该数组充当另一个指针,因此您在数据(char **)中有一个char指针指针。函数allocate期望单个指针值。
You can fix this by eliminating the pointer portion of the data type declaration.
您可以通过消除数据类型声明的指针部分来解决此问题。
char data[3];
#3
Main is right. It's allocate that's wrong.
主是对的。它分配错了。
Simply change
void allocate(char *dt)
to
void allocate(char **dt)
or (equivalently)
void allocate(char *dt[])
The argument to allocate is logically "An array of strings", which is the same as "An array of char pointers" which (as far as an argument is concerned) is the same as "A pointer to a char pointer".
分配的参数在逻辑上是“一个字符串数组”,它与“一个char指针数组”相同,它(就参数而言)与“指向char指针的指针”相同。
#1
It looks like allocate is trying to allocate three strings dynamically and assign them to each member of an array of three char*
pointers.
看起来分配试图动态分配三个字符串并将它们分配给三个char *指针数组的每个成员。
Arrays decay to pointers when you pass them as function arguments which is what you want, so the declaration of allocate
needs to be void allocate(char**)
. Passing in data
will pass in a pointer to the first element of the array, i.e. a pointer to a char*
.
当你将它们作为你想要的函数参数传递时,数组会衰减为指针,因此allocate的声明需要为void allocate(char **)。传入数据将传入指向数组的第一个元素的指针,即指向char *的指针。
In allocate you will need to allocate some memory for the new strings. I'm presuming that as this is a test example you really do want separate copies of the strings for each member of the array.
在分配中,您需要为新字符串分配一些内存。我假设这是一个测试示例,你真的想要为数组的每个成员分别创建字符串副本。
Of course, at this point you will probably want a deallocate
function and make sure that this is always called to perform the corresponding free
for the new malloc
s.
当然,此时您可能需要一个deallocate函数并确保始终调用它来为新的malloc执行相应的free。
void allocate(char** dt)
{
int i;
size_t len;
char buf[] = "A test string";
len = sizeof buf;
for (i = 0; i < 3; i++)
{
dt[i] = malloc(len);
if (dt[i] != NULL)
{
memcpy(dt[i], buf, len);
printf("%s\n", dt[i]);
}
}
}
#2
The problem is in the declaration of the variable data. You have specified both a pointer (*) and an array ([]). The actual type of data is an array of pointer values.
问题出在可变数据的声明中。您已指定指针(*)和数组([])。实际的数据类型是指针值数组。
For the purpose of this particular overload, the array acts as another pointer so you have a char pointer pointer in data (char**
). The function allocate is expecting a single pointer value.
出于此特定重载的目的,该数组充当另一个指针,因此您在数据(char **)中有一个char指针指针。函数allocate期望单个指针值。
You can fix this by eliminating the pointer portion of the data type declaration.
您可以通过消除数据类型声明的指针部分来解决此问题。
char data[3];
#3
Main is right. It's allocate that's wrong.
主是对的。它分配错了。
Simply change
void allocate(char *dt)
to
void allocate(char **dt)
or (equivalently)
void allocate(char *dt[])
The argument to allocate is logically "An array of strings", which is the same as "An array of char pointers" which (as far as an argument is concerned) is the same as "A pointer to a char pointer".
分配的参数在逻辑上是“一个字符串数组”,它与“一个char指针数组”相同,它(就参数而言)与“指向char指针的指针”相同。