【C语言】一些简单编译错误或警告

1. [Warning] ‘s’ is used uninitialized in this function [-Wuninitialized]

错误代码

#include <stdio.h>

int main(){
	char *s;
	scanf("%3s",s);
	printf("%s",s);
	return 0;
}

正确代码

#include <stdio.h>

int main(){
	char s[100];
	scanf("%3s",s);
	printf("%s",s);
	return 0;
}

以下代码也是正确的。

char *s;
s="ABCDE";

错误原因

没有给s分配内存空间,如果要使用char *s的话,需要先进行赋值如char *s="hello world"

posted @ 2022-02-13 22:49  Fannnf  阅读(1999)  评论(0)    收藏  举报