简体   繁体   English

关于使用“ strcpy”功能

[英]about 'strcpy' function using

char input[32];
char name[32];
char discountUser[32];//not sure to using about arrays.
char notDiscountUser[32];//not sure to using about arrays.
int i,j;
int len;
fgets(input,32,stdin);
sscanf(input,"%s",name);
len = strlen(name);
for(i=0,j=0; i < len; i++)
{
if(isdigit(name[i]))
{
    digits[j] = name[i];

    if (digits[j] > 48)
    {
        strcpy(discountUser[i],name); //i want to stored the name at i index
        printf("you have discount code\n");
    }
    else if (digits[j] <= 48)
    {
        strcpy(notDiscountUser[i],name); //i want to stored the name at i index
        printf("you don't have discount code\n");
    }
    j++ ;
}
}

I need to separate user who have discountcode or not by enter 3charofname and 1 digit eg. 我需要输入3charofname和1位数字来区分是否有折扣代码的用户。 cat2 if digit more than 0 so, the user have discount if digit is 0 so, they not have discount example i have cat0 bee1 ear2 eye0 when i print notdiscount : cat0 , eye0 discount : bee1 , ear2 cat2如果数字大于0,则用户有折扣,如​​果数字为0,则他们没有折扣示例例如,当我打印notdiscount时,我有cat0 bee1 ear2 eye0:cat0,eye0折扣:bee1,ear2

i check digit by isdigit and i have problem with copy username by strcpy . 我通过isdigit检查digit,并通过strcpy复制用户名时遇到问题。 Thanks for help . 感谢帮助 。 :] :]

Use 2D arrays as: 将2D数组用作:

char discountUser[N][32];  
char notDiscountUser[N][32];  

where N is max number of users, you can #define it to some value. 其中N是最大用户数,您可以将其#define为某个值。

What you are trying to do is: 您正在尝试做的是:

char discountUser[32];

This is a string, if you use char discountUser[i] you are referring to the char (A single character, not a string) at index i in the string discountUser . 这是一个字符串,如果使用char discountUser[i] ,则是在字符串discountUser索引i处引用char (单个字符,不是字符串)。

Now, strcpy expects a string as input to both of its arguments, therefore you cannot pass discountuser[i] as its input. 现在, strcpy希望将字符串作为两个参数的输入,因此您不能将discountuser[i]作为其输入。

When you declare a 2D array as I told above, discountuser[i] will refer to a string (actually dicountuser[i] will act as char pointer), so now you are allowed to pass it as argument to strcpy . 当您如上所述声明2D数组时, dicountuser[i] discountuser[i]将引用一个string (实际上dicountuser[i]将用作char指针),因此现在您可以将其作为参数传递给strcpy

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

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