简体   繁体   English

“ char”类型的参数与char *类型的参数不兼容

[英]Argument of type “char” is incompatible with parameter of type char*

I keep receiving this error and unsure how to fix it. 我一直收到此错误,不确定如何解决。 I'm new to C programming and tried searching through the book/internet and couldn't find much help. 我是C编程的新手,并尝试通过书/互联网进行搜索,但找不到太多帮助。 I'm trying to create a program that will print a grade report using a loop and a sctructure 我正在尝试创建一个程序,该程序将使用循环和结构打印成绩报告

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

struct Grades
{
    char Name[20];
    char Hrs;
    int ID;
    char ClassName[20];
    char Grade;
    char ClassID[6];
};

int main ()
{
    struct Grades Transcript[6];
    int classCnt = 0;
    int vHrs=0;
    char vGrade[2];
    char vName[20], vCID[6], vClassName[20];

    printf("Enter Students Name: ");
    fgets(vName, 20, stdin);

    do
    {   printf("Enter Class ID; ");
        fgets(vCID, 6, stdin);
        strcpy_s(Transcript[classCnt].ClassID, vCID);
        printf("Enter Class Name: ");
        fgets(vClassName, 20, stdin);
        strcpy_s(Transcript[classCnt].ClassName, vClassName);
        printf("Enter Class Hours: ");
        scanf("%d", &vHrs);
        strcpy(Transcript[classCnt].Hrs, vHrs);         //Problem occurs here
        printf("Enter Class Grade: ");
        scanf("%c", vGrade);
        strcpy(Transcript[classCnt].Grade, vGrade);     //Problem occurs here
        classCnt++;
    }while(classCnt<=6);
}

You actually have a number of problems here: 您实际上在这里有很多问题:

First, strcpy() is used to copy a string, if you have a character and you want it to assign it, you can simply assign it with the = operator. 首先, strcpy()用于复制字符串,如果您有字符并希望为其分配字符,则可以简单地使用=运算符对其进行分配。 The strcpy() function is used when you have a character array you want to assign. 当您有要分配的字符数组时,将使用strcpy()函数。

So your first problem 所以你的第一个问题

strcpy(Transcript[classCnt].Hrs, vHrs);

Hrs from your struct is just a char type, and vHrs is an int type. 来自您的struct的Hrs只是一个char类型,而vHrs是一个int类型。 You can simply assign it like: 您可以像这样简单地分配它:

Transcript[classCnt].Hrs = vHrs;

However, an int can hold a lot more data than a char can, this is going to give you a warning about overflow and you should listen to it (depending on the implementation char holds -128 to 127, where as int holds −2,147,483,648 to 2,147,483,647). 但是,一个int可以容纳比char更多的数据,这将向您发出有关溢出的警告,您应该听一下(取决于实现char保持-128到127,其中int保持−2,147,483,648到2,147,483,647)。 Decide what data type you really wanted here and either make Hrs an int or vHrs a char then just do the assignment. 在这里确定您真正想要的数据类型,然后将Hrsint或将vHrschar然后执行分配。

Second problem: 第二个问题:

scanf("%c", vGrade);

vGrade as a character array (it is made up of more than one character) that means you should assign it with the string format operator "%s" , but when you do a string you should make the array long enough for the number of characters you want + 1 (for the NULL terminator). vGrade是一个字符数组(由多个字符组成),这意味着您应该为它分配字符串格式运算符"%s" ,但是当您执行字符串操作时,应使数组足够长以容纳字符数您需要+1(用于NULL终止符)。

Third problem: 第三个问题:

strcpy(Transcript[classCnt].Grade, vGrade);

Grade is a char whereas vGrade is an array. Grade是一个charvGrade是一个数组。 Again, you have to make a decision of type, if you wanted a "string" of characters then you need to make them both arrays, if you wanted just a single character then change the type of vGrade and do a simple assignment with the = operator. 同样,您必须确定类型,如果您想要一个“字符串”字符,那么就需要将它们都设置为两个数组,如果只需要一个字符,则可以更改vGrade的类型并使用=进行简单赋值操作员。

The thing you're running into here is a data typing error. 您在这里遇到的是数据输入错误。

The structure member .Hrs is of type "char" . 结构成员.Hrs的类型为"char" The argument to strcpy is a "char *" . strcpy的参数是"char *" Think of it like this: A char is a single 8 byte number representing a value in ASCII. 这样想: char是一个8字节的数字,代表ASCII值。 A "char *" is a pointer to an array of characters (or a string in C language). "char *"是指向字符数组(或C语言中的字符串)的指针。

The Above is traditionally true,but , today it might be a unicode or multi-byte character string, as well. 上面的代码在传统上是正确的,但今天它也可能是unicode或多字节字符串。 In either case, what you need is the assignment operator ( = ), not a strcpy . 无论哪种情况,您都需要赋值运算符( = ),而不是strcpy

So you're telling strcpy to look at a single value ( char ), not a string of values ( char * ) which is it expecting as it's argument. 因此,您要告诉strcpy查看单个值( char ),而不是它期望作为参数的字符串( char * )。 In this case, you can just copy the value of the integer you scanned in with scanf directly. 在这种情况下,您可以直接复制用scanf扫描的整数值。

Transcript[classCnt].Hrs = vHrs;

and

Transcript[classCnt].Grade = vGrade;

If you're looking for the best book ever written, well in my opinion anyway :-), on C Language, check out: C Programming Language (2nd Edition) - Looks like it's available on Amazon (or at you're favorite used book seller) for about $21 (USD). 如果您正在寻找有史以来最好的书,无论如何,我认为:-)关于C语言,请查看:C编程语言(第二版)-看起来它在Amazon上可用(或者您最喜欢用它图书卖家),价格约为21美元。

暂无
暂无

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

相关问题 类型“ double”的参数与类型“ char *”的参数不兼容 - argument of type “double” is incompatible with parameter of type “char *” “volatile char *”类型的参数与“const char *”类型的参数不兼容 - Argument of type “volatile char *” is incompatible with parameter of type “const char *” const char* 类型的参数与“LPCWSTR”类型的参数不兼容 - argument of type const char* is incompatible with parameter of type “LPCWSTR” IntelliSense:“ const char *”类型的参数与“ LPCWSTR”类型的参数不兼容 - IntelliSense: argument of type “const char *” is incompatible with parameter of type “LPCWSTR” 将“void”传递给不兼容类型“const char *”的参数? - Passing 'void' to parameter of incompatible type 'const char *'? 不兼容的指针类型,将“ char(*)[128]”传递给类型为“ char **”的参数 - incompatible pointer types passing 'char (*)[128]' to parameter of type 'char **' 指针转换不兼容的整数,将“ char”传递给类型为“ const char *”的参数 - Incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *' 不兼容的指针类型将“char (*)[3]”传递给“const char *”类型的参数 - incompatible pointer types passing 'char (*)[3]' to parameter of type 'const char * 不兼容的指针类型将&#39;char *(char *)&#39;传递给类型&#39;VALUE(*)()&#39;的参数 - incompatible pointer types passing 'char *(char *)' to parameter of type 'VALUE (*)()' 错误[P​​e167]:“ uint16_t *”类型的参数与“ unsigned char *”类型的参数不兼容 - Error[Pe167]: argument of type “uint16_t *” is incompatible with parameter of type “unsigned char *”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM