刚开始学C,写了几行代码,报错了,用的是VS2015,报的错是c2040, getstra”: “char *()” differs in levels of indirection from “int ()” ,看了好久不清楚啥问题,请各位帮看看,谢谢
#include <stdlib.h> int main() { char *ssa = getstra(); printf("%d\n", *ssa); free(ssa); system("pause"); return 0; } char *getstra() { char *bufa = malloc(sizeof(char) * 100); strcpy_s(bufa,"abcdefg",10); return bufa; }
解决方案
10
本人来给个代码
#include "stdafx.h" #include <stdlib.h> #include <string.h> char *getstra(){ char *bufa=(char*)malloc(sizeof(char)*100); strcpy(bufa,"abcdefg"); return bufa; } int main() { char *ssa=getstra(); printf("%d\n",*ssa); free(ssa); system("pause"); return 0; }
40
strcpy_s参数顺序反了
需要在main前加上char *getstra();声明
需要在main前加上char *getstra();声明
#include <stdlib.h> char *getstra(); int main() { char *ssa = getstra(); printf("%d\n", *ssa); free(ssa); system("pause"); return 0; } char *getstra() { char *bufa = (char*)malloc(sizeof(char) * 100); strcpy_s(bufa, 100, "abcdefg"); return bufa; }
20
#include <stdlib.h> char *getstra(); //首先这里要声明,这就是错误显示的原因 int main() { char *ssa = getstra(); printf("%d\n", *ssa); free(ssa); system("pause"); return 0; } char *getstra() { char *bufa = malloc(sizeof(char) * 100); strcpy_s(bufa,"abcdefg",10); //strcpy_s(bufa,10,"abcdefg"); //函数应该是这样 return bufa; }