从 stdin 动态分配行?
Dynamically allocating lines from stdin?
我正在尝试读取一个包含 10 行不同长度(不会超过 349 个字符)的文件,并将它们存储在一个动态分配的数组中,以便以后能够对其进行操作。我有下面的代码,它给我一条 "warning: passing argument 1 of âstrcpyâ makes pointer from integer without a cast" 消息。
我这里做错了什么?我的逻辑是地址应该是指向指针数组的指针,其中数组中的每个指针都指向字符串中的一行,我会尝试通过对该行使用指向该点的 strcpy 来实现这一点。
char buffer[350];
char** addresses;
int i = 0;
*addresses = malloc(sizeof(char*)*10); /* Let's start off with 10 addresses */
while(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
strcpy(*addresses[i],buffer);
i++;
}
你要这样给,
strcpy(addresses[i],buffer);
如果你这样给,
strcpy(*addresses[i],buffer);
第一个参数将被视为单个字符。
在分配内存时,您可以这样做,
address=malloc(sizeof(char)*10);
你必须为每个指针分配内存。
address[i]=malloc(strlen(buffer)+1);
否则,您可以使用 strdup
函数,它将为给定的内存分配内存
字符串长度。
address[i]=strdup(buffer);
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory
for the new string is obtained with malloc(3), and can be freed with free(3).
您有几个问题:
语法:
使用
strcpy(addresses[i],buffer);
而不是
strcpy(*addresses[i],buffer);
内存:
您需要为 addresses[i]
分配内存,然后才能将 buffer
的内容复制到它。
在调用 strcpy
.
之前添加一行为 addresses[i]
分配内存
addresses[i] = malloc(sizeof(buffer));
addresses
是指向指针的指针,因此您需要先为指针分配内存,然后再分别为每个指针分配内存,如下所示。
在 2D 中 space 如果 addresses
是指向指针的指针,则 addresses[i]
是 pointer.So 在写入此位置之前为其分配内存。
char** addresses;
addresses = malloc(sizeof(char*)*10);// Now you 10 character pointers
int i = 0;
while(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
size_t n = strlen(buffer);
if(n>0 && buffer[n-1] == '\n')
buffer[n-1] = '[=10=]';
if(i>=10)
break;
addresses[i] = malloc(strlen(buffer)+1);//Allocate memory to the pointer before writing to it
strcpy(addresses[i],buffer);
i++;
}
PS:fgets()
带有一个换行符,所以我添加了一个检查以用 null 替换换行符。
快速解决我在此处看到的错误:
- 您正在取消引用
addresses
(例如 *addresses
或 addresses[0]
),但未确保 addresses
指向某处有效。在取消引用之前,您需要分配给 addresses
。
strcpy(*addresses[i],buffer)
中*addresses[i]
的类型是char
,这里应该是char *
。这就是您的编译器所抱怨的。我怀疑你的意思是 strcpy(addresses[i], buffer)
.
即使您要在嵌入式环境中 运行 此代码,您的要求似乎也很简单,您不需要 malloc
。事实上,引入 malloc
会使事情复杂化......除非绝对必要,否则我倾向于避免它。请改用自动存储期限。它会让生活更轻松,因为您不会有那么多错误情况需要处理(尽管实际上您并没有处理它们)...
#define nelem(array) (sizeof array / sizeof *array) /* number of elements in array */
int main(void)
{
char address[10][351] = { 0 };
size_t size = 0;
while (size < nelem(address) && fgets(address + size, sizeof *address, stdin))
{
address[size][strcspn(address[size], "\n")] = '[=10=]';
size++;
}
}
请注意,10
和 351
仅在此处出现一次...请在合理范围内根据需要随意调整它们。如果您可以将它们乘以兆字节区域,您可能需要考虑不同的数据结构,具体取决于您打算用它做什么。
我正在尝试读取一个包含 10 行不同长度(不会超过 349 个字符)的文件,并将它们存储在一个动态分配的数组中,以便以后能够对其进行操作。我有下面的代码,它给我一条 "warning: passing argument 1 of âstrcpyâ makes pointer from integer without a cast" 消息。
我这里做错了什么?我的逻辑是地址应该是指向指针数组的指针,其中数组中的每个指针都指向字符串中的一行,我会尝试通过对该行使用指向该点的 strcpy 来实现这一点。
char buffer[350];
char** addresses;
int i = 0;
*addresses = malloc(sizeof(char*)*10); /* Let's start off with 10 addresses */
while(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
strcpy(*addresses[i],buffer);
i++;
}
你要这样给,
strcpy(addresses[i],buffer);
如果你这样给,
strcpy(*addresses[i],buffer);
第一个参数将被视为单个字符。
在分配内存时,您可以这样做,
address=malloc(sizeof(char)*10);
你必须为每个指针分配内存。
address[i]=malloc(strlen(buffer)+1);
否则,您可以使用 strdup
函数,它将为给定的内存分配内存
字符串长度。
address[i]=strdup(buffer);
The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).
您有几个问题:
语法:
使用
strcpy(addresses[i],buffer);
而不是
strcpy(*addresses[i],buffer);
内存:
您需要为
addresses[i]
分配内存,然后才能将buffer
的内容复制到它。在调用
之前添加一行为strcpy
.addresses[i]
分配内存addresses[i] = malloc(sizeof(buffer));
addresses
是指向指针的指针,因此您需要先为指针分配内存,然后再分别为每个指针分配内存,如下所示。
在 2D 中 space 如果 addresses
是指向指针的指针,则 addresses[i]
是 pointer.So 在写入此位置之前为其分配内存。
char** addresses;
addresses = malloc(sizeof(char*)*10);// Now you 10 character pointers
int i = 0;
while(fgets(buffer, sizeof(buffer), stdin) != NULL)
{
size_t n = strlen(buffer);
if(n>0 && buffer[n-1] == '\n')
buffer[n-1] = '[=10=]';
if(i>=10)
break;
addresses[i] = malloc(strlen(buffer)+1);//Allocate memory to the pointer before writing to it
strcpy(addresses[i],buffer);
i++;
}
PS:fgets()
带有一个换行符,所以我添加了一个检查以用 null 替换换行符。
快速解决我在此处看到的错误:
- 您正在取消引用
addresses
(例如*addresses
或addresses[0]
),但未确保addresses
指向某处有效。在取消引用之前,您需要分配给addresses
。 strcpy(*addresses[i],buffer)
中*addresses[i]
的类型是char
,这里应该是char *
。这就是您的编译器所抱怨的。我怀疑你的意思是strcpy(addresses[i], buffer)
.
即使您要在嵌入式环境中 运行 此代码,您的要求似乎也很简单,您不需要 malloc
。事实上,引入 malloc
会使事情复杂化......除非绝对必要,否则我倾向于避免它。请改用自动存储期限。它会让生活更轻松,因为您不会有那么多错误情况需要处理(尽管实际上您并没有处理它们)...
#define nelem(array) (sizeof array / sizeof *array) /* number of elements in array */
int main(void)
{
char address[10][351] = { 0 };
size_t size = 0;
while (size < nelem(address) && fgets(address + size, sizeof *address, stdin))
{
address[size][strcspn(address[size], "\n")] = '[=10=]';
size++;
}
}
请注意,10
和 351
仅在此处出现一次...请在合理范围内根据需要随意调整它们。如果您可以将它们乘以兆字节区域,您可能需要考虑不同的数据结构,具体取决于您打算用它做什么。