帮看看这个C代码哪里有问题

C语言 码拜 7年前 (2017-04-14) 892次浏览
刚开始学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;
}

帮看看这个C代码哪里有问题

40

strcpy_s参数顺序反了
需要在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;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明帮看看这个C代码哪里有问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)