[英]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.