我制作的这个函数是否正确地将一个字符串附加到另一个字符串?
Does this function I made correctly append a string to another string?
昨晚凌晨 3 点我还在写代码,今天醒来在源文件中发现了这个:(删掉脏话)
void append_this_stuff(char *stuff_to_append_to[], char **stuff_to_append, int position) {
char the_actual_stuff[] = *(stuff_to_append_to);
char *screw_me = *(stuff_to_append);
int someNumber = strlen(screw_me);
int j = 0;
for (int i = position; i < (someNumber + position - 1); i++) {
the_actual_stuff[i] = (screw_me + j);
j++;
}
stuff_to_append_to = &the_actual_stuff;
}
当我尝试编译它时,出现此错误:
<project root>/src/brstring.c: In function ‘append_this_stuff’:
<project root>/src/brstring.c:38:28: error: invalid initializer
char the_actual_stuff[] = *(stuff_to_append_to);
^
<project root>/src/brstring.c:46:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
the_actual_stuff[i] = (screw_me + j);
^
<project root>/src/brstring.c:50:21: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
stuff_to_append_to = &the_actual_stuff;
有谁知道我这样做是否正确?我正在通过 C99 标准和 cmake 进行编译,我正在 Fedora Linux 上使用 GCC,这是否会影响任何事情。
1.) 错误:初始值设定项无效:
char the_actual_stuff[] = *(stuff_to_append_to);
The_actual_stuff 是一个没有初始大小的数组。您的意思是使用指针指向内存地址吗?
2.) 警告:赋值从指针生成整数而不进行强制转换:
the_actual_stuff[i] = (screw_me + j);
您正在尝试为没有初始大小的数组索引赋值。
旁注:如果您打算将变量的值加在一起,您必须首先取消引用带有 * 的指针。您正在向内存地址添加一个整数。
3.) 警告:来自不兼容指针类型的赋值
stuff_to_append_to = &the_actual_stuff;
看这个post:解释得很好。
首先,char *stuff_to_append_to[]
是一个未确定长度的指针数组,这不是一个有效的参数,因为数组的最后一个维度必须在传递时指定给一个函数,否则,传递一个指向类型的指针。
接下来,char **stuff_to_append
是指向 char 的指针的 指针并且是完全有效的,但是考虑到您在函数中使用 stuff_to_append
,它是显然这不是您想要的。
如果你想插入 stuff_to_append
和截断 stuff_to_append_to
在stuff_to_append
只需将指向每个字符串的指针作为参数传递。虽然 int position
很好,但选择 unsigned 值可能更好,因为您不会在 negative 数组索引处插入。
在您的函数中,您必须验证 stuff_to_append_to
中有足够的 space 来保存从索引 position
开始的 stuff_to_append
(包括 space 空字节)
考虑到这一点,您可能需要执行以下操作:
void append_this_stuff (char *stuff_to_append_to, char *stuff_to_append,
int position)
{
int somenumber = strlen (stuff_to_append),
lento = strlen (stuff_to_append_to),
end = position + somenumber;
if (end > lento) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (int i = position; i < end + 1; i++) /* +1 to force copy of nul-byte */
stuff_to_append_to[i] = stuff_to_append[i - position];
}
您可以编写一个小测试程序来确认它的运行,例如
#include <stdio.h>
#include <string.h>
...
int main (void) {
char stuff[] = "my dog has fleas!",
append[] = "cat has none!";
int pos = 3;
printf ("original: %s\n", stuff);
append_this_stuff (stuff, append, pos);
printf (" new: %s\n", stuff);
return 0;
}
示例Use/Output
$ ./bin/append
original: my dog has fleas!
new: my cat has none!
要使用指针算法而不是使用数组索引来做同样的事情,你可以重写append_this_stuff
类似于以下:
void ats (char *to, char *from, int p)
{
if (p + strlen (from) > strlen (to)) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (to += p; *from; to++, from++)
*to = *from;
*to = *from;
}
最后,如果这个教训没有完全融入你的思维过程,“在你第一次编程面试时,不要post任何你不希望在招聘人员手中的东西position." 使用不专业或可爱的变量名,虽然它可能表达你的挫败感,但可能不会给人留下你想要的印象。说够了。
昨晚凌晨 3 点我还在写代码,今天醒来在源文件中发现了这个:(删掉脏话)
void append_this_stuff(char *stuff_to_append_to[], char **stuff_to_append, int position) {
char the_actual_stuff[] = *(stuff_to_append_to);
char *screw_me = *(stuff_to_append);
int someNumber = strlen(screw_me);
int j = 0;
for (int i = position; i < (someNumber + position - 1); i++) {
the_actual_stuff[i] = (screw_me + j);
j++;
}
stuff_to_append_to = &the_actual_stuff;
}
当我尝试编译它时,出现此错误:
<project root>/src/brstring.c: In function ‘append_this_stuff’:
<project root>/src/brstring.c:38:28: error: invalid initializer
char the_actual_stuff[] = *(stuff_to_append_to);
^
<project root>/src/brstring.c:46:24: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
the_actual_stuff[i] = (screw_me + j);
^
<project root>/src/brstring.c:50:21: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
stuff_to_append_to = &the_actual_stuff;
有谁知道我这样做是否正确?我正在通过 C99 标准和 cmake 进行编译,我正在 Fedora Linux 上使用 GCC,这是否会影响任何事情。
1.) 错误:初始值设定项无效:
char the_actual_stuff[] = *(stuff_to_append_to);
The_actual_stuff 是一个没有初始大小的数组。您的意思是使用指针指向内存地址吗?
2.) 警告:赋值从指针生成整数而不进行强制转换:
the_actual_stuff[i] = (screw_me + j);
您正在尝试为没有初始大小的数组索引赋值。
旁注:如果您打算将变量的值加在一起,您必须首先取消引用带有 * 的指针。您正在向内存地址添加一个整数。
3.) 警告:来自不兼容指针类型的赋值
stuff_to_append_to = &the_actual_stuff;
看这个post:解释得很好。
首先,char *stuff_to_append_to[]
是一个未确定长度的指针数组,这不是一个有效的参数,因为数组的最后一个维度必须在传递时指定给一个函数,否则,传递一个指向类型的指针。
接下来,char **stuff_to_append
是指向 char 的指针的 指针并且是完全有效的,但是考虑到您在函数中使用 stuff_to_append
,它是显然这不是您想要的。
如果你想插入 stuff_to_append
和截断 stuff_to_append_to
在stuff_to_append
只需将指向每个字符串的指针作为参数传递。虽然 int position
很好,但选择 unsigned 值可能更好,因为您不会在 negative 数组索引处插入。
在您的函数中,您必须验证 stuff_to_append_to
中有足够的 space 来保存从索引 position
开始的 stuff_to_append
(包括 space 空字节)
考虑到这一点,您可能需要执行以下操作:
void append_this_stuff (char *stuff_to_append_to, char *stuff_to_append,
int position)
{
int somenumber = strlen (stuff_to_append),
lento = strlen (stuff_to_append_to),
end = position + somenumber;
if (end > lento) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (int i = position; i < end + 1; i++) /* +1 to force copy of nul-byte */
stuff_to_append_to[i] = stuff_to_append[i - position];
}
您可以编写一个小测试程序来确认它的运行,例如
#include <stdio.h>
#include <string.h>
...
int main (void) {
char stuff[] = "my dog has fleas!",
append[] = "cat has none!";
int pos = 3;
printf ("original: %s\n", stuff);
append_this_stuff (stuff, append, pos);
printf (" new: %s\n", stuff);
return 0;
}
示例Use/Output
$ ./bin/append
original: my dog has fleas!
new: my cat has none!
要使用指针算法而不是使用数组索引来做同样的事情,你可以重写append_this_stuff
类似于以下:
void ats (char *to, char *from, int p)
{
if (p + strlen (from) > strlen (to)) {
fprintf (stderr, "error: insufficient space in stuff_to_append_to.\n");
return;
}
for (to += p; *from; to++, from++)
*to = *from;
*to = *from;
}
最后,如果这个教训没有完全融入你的思维过程,“在你第一次编程面试时,不要post任何你不希望在招聘人员手中的东西position." 使用不专业或可爱的变量名,虽然它可能表达你的挫败感,但可能不会给人留下你想要的印象。说够了。