c - 在 C 中无法识别字符串
问题描述
这是我的代码,在我使用字符串时,它表示未知类型名称。我试图包括但仍然没有工作。我不明白这里发生了什么。任何帮助将不胜感激。
#include<stdio.h>
#include<conio.h>
string getname(string);
int getclass(int);
float calculation(int);
int main()
{
string str2=getname();
printf("name of student is %s \n",str2);
int b=getclass(b);
printf("class = %d \n",b);
// float per=calculation(/*marks kithy ny??*/);
// printf("percentage = %f \n",per);
getch();
return 0;
}
string getname(string str /* str likhdy nay ithy variable da nam sirf is nal coma nhi landay */)
{
printf("enter the name of student \n");
scanf("%s",&str);
return str;
}
int getclass(int a)
{
printf("enter the class \n");
scanf("%d",&a);
return a;
}
float calculation(int marks)
{
printf("enter the marks \n");
scanf("%d",&marks);
int per=marks*100/550;
return per;
}
解决方案
string
不是C
类型。string
用替换函数参数char *
。
char* getname(char*);
由于在 this 中作为指针传递的数组C
将起作用。并使用 char 数组来存储字符串值。
char str2[10];
getname(str2);
最后一件事,不要&
在 scanf 中将运算符与数组或指针一起使用。利用:
scanf("%s", str);
推荐阅读
- react-native - React Native 中的 AsynStorage {"40":0,"_65":0,"_55":null,"_72":null}
- java - 数学公式的表示
- android - ConstraintLayout 中的固定链部分
- c# - HttpBasicAuthenticator 身份验证问题
- css - 如何通过覆盖来禁用 SCSS 属性?
- cloudant - IBM Cloud Private 2.1.0.2 安装在“等待 Cloudant 启动”时失败
- python - Boto3 Kinesis Video GetMedia 和 OpenCV
- sql - 编写查询以显示 order_ids 列表,并针对它显示同一客户下的下一个订单
- amazon-web-services - 如何在 Cloudformation 中使用 List 和 Map 参数
- javascript - 上午图表。如何更改列的背景?