如何将char传递到函数中?

如何将char传递到函数中?,c,string,C,String,我想随机化我的字符串,所以这是我的代码 while(strcmp(word,"END")!=0) { printf("Enter word"); fgets(input,sizeof(input),stdin); sscanf(input,"VERTEX %s",key1); strcpy(list[count],key1); count++; } random(list); 我将list和key1声明为charlist[32],key1[32] 然后我尝试将它传递到这个函数中 void ra

我想随机化我的字符串,所以这是我的代码

while(strcmp(word,"END")!=0)
{
printf("Enter word");
fgets(input,sizeof(input),stdin);
sscanf(input,"VERTEX %s",key1);
strcpy(list[count],key1);
count++;
}
random(list);
我将list和key1声明为
charlist[32],key1[32]
然后我尝试将它传递到这个函数中

void random(char* list)
{
    int i = rand()%5;
    char key1[32];
    printf("%d",i);
    printf("%s",list[i]);
    strcpy(key1,list[i]);
}
但它给了我这个警告

incompatible integer to pointer conversion passing 'char'
  to parameter of type 'char *'

而且它不能打印。有什么建议吗?

如果您已经定义了
字符列表[32],调用<代码>随机(列表)并使用
无效随机(字符*列表)
,然后

   strcpy(list[count],key1);
   printf("%s",list[i]);
   strcpy(key1,list[i]);
所有的陈述都是错误的

  • 期望其参数分别为
    char*
    const char*
  • printf()
在您的代码中,
list[count]
list[i]
char
类型,而不是
const char*
char*
,视需要而定

void random(char *list);
所以这里的
list
char
类型的指针,当您将有效的char数组传递给这个API时,list指向您的数组
list

现在你需要的是

   printf("%s",list); /* Format specifier %s needs char * */
   strcpy(key1,list); /* The arguments should be char * */
#包括
#包括
#包括
#包括
void random(字符列表[][32],字符*键,整数大小){
int i=rand()%size;
printf(“选项%d\n”,i);
printf(“选择键为%s\n”,列表[i]);
strcpy(键,列表[i]);
}
内部主(空){
字符列表[5][32],键1[32],单词[32];
整数计数=0;
srand(时间(空));
而(1){
printf(“输入单词:”);
fgets(word、sizeof(word)、stdin);
if(strcmp(单词“END\n”)==0)
打破
if(计数<5&&1==sscanf(字,“顶点%s”,键1)){
strcpy(列表[count++],键1);
}
}
如果(计数){//计数的保护==0
随机(列表、键1、计数);
printf(“选择键:%s\n”,键1);
}
返回0;
}

编译器会告诉您这个问题:
不兼容的整数到指针转换将“char”传递到“char*”类型的参数。
下次尝试搜索该错误
char list[32]-->
字符列表[5][32]
无效随机(char*list)
-->
无效随机(char list[][32])
我想将key1复制到数组中,而不是将list[I]复制到key1中@苏拉夫Ghosh@asiandudeCom更新了我的答案,增加了更多的澄清。请检查。你说没有初始化是什么意思?我已经声明了@SouravGhosh@asiandudeCom您是否首先理解了
list[count]
不能保存字符串?如果我将list[32][20]更改为二维数组会怎么样。它能撑得住吗@我在while循环中使用strcpy时出错了吗?我想复制一个已经被sscanf的key1到一个数组中@Gopi@asiandudeCom如果要存储多个字符串,则应具有2D字符数组或字符指针数组。。所以,是的,你们在while循环中所做的是错误的。更改列表至
列表[32][100]并确保在运行此代码时,API中的
char*
char
没有错误。它有浮点异常@BLUEPIXY@asiandudeCom这对我很管用。你有什么意见吗?顺便说一下,我不使用浮点。我输入了大约3个字,然后出现了浮点错误@bluepixy。你输入了什么,不是一个数字字。可能正确的输入是因为没有人。所以,
count
0
。我输入单词“look”、“exit”和“word”,然后键入END,然后出现错误。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void random(char list[][32], char *key, int size){
    int i = rand()%size;
    printf("choice %d\n",i);
    printf("choice key is %s\n", list[i]);
    strcpy(key, list[i]);
}

int main(void){
    char list[5][32], key1[32], word[32];
    int count = 0;
    srand(time(NULL));

    while(1){
        printf("Enter word : ");
        fgets(word, sizeof(word), stdin);
        if(strcmp(word, "END\n")==0)
            break;
        if(count < 5 && 1==sscanf(word, "VERTEX %s", key1)){
            strcpy(list[count++],key1);
        }
    }
    if(count){//guard for count == 0
        random(list, key1, count);
        printf("choice key : %s\n", key1);
    }

    return 0;
}