打赏

4.0-day10-C语言基础-指针和数组

C语言基础-指针和数组

 
指针变量可以作为函数返回值使用,只能把全局变量的地址,静态局部变量的地址或者通过参数得到的地址当作返回值使用,普通局部变量的地址绝对不可以当返回值使用。
 
字符串是由内存中一组连续的字符变量构成的。C语言程序中使用第一个字符的地址表示整个字符串。'\0'是字符串的结尾字符,它的位置决定了一个字符串中有效字符变量的个数。这个字符在ASCII表中对应数字0。
 
字面值是程序中表示字符串的一种写法,用双引号表示。字面值表示的字符串不可以被修改。多个相同内容的字面值在程序运行时都是同一个。多个连续的字符串字面值在编译时会被合并成一个。
 
字符数组也可以表示字符串,它存储在栈中。它可以被修改,可以使用多种方式进行初始化。
 
fgets函数可以用来从键盘读取字符串,不会出现字符数组溢出问题。
 
strlen函数可以计算字符串中有效字符的个数
strcat函数可以把两个字符串合并,可能造成数组溢出。使用strncat函数可以避免溢出。
strcpy函数可以把一个字符串赋值成另一个字符串。使用strncpy函数可以避免溢出。
strcmp函数可以比较两个字符串的大小关系。strncmp的函数比较两个字符串中前面几个字符的大小。
 
预处理指令#define可以用于实现宏定义,宏可以用来当作数字的名称。
可以在gcc命令行中使用-D选项定义宏
 
宏可以带参数,宏的参数都既是输入参数也是输出参数
 
宏的所有参数在使用的时候要用小括号包括起来
宏的计算结果要用小括号包括起来
不要使用自增或自减的计算结果作为宏的参数使用
 
#操作符可以把宏的某个参数变成字符串字面值
##操作符可以把宏的某个参数代表的标示符和其他内容合并成为一个新的标示符
 
编译器内部提供了一些预定义的宏,在编译的时候编译器把他们替换成正确的内容
 
使用如下预处理指令可以实现条件编译
#ifdef   宏名称  (#ifndef     宏名称)
    ....
#else
....
#endif
其中#ifdef和#ifndef意思相反
 
使用#if和#elif可以根据任何逻辑表达式实现条件编译,并且可以从更多语句组中进行选择。
 

01ptr.c
  1. /*
  2. 指针练习
  3. */
  4. #include<stdio.h>
  5. int*func(){
  6. staticint value;
  7. printf("value是%d\n", value);
  8. return&value;
  9. }
  10. int main(){
  11. int*p_value = NULL;
  12. p_value = func();
  13. *p_value =7;
  14. func();
  15. return0;
  16. }
 

02str.c
  1. /*
  2. 字符串练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. char*p_str ="abc";
  7. char ch ='x';
  8. printf("%p\n","abc");
  9. printf("%c\n",*p_str);
  10. printf("%s\n", p_str);
  11. p_str =&ch;
  12. printf("%s\n", p_str);//不可以当字符串使用
  13. return0;
  14. }

03str.c
  1. /*
  2. 字符串练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. char*p_str ="abcdef";
  7. char str[]="abc";
  8. char str1[3]="abc";//错误
  9. char str2[]={'a','b','c'};//错误
  10. char str3[]={'a','b','c',0};
  11. //*p_str = 'x'; 字符串字面值不可以修改
  12. printf("%p\n","abcdef");
  13. printf("p_str是%p\n", p_str);
  14. printf("%s\n","abc""xyz");
  15. printf("%s\n", str);
  16. printf("字符变量个数是%d\n",sizeof(str)/sizeof(str[0]));
  17. str[0]='y';
  18. str[1]=0;
  19. printf("%s\n", str);
  20. printf("%s\n", str2);
  21. return0;
  22. }
 
 

04str.c
  1. /*
  2. 字符串练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. char buf[10]={0};
  7. printf("请输入一个字符串:");
  8. //scanf("%s", buf);
  9. fgets(buf,10, stdin);
  10. printf("%s\n", buf);
  11. return0;
  12. }
 

05str.c
  1. /*
  2. 字符串函数练习
  3. */
  4. #include<stdio.h>
  5. #include<string.h>
  6. int main(){
  7. char str[20]="abc";
  8. printf("长度是%d\n", strlen("abcdef"));
  9. printf("合并后是%s\n", strcat(str,"xyz"));
  10. printf("赋值后字符串是%s\n", strcpy(str,"uvw"));
  11. printf("比较结果是%d\n", strcmp("abc","abd"));
  12. return0;vi
  13. }
 

06login.c
  1. /*
  2. 模拟登陆练习
  3. */
  4. #include<stdio.h>
  5. #include<string.h>
  6. int main(){
  7. int loop =0;
  8. char buf[10];
  9. for(loop =0; loop <3; loop++){
  10. printf("请输入用户名:");
  11. fgets(buf,10, stdin);
  12. if(strcmp(buf,"admin\n")){
  13. continue;
  14. }
  15. printf("请输入密码:");
  16. fgets(buf,10, stdin);
  17. if(!strcmp(buf,"123456\n")){
  18. break;
  19. }
  20. }
  21. if(loop <3){
  22. printf("登陆成功\n");
  23. }
  24. else{
  25. printf("登陆失败\n");
  26. }
  27. return0;
  28. }
 
 
 

 
