C/C++基础之一

C/C++基础之一(数据类型、数组、指针、结构体)

Talk is cheap. Show me the code.

  • 结构体定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef TYPEDEFINE_H
#define TYPEDEFINE_H
//枚举的使用
typedef enum{
RED = 0,
YELLOW = 1,
GREEN = 2
}TrafficLight;

typedef enum{
START,
RUNNING,
SUSPEND,
STOP,
DESTORY
}Mission;

struct Student
{
int age;
int sex;
char lastName[10];
char firstName[10];
};
  • main函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

void testDatatype()
{
printf("数据类型:\n");
int intValue = 1;
printf("整型%d\n",intValue);
char charValue = 'a';
printf("char类型:%c\n",charValue);
unsigned int uintValue = 2;
printf("无符号整型:%d\n",uintValue);
float fValue = 3.14;
double dValue = 3.14;
//long 是4 byte, long long是8字节
long long lValue = 123456789;
printf("float类型:%.2f\n",fValue);
printf("double类型%f\n",dValue);
printf("long类型:%ld\n",lValue);
//除此之外还有一些无符号整型的类型 ,无符号第一位不表示正负符号,只存在正数,因此比有符号的正数大一倍。
//unsigned int
//unsigned short
//unsigned char
//unsigned long
}

//指针
void testPointer()
{
int a = 10;
int *p = &a;
int **pp = &p;
printf("\n");
printf("a value:%d\n,a address is:%p\n",a,&a);
// &p p这个int指针自身的内存地址
//p p指针存放的地址值,
//*p 解引用,得到p指针存放的地址值所指向的值
printf("p memroy address is:%p,p point address is%p, p point value is:%d\n",&p,p,*p);
// pp 是pp指针的值, &pp是指针的内存地址, **pp是两次解引用,得到p指针所指向的值
// *pp 一次解引用,得到p的值,也就是a的地址
printf("*pp is%p, pp memroy address is:%p,pp point address value is:%p\n, pp value:%d\n",*pp, &pp, pp,**pp);


int s = 1024;
int *sP = &s;
// 发生了隐式转换 'b' == 98
*sP = 'b';

printf("s origin address:%p\n",&s);
printf("sp point address:%p\n",&sP);
printf("sP point target address:%p\n",sP);
printf("sP point target vakye:%c\n",*sP);
printf("point sieze:%d\n",sizeof(*sP));
printf("%d\n",s);

int arrayA[] = {1,2,3};
int *pA = arrayA;

printf("pA is :%d\n",*(pA+2));

int arrayB[2][3] = {{1,2,3},{4,5,6}};
int (*arrayBP)[3] = arrayB;
//int (*p)[3] 是指针,指向“每行 3 个元素”的数组。
int (*pC)[3] = arrayB;
printf("arrayBP[0][2]%d\n",arrayBP[0][2]);
printf("%d\n",(*(*(pC+1)+2)));

}

int testCalculate(int a, int b)
{
return a + b;
}

void testArray()
{
//数组测试,数组在创建的时候必须指明容量,不要越界访问
int a[] = {1,3,5};
int b[10];
for(int i =0;i<10;i++)
{
b[i] = i;
}
// 二维数组
int c[2][3] = {{1,2,3},{4,5,6}};

char str[] ="hello";
// 得到的这个长度是6,多了一个\0
int charArrayLength = sizeof(str)/sizeof(str[0]);
printf("%d\n",charArrayLength);
printf("%d\n",sizeof(str));
for(int i=0;i<charArrayLength;i++)
{
printf("%c\n",str[i]);
}
}

char* reverseChars(char *s,int length)
{
//1.当前的字符串长度,下面这个写法是错误的,因为sizeof(s) 是指针的长度,指针的长度是4 byte
// int length = sizeof(s)/sizeof(s[0]);
printf("the length is %d",length);
//2.创建存储结果的字符串数组
char *targetResult = (char *)malloc((length+1) * sizeof(char));
if (targetResult == NULL)
{
printf("内存分配失败");
return NULL;
}
//3.for倒叙循环获取字符,存入数组
for(int i=0;i<length;i++)
{
targetResult[i] = s[length-1-i];
}

targetResult[length] = '\0';
//4.返回结果
return targetResult;
}

int main()
{
testDatatype();
testPointer();
//测试指针函数,指针函数作为一个变量可以在函数间传递,方便的进行回调,使得程序更加灵活。
int (*pSumFunc)(int,int) = testCalculate;
int result = pSumFunc(10,20);
printf("Sum is:%d\n",result);

int i = 0;
int count = 10;

for (i = 0; i < count; i++)
{
printf("value is:%d \n",calculateSub(i*i,i));
}

//test enum
TrafficLight currentLight = YELLOW;
printf("current light is:%d",currentLight);

// struct Student student;
// student.age = 15;
// student.sex = 1;
// lastName是char[10], c语言中数组不能整体赋值, 所以下面的赋值是错误的
// student.lastName = {'x','y','z'};
// student.firstName = {'a','b','c','d'};

//下面是结构体的几种赋值
struct Student student = {15,1,"y","p"};
//使用strcpy赋值
struct Student student2;
strcpy(student2.firstName,"p");
//或者如下
student2.lastName[0] = 'c';
student2.lastName[1] = 'h';
student2.lastName[2] = 'e';
printf("%s %s\n",student.firstName,student.lastName);
printf("%s %s\n",student2.firstName,student2.lastName);

// 字符串反转
char *sArray = "abcdefg";
//获取长度这里问题就来了,如果使用sizeof(s) / sizeof(s[0]); 这种获取的是包含结尾\0的
//如果通过strlen(s); 那么是不包含\0的
if ((sArray!=NULL))
{
//下面计算长度这个写法是错误的
//数组 → 可以用 sizeof 计算长度
// 指针 → 必须用 strlen(s) 来计算字符串长度
// int sLength = sizeof(sArray)/sizeof(sArray[0]) - 1; // -1移除\0
int sLength = strlen(sArray);
char *sResult = reverseChars(sArray,sLength);
printf("倒叙结果:%s\n",sResult);
free(sResult);
}
return 0;
}