demo_3_9
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 char *getmemory(void) 7 { 8 char p[] = "hello world"; 9 return p; 10 } 11 void test(void) 12 { 13 char *str = NULL; 14 str = getmemory(); 15 printf(str); 16 } 17 int main() 18 { 19 test(); 20 system("pause"); 21 return 0; 22 //非法访问内存 23 } 24 25 int* test() 26 { 27 static int a = 10;//静态区 static修饰局部变量时生命周期变长了 28 int a = 10;//栈区 29 return &a; 30 } 31 int main() 32 { 33 int*p = test(); 34 *p = 20;//error 非法访问内存 35 system("pause"); 36 return 0; 37 } 38 39 int* test() 40 { 41 int *ptr = malloc(100);//堆区 只有free后才回收/释放 42 return ptr; 43 } 44 int main() 45 { 46 int *p = test(); 47 system("pause"); 48 return 0; 49 } 50 51 //运行test会出现什么结果? 52 void getmemory(char **p, int num) 53 { 54 *p = (char *)malloc(num); 55 } 56 void test(void) 57 { 58 char *str = null; 59 getmemory(&str, 100); 60 strcpy(str, "hello"); 61 printf(str); 62 free(str);//动态释放内存空间 63 str = null; 64 //忘记释放动态开辟的内存,导致内存泄漏 65 } 66 int main() 67 { 68 test(); 69 return 0; 70 //输出hello 存在内存泄漏 71 } 72 73 void test(void) 74 { 75 char *str = (char *)malloc(100); 76 strcpy(str, "hello"); 77 free(str);//free释放str指向的空间后,并不会把str置为null 78 str = null;//把str置为空指针 79 //对下面的指针判断才有意义 80 if (str != null) 81 { 82 strcpy(str, "world"); 83 printf(str); 84 } 85 } 86 int main() 87 { 88 test(); 89 system("pause"); 90 return 0; 91 //输出world 非法访问 程序崩溃 92 } 93 94 struct s 95 { 96 int n; 97 int arr[10];//未知大小 - 柔性数组成员 98 //结构的最后一个元素 99 }; 100 int main() 101 { 102 //struct s s; 103 //printf("%d\n", sizeof(s)); 104 struct s* ps = (struct s*)malloc(sizeof(struct s) + 5 * sizeof(int)); 105 ps->n = 100; 106 int i; 107 for (i = 0; i < 5; i++) 108 { 109 ps->arr[i] = i;//0 1 2 3 4 110 } 111 struct s* ptr = realloc(ps, 44); 112 if (ptr != null) 113 { 114 //开辟成功后就使用它 115 ps = ptr; 116 } 117 for (i = 5; i < 10; i++) 118 { 119 ps->arr[i] = i; 120 } 121 for (i = 0; i < 10; i++) 122 { 123 printf("%d", ps->arr[i]); 124 } 125 //释放空间 126 free(ps); 127 ps = null; 128 129 system("pause"); 130 return 0; 131 } 132 133 struct s 134 { 135 int n; 136 int *arr; 137 }; 138 int main() 139 { 140 struct s*ps = (struct s*)malloc(sizeof(struct s));//创建结构体大小 141 ps->arr = malloc(5 * sizeof(int)); 142 int i = 0; 143 for (i = 0; i < 5; i++) 144 { 145 ps->arr[i] = i; 146 } 147 for (i = 0; i < 5; i++) 148 { 149 printf("%d ", ps->arr[i]); 150 } 151 //调整大小 152 int *ptr = realloc(ps->arr, 10 * sizeof(int)); 153 if (ptr != null) 154 { 155 ps->arr = ptr; 156 } 157 for (i = 5; i < 10; i++) 158 { 159 ps->arr[i] = i; 160 } 161 for (i = 0; i < 10; i++) 162 { 163 printf("%d ", ps->arr[i]); 164 } 165 free(ps->arr); 166 ps->arr = null; 167 free(ps); 168 ps = null; 169 system("pause"); 170 return 0; 171 } 172 173 文件操作 174 175 int main() 176 { 177 int a = 10000; 178 file* pf = fopen("test.txt", "wb"); 179 fwrite(&a, 4, 1, pf);//以二进制文件写 180 fclose(pf); 181 pf = null; 182 system("pause"); 183 return 0; 184 } 185 186 int main() 187 { 188 //打开文件test.txt 189 //相对路径 190 fopen("test.txt", "r"); 191 //绝对路径 192 fopen("e:\\c语言\\专升本c语言\\专练\\test_3_9\\test_3_9", "r"); 193 // .. 表示上一级路径 194 // . 表示当前路径 195 system("pause"); 196 return 0; 197 } 198 199 int main() 200 { 201 file* pf = fopen("test.txt", "r"); 202 //打开文件失败会返回一个空指针 203 if (pf == null) 204 { 205 printf("%s\n",strerror(errno)); 206 } 207 //打开成功 208 //读文件 209 //关闭文件,把资源释放掉 210 fclose(pf); 211 pf = null;//赋值给空指针 212 system("pause"); 213 return 0; 214 } 215 216 int main() 217 { 218 FILE *pfWrite = fopen("test2.txt", "w"); 219 if (pfWrite == NULL) 220 { 221 printf("%s\n", strerror(errno)); 222 return 0; 223 } 224 //写文件 225 fputc('b', pfWrite); 226 fputc('i', pfWrite); 227 fputc('t', pfWrite); 228 //关闭文件 229 fclose(pfWrite); 230 pfWrite = NULL; 231 } 232 233 int main() 234 { 235 FILE *pfRead = fopen("test2.txt", "r"); 236 if (pfRead == NULL) 237 { 238 printf("%s\n", strerror(errno)); 239 return 0; 240 } 241 //读文件 242 printf("%c ", fgetc(pfRead)); 243 printf("%c ", fgetc(pfRead)); 244 printf("%c ", fgetc(pfRead)); 245 //关闭文件 246 fclose(pfRead); 247 pfRead = NULL; 248 } 249 250 //从键盘输入 - 输出到屏幕 - 键盘&屏幕都是外部设备 251 //键盘 - 标准输入设备 - stdin 252 //屏幕 - 标准输出设备 - stdout 253 //是一个程序默认打开的两个流设备 254 //stdin FILE* 255 //stdout FILE* 256 //stderr FILE* 257 258 int main() 259 { 260 int ch=fgetc(stdin);//从标准输入流中写 261 fputc(ch, stdout);//从标准输出流中读 262 system("pause"); 263 return 0; 264 } 265 266 int main() 267 { 268 FILE* pf = fopen("test.txt", "r"); 269 if (pf == NULL) 270 { 271 printf("%s\n", strerror(errno)); 272 return 0; 273 } 274 //读文件 275 int ch = fgetc(pf); 276 printf("%c", ch); 277 ch = fgetc(pf); 278 printf("%c", ch); 279 ch = fgetc(pf); 280 printf("%c", ch); 281 //关闭文件 282 fclose(pf); 283 pf = NULL; 284 system("pause"); 285 return 0; 286 } 287 288 int main() 289 { 290 char buf[1024] = { 0 }; 291 //打开一个文件,模式为只读 292 FILE* pf = fopen("test.txt", "r"); 293 //如果为空则返回0 294 if (pf == NULL) 295 { 296 return 0; 297 } 298 //读文件 299 fgets(buf,1024,pf); 300 printf("%s\n", buf); 301 302 //关闭文件 303 fclose(pf); 304 //置空 305 pf = NULL; 306 system("pause"); 307 return 0; 308 } 309 310 int main() 311 { 312 char buf[1024] = { 0 }; 313 //打开一个文件,模式为只读 314 FILE* pf = fopen("test.txt", "r"); 315 //如果为空则返回0 316 if (pf == NULL) 317 { 318 return 0; 319 } 320 //读文件 321 fgets(buf, 1024, pf); 322 //puts会换行 323 puts(buf); 324 325 //关闭文件 326 fclose(pf); 327 //置空 328 pf = NULL; 329 system("pause"); 330 return 0; 331 } 332 333 //写一行数据 - 操作的是一行数据 334 int main() 335 { 336 char buf[1024] = { 0 }; 337 //打开一个文件,模式为只读 338 FILE* pf = fopen("test.txt", "r"); 339 //如果为空则返回0 340 if (pf == NULL) 341 { 342 return 0; 343 } 344 //写文件 345 fputs("hello\n", pf); 346 fputs("world\n", pf); 347 //关闭文件 348 fclose(pf); 349 //置空 350 pf = NULL; 351 system("pause"); 352 return 0; 353 } 354 355 int main() 356 { 357 //从键盘读一行文本信息 358 char buf[1024] = { 0 }; 359 fgets(buf,1024,stdin);//从标准输入流 360 fputs(buf, stdout);//从标准输出流 361 362 //等价于 363 gets(buf); 364 puts(buf); 365 system("pause"); 366 return 0; 367 } 368 369 //格式化输入输出函数 - fscanf - fprintf 370 struct S 371 { 372 int n; 373 float score; 374 char arr[10]; 375 }; 376 int main() 377 { 378 struct S s = { 100, 3.14f, "ding" }; 379 FILE* pf = fopen("test.txt", "w"); 380 //判断 381 if (pf == NULL) 382 { 383 return 0; 384 } 385 //以格式化的形式写文件 386 387 //关闭文件 388 fclose(pf); 389 //置空 390 pf = NULL; 391 system("pause"); 392 return 0; 393 }