TOPIC NOTES

C/C++基础之一

C/C++基础
文章目录
  1. C/C++基础之一(数据类型、数组、指针、结构体)
  2. Talk is cheap. Show me the code.

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

Talk is cheap. Show me the code.

  • 结构体定义
c
#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函数
c

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;
}

每一个不曾起舞的日子,都是对生命的一种辜负。

继续阅读C++