07main.c
  1. /*
  2. main函数的参数
  3. */
  4. #include<stdio.h>
  5. int main(int argc,char*argv[]){
  6. int loop =0;
  7. for(loop =0; loop <= argc -1; loop++){
  8. printf("%s\n", argv[loop]);
  9. }
  10. return0;
  11. }
 

08define.c
  1. /*
  2. 宏练习
  3. */
  4. #include<stdio.h>
  5. #define PI 3.14f
  6. #define CIRCLE(r)2* PI * r
  7. #define AREA(r) PI * r * r
  8. int main(){
  9. int radius =0;
  10. printf("请输入半径:");
  11. scanf("%d",&radius);
  12. printf("圆的周长是%g\n", CIRCLE(radius));
  13. printf("圆的面积是%g\n", AREA(radius));
  14. return0;
  15. }
 
 %g用来输出实数,它根据数值的大小,自动选f格式或者e格式
 

09macro.c
  1. /*
  2. 宏练习
  3. */
  4. #include<stdio.h>
  5. #define NEG(n) n =0- n
  6. void neg(int value){
  7. value =0- value;
  8. }
  9. int main(){
  10. int value =4;
  11. //neg(value);
  12. NEG(value);
  13. printf("value是%d\n", value);
  14. return0;
  15. }
 
 
 函数没有返回值,返回的是局部变量的值;因为是
void neg(int value)

10max.c
  1. /*
  2. 宏练习
  3. */
  4. #include<stdio.h>
  5. #define MAX(n,m) n > m ? n : m
  6. int main(){
  7. printf("MAX(4, 8)是%d\n", MAX(4,8));
  8. return0;
  9. }
 

11macro.c
  1. /*
  2. 宏练习
  3. */
  4. #include<stdio.h>
  5. #define SECPH (60*60)
  6. #define NEG(n)0-(n)
  7. #define SQUARE(n)((n)*(n))
  8. int main(){
  9. int value =4;
  10. printf("小时数是%d\n",7200/ SECPH);
  11. printf("NEG(3)是%d\n", NEG(3));
  12. printf("NEG(3 + 8)是%d\n", NEG(3+8));
  13. printf("SQUARE(4)是%d\n", SQUARE(4));
  14. printf("SQUARE(++value)是%d\n", SQUARE(++value));
  15. return0;
  16. }
 
 注意:SQUARE(++value)要执行两次的n*n,相当于4加了两次变为6,6*6=36;
 

12macro.c
  1. /*
  2. 宏练习
  3. */
  4. #include<stdio.h>
  5. #define STR(n)#n
  6. #define PTR(n) p_##n
  7. int main(){
  8. int*PTR(value)= NULL;
  9. printf("STR(3 + 6)是%s\n", STR(3+6));
  10. return0;
  11. }
 
 
 #n变成字符串的字面值,不会执行参数里面的内容。
 

13macro.c
  1. /*
  2. 预定义宏练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. printf("行号是%d\n", __LINE__);
  7. printf("文件名是%s\n", __FILE__);
  8. printf("日期是%s\n", __DATE__);
  9. printf("时间是%s\n", __TIME__);
  10. printf("%sC标准\n", __STDC__ ?"符合":"不符合");
  11. return0;
  12. }
 
14compile.c
  1. /*
  2. 条件编译练习
  3. */
  4. #include<stdio.h>
  5. //#define ONE
  6. //#define TWO
  7. int main(){
  8. //#ifdef ONE
  9. #ifndef TWO
  10. printf("1\n");
  11. #else
  12. printf("2\n");
  13. #endif
  14. return0;
  15. }
希望明确规定转出的数字是几---宏;
不关心转出的数字是几---枚举;
一旦开始使用堆中的变量,必须保证适合的时候只消灭一次;
 
 
 
 

15order.c
  1. /*
  2. 点菜练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. int order =0;
  7. printf("请点菜:");
  8. scanf("%d",&order);
  9. //#ifdef ZHAOBENSHAN
  10. #if defined(ZHAOBENSHAN)
  11. if(1== order){
  12. printf("这个没有\n");
  13. }
  14. elseif(2== order){
  15. printf("这个真没有\n");
  16. }
  17. #else
  18. if(1== order){
  19. printf("这个菜有\n");
  20. }
  21. elseif(2== order){
  22. printf("这个菜没有\n");
  23. }
  24. #endif
  25. return0;
  26. }
 
 
 
 
 

16discount.c
  1. /*
  2. 条件编译练习
  3. */
  4. #include<stdio.h>
  5. int main(){
  6. #if defined(ELITE)
  7. printf("120%%\n");
  8. #elif!defined(FACTORY)
  9. printf("100%%\n");
  10. #else
  11. printf("80%%\n");
  12. #endif
  13. return0;
  14. }
#ifndef 在此之前没有定义过该宏(条件编译)
#elif !defined(FACTORY)在此之前没有定义过FACTORY这个宏。
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





posted on 2018-11-29 22:50  XuCodeX  阅读(248)  评论(0)    收藏  举报

导航