- #include<stdio.h>
- void question1(){
- //注意少得很么时候用分号; 逗号, 等号= ,什么时候不写
- //下面这些,你能保证一个也写不错嘛
- char arr[3]={'a','b','c'};
- char arr2[3][4] = {
- {'a','b','c','d'},
- {'a','b','c','d'},
- {'a','b','c','d'},
- };
-
- char str[]="apple";
- char str2[3][10]={
- "apple",
- "apple",
- "apple"
- };
-
- struct Person{
- char *name;
- int age;
- float height;
- };
- struct Person person = {
- "jack",
- 18,
- 1.78
- };
-
- struct Car{
- char *name;
- int wheel;
- float speed;
- }car = {"bike",2,34};
-
- enum Season{
- kSpring,
- kSummer,
- kWinter
- };
- enum Season season = kSpring;
-
- enum Phone{
- kApple,
- kXioami,
- kHuawei
- }phone = kApple;
-
- typedef enum IphoneColor{
- kWhite,
- kBlack,
- kRosepink,
- kRichgold
- }color;
- color myphone = kBlack;
- }
- void question2(){
-
- //野指针
- int a=1;
- int *p;
- *p=a;
- printf("*p=%d",*p);
-
- printf("请输入一个整数给*野指针赋值:\n");
- scanf("%d",p);
-
- printf("*p=%d",*p);
- //使用野指针,编译器不会报错,本来就没有语法错误,但是有"逻辑"错误
- }
- void question3(){
- //用指针定义的字符串
- char *name = "jack";
- name = "rose";
- name = "Django";
- *(name+1) = 'h';
-
- printf("%s",name);
-
- //你可以给指针换一串字符串(换个地址),但是不能让这个字符串常量改变
- }
- void question4(){
- //批量赋值的问题
-
- //一.数组的批量赋值
- char arr[4];
- arr = {1,2,3,4};//错误,数组的批量赋值只能在定义的时候,这样写的话,数组名是个地址,不接收"值"
-
- //二.结构体的批量赋值
- struct Person{
- char *name;
- int age;
- }person;
- person={"Django",23};//错误
- person=(struct Person){"Django",23};//正确,需要一个强转
- }
- int main()
- {
- return 0;
- }
复制代码 |
|