我正确执行的此功能是否将字符串附加到另一个字符串? [英] Does this function I made correctly append a string to another string?

查看:100
本文介绍了我正确执行的此功能是否将字符串附加到另一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨晚凌晨3点正在编写代码,今天我醒来并在源文件中找到了它:(删节的诅咒词)

I was up coding at 3 AM last night, and I wake up today and find this in a source file: (curse words redacted)

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;
}

当我尝试编译它时,出现此错误:

When I try to compile it, I get this error:

<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,这会影响任何东西。

Does anyone have any idea if I'm doing this right? I'm compiling via the C99 standard and cmake and I'm using GCC on Fedora Linux, should that affect anything.

推荐答案

首先, char * stuff_to_append_to [] 是长度不确定的指针的指针数组,它不是有效参数,它是数组的最后一个维度

First, char *stuff_to_append_to[] is an array of pointers of undetermined length, that is not a valid parameter, as the last dimension of an array must be specified when passed to a function, otherwise, pass a pointer to type.

下一步, char ** stuff_to_append 是指向char 的指针的指针,并且完全有效,但是考虑到在函数中使用了 stuff_to_append ,很明显,这不是什么

Next, char **stuff_to_append is a pointer to pointer to char and is completely valid, but given your use of stuff_to_append within the function, it is obvious that is not what you intended.

如果您想插入 stuff_to_append 截断 stuff_to_append 末尾的 stuff_to_append_to 只需将指向每个字符串的指针作为参数即可。虽然 int位置很好,但是选择 unsigned 值可能会更好,因为您不会在负值处插入数组索引。

If you wish to insert stuff_to_append and truncate stuff_to_append_to at the end of stuff_to_append simply pass a pointer to each string as the argument. While int position is fine, a choice of an unsigned value may be better as you will not insert at a negative array index.

在函数内部,必须验证 stuff_to_append_to 中是否有足够的空间来容纳 stuff_to_append 从索引位置开始(包括 null-byte 的空间)

Inside your function, you must validate there is sufficient space in stuff_to_append_to to hold stuff_to_append beginning at index position (including space for the nul-byte)

考虑到这一点,您可能需要执行以下操作:

With that in mind, you may want to do something like the following:

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];
}

您可以编写一个小型测试程序来确认其运行,例如

You can write a small test program to confirm it's operation, e.g.

#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;
}

示例用法/输出

$ ./bin/append
original: my dog has fleas!
     new: my cat has none!

使用指针算术而不是使用数组索引,您可以重写 append_this_stuff ,类似于以下内容:

To do the same thing using pointer arithmetic instead of using array indexes, you can rewrite append_this_stuff similar to the following:

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;
}

最后,如果本课程并未完全根植于您的思考过程中, em>在面试第一个编程职位时,切勿在招聘人员手中发布任何您不想要的东西。使用不专业或可爱的变量名可能会表达您的挫败感,但可能不会你想要的印象在另一个人的手中。够了。

Lastly, if this lesson is not completely ingrained in your thought process, "Never post anything that you would not want in the hands of the recruiter as you interview for your first programming position." The use of unprofessional or cute variable names, while it may express your frustrations, may not give the impression you want in the hands of another. Enough said.

这篇关于我正确执行的此功能是否将字符串附加到另一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