微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C 对二进制 + 的无效操作数有‘char *’和‘char *’?

如何解决C 对二进制 + 的无效操作数有‘char *’和‘char *’?

在 C 我有

filedata->string_table + (X)->sh_name

现在我想在它旁边添加以下字符串:“new”

我试过了:

filedata->string_table + (X)->sh_name + "new"

但这不会编译,我得到:

readelf.c:6807:83: error: invalid operands to binary + (have ‘char *’ and ‘char *’)
  filedata->string_table + (X)->sh_name : filedata->string_table + (X)->sh_name+"new")
                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      ^

如何解决这个问题?

解决方法

(X)->sh_name+"new" + 在 C 中不连接字符串。它尝试添加指向无效指针的指针。

您需要使用函数 strcatstrncat

抱歉,这些函数会影响字符串,但我想要一个新的 –

就这么简单:

char *strcatAlloc(const char * restrict s1,const char * restrict s2)
{
    size_t s1len;
    char *newstring;

    if(s1 && s2)
    {
        s1len = strlen(s1);
        newstring = malloc(s1len + strlen(s2) + 1);
        if(newstring)
        {
            strcpy(newstring,s1);
            strcpy(newstring + s1len,s2);
        }
    }
    return newstring;
}

int main(void)
{
    // you should check if the function fails. Omitted for the example clarity
    printf("`%s`\n",strcatAlloc("hello ","world"));
    printf("`%s`\n",strcatAlloc("",""));
    printf("`%s`\n",""));

    //you should free allocated space
}

或可以传递缓冲区的版本(如果该缓冲区为 NULL,它将分配内存)

char *strcatAlloc(const char * restrict s1,const char * restrict s2,char *buff)
{
    size_t s1len;

    if(s1 && s2)
    {
        s1len = strlen(s1);
        if(!buff) buff = malloc(s1len + strlen(s2) + 1);
        if(buff)
        {
            strcpy(buff,s1);
            strcpy(buff + s1len,s2);
        }
    }
    return buff;
}

int main(void)
{

    char s[100];
    // you should check if the function fails. Omitted for the example
     printf("`%s`\n","world",NULL));
    printf("`%s`\n","",s));  //you can pass your own large enough buffer 
    printf("`%s`\n",NULL));

    //you should free allocaated space
}
,

使用正确的方式连接字符串。

如果要将结果添加到现有缓冲区,请使用 Tomcat 连接字符串。

strcat()

您也可以使用 char buffer[1024000]; /* enough size */ strcpy(buffer,filedata->string_table + (X)->sh_name); strcat(buffer,"new"); 将连接结果放入缓冲区。

snprintf()

要将结果打印到标准输出,请使用两个 char buffer[1024000]; /* enough size */ snprintf(buffer,sizeof(buffer),"%s%s",filedata->string_table + (X)->sh_name,"new"); 格式说明符连续打印两个字符串。

%s

如果字符串 printf("%s%s","new"); 是固定的,你也可以这样做:

"new"
,

您不能使用运算符 strings 连接 char+。使用 strncat()snprintf()

,

请记住,C 没有实际的字符串类型 - 字符串存储在 char 的数组中,并且 C 没有为数组类型定义 += 运算符。

您可以使用 strcatstrncat 将字符串附加到字符缓冲区:

char foo[] = "This is ";
char bar[] = "a test";
char target[ sizeof foo + sizeof bar ];

strcpy( target,foo ); // copies contents of foo to target buffer
strcat( target,bar ); // appends contents of bar to target buffer

目标缓冲区必须足够大以容纳包括终止符在内的最终字符串。在此示例中,我们采用了 foobar 的大小(不是长度!),它们分别为 9 和 7(包括字符串终止符)。请注意,如果它们被声明为

,这将不起作用
char *foo = "This is ";
char *bar = "a test";

因为 sizeof foo 只会给我们指针对象的大小,而不是它指向的字符串的大小。在这种情况下,您必须在运行时使用 strlen 获取大小。如果您使用的是支持 VLA(C99 或更高版本)的版本,您可以简单地执行

char *foo = "This is ";
char *bar = "a test";

char target[ strlen( foo ) + strlen( bar ) + 1];

否则,您必须动态分配缓冲区:

char *target = malloc( strlen( foo ) + strlen( bar ) + 1 );

并且您必须记住在完成后free


如果您混合使用字符串和非字符串类型,则可以使用 sprintf:

char *name = "blah";
int number = 42;

sprintf( target,"%s-%d",name,number ); // writes "blah-42" to target buffer

同样,目标缓冲区必须足够大以存储结果字符串和终止符。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。