繁体   English   中英

将 const char 连接到 char

[英]Concatenate const char to char

我正在尝试生成密码并将其复制到字符串中。 这是我想做的事情:

char generatePassword(int npasswords) {
    int i, z;
    char c, fullPw[300];

    for(z = 0; z < npasswords; z++) {
        for (i = 0; i < 3; ++i) {
            c = rand() % 23 + 'a';
            strcpy(fullPw, c); // ERROR HERE
            printf("%c", c);
        }
        for (i = 0; i < 3; ++i) {
            c = rand() % 23 + 'A';
            printf("%c", c);
            strcat(fullPw, c);
        }
        for (i = 0; i < 3; ++i) {
            c = rand() % 9 + '1';
            printf("%c", c);
            strcat(fullPw, c);
        }
        for (i = 0; i < 4; ++i) {
            c = rand() % 10 + '!';
            printf("%c", c);
            strcat(fullPw, c);
            strcat(fullPw, "\n");
        }
    }
    return fullPw;
}

我收到一个错误

从 'char' 到 'const char*' 的无效转换

任何人都可以帮助如何解决它?

strcpy()strcat()是对“字符串”进行操作的函数。 单个char不是字符串(在 C 中)。

strcpy()strcat()将指针指向以0结尾的char数组的第一个元素(实际上是 C 使用的,其他语言称为“字符串”)。

您显示的代码不会将其传递给函数的第二个参数,而是仅作为单个char传递。

到目前为止,错误消息告诉您的内容。

为了解决这个问题,有几种方法。

要继续使用对字符串进行操作的函数,您需要以某种方式将 append 的char设为字符串。 这可以通过使用像这样的复合文字来完成:

    strcpy(fullPw, (char[2]){c});

这个(char)[2]定义了一个char[2]类型的未命名变量,将其第一个元素初始化为c并将第二个元素隐式初始化为'\0' 总而言之,这是一个 1 字符大小的“字符串”。 将它传递给strcpy()使其衰减到第一个元素的地址,到char*

strcat()相同。

顺便说一句,这个strcat(fullPw, "\n"); 正确使用"\n"是一个字符串文字,它定义了一个 2 字符数组,第一个元素设置为 '\n' ,第二个元素设置为'\0' 按照上述方法,这也可以写成strcat(fullPw, (char[2]){'\n'});

另请注意,此类“未命名”变量的生命周期是当前的 scope。 因此,如果您想确保它们在使用后立即被释放,即在调用的函数内部,则将这些调用包含在每个 scope 中,如下所示:

    {
      strcpy(fullPw, (char[2]){c});
    }

首先,在编译时,始终启用警告,然后修复它们。

对于gcc ,至少使用: -Wall -Wextra -Wconversion -pedantic -std=gnu11

注意:其他编译器使用不同的选项来产生相同的结果。

在启用警告的情况下编译会导致:

gcc    -ggdb -Wall -Wextra -Wconversion -pedantic -std=gnu11  -c "untitled.c"  (in directory: /home/richard/Documents/forum)
untitled.c: In function ‘generatePassword’:
untitled.c:12:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
             c = rand() % 23 + 'a';
                 ^~~~
untitled.c:13:28: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]
             strcpy(fullPw, c); // ERROR HERE
                            ^
In file included from untitled.c:2:0:
/usr/include/string.h:121:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
untitled.c:17:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
             c = rand() % 23 + 'A';
                 ^~~~
untitled.c:19:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
             strcat(fullPw, c);
                            ^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcat (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
untitled.c:22:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
             c = rand() % 9 + '1';
                 ^~~~
untitled.c:24:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
             strcat(fullPw, c);
                            ^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcat (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
untitled.c:27:17: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
             c = rand() % 10 + '!';
                 ^~~~
untitled.c:29:28: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]
             strcat(fullPw, c);
                            ^
In file included from untitled.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
 extern char *strcat (char *__restrict __dest, const char *__restrict __src)
              ^~~~~~
untitled.c:33:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
     return fullPw;
            ^~~~~~
untitled.c:33:12: warning: function returns address of local variable [-Wreturn-local-addr]
Compilation finished successfully.

此外,即使在上述问题得到修复后,发布的代码也不会编译,因为它缺少语句:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

注意:仅仅因为编译器警告列表以:

Compilation finished successfully.

并不意味着代码是正确的。 这只是意味着编译器能够为代码中的每个问题合并某种解决方法。 通常结果不是你想要的

此外,function: rand()应该在前面,通常在main() function 的顶部,通过调用: srand()类似于:

#include <time.h>
...
int main( void )
{
    ...
    srand( (unsigned)time( void ) );

用随机值初始化随机数序列

强烈建议阅读/理解程序调用的函数的 MAN 页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM