C语言

指针

#include <stdio.h>

void f(int *p);
int main(void)
{
    int i = 6;
    printf("&i=%p\n", &i);
    f(&i);
}

void f(int *p)
{
    printf("p=%p\n", p);
}

 

 

 

指针与const

1.指针是const

  • 表示一旦得到了某个变量的地址,不能再指向其他变量
int *const q = &i;  //q是const
*q = 26;     //OK
q++; //ERROR
  • 表示不能通过这个指针去修改那个变量(并不能使得那个变量成为const)
const int *p = &i;  
*p =26;       //ERROR
i = 26;       //OK
p = &j;    //OK
  • 这些是啥意思?
int i;
const int* p1 = &i;
int const* p2 = &i;
int *const p3 = &i;

判断哪个被const 了的标志是const在*的前面还是后面

const在*前面表示它所指的东西不能被修改。

const在*后面表示指针不能被修改。

 

 

指针运算

 

*p++

  • 取出p所指的那个数据来,完事之后顺便把p移到下一个位置去
  • *的优先级虽然高,但没有++高
  • 常用于数组类的连续空间操作
  • 在某些CPU上,这可以直接被翻译成一条汇编指令

用指针干啥

  • 需要传入较大的数据时用作参数
  • 传入数组后对数组做操作
  • 函数返回不止一个结果
  • 需要用函数来修改不止一个变量
  • 动态申请的内存...

 

动态内存分配

malloc返回的是void

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number;
    int *a;
    int i;
    printf("输入数量:");
    scanf("%d", &number);
    // int a[number];
    a = (int *)malloc(number * sizeof(int));
    for (i = 0; i < number; i++)
    {
        scanf("%d", &a[i]);
    }
    for (i = number - 1; i >= 0; i--)
        printf("%d ", a[i]);
    free(a);

return 0;
}

 

字符串变量

指针还是数组?

char *str= "hello";

char word[ ] = "hello";

  • 数组:这个字符串在这里

-作为本地变量空间自动被回收

  • 指针:这个字符串不知道在哪里

-处理参数

-动态分配空间

如果要构造一个字符串->数组

如果要处理一个字符串->指针

 

char*是字符串?

  • 字符串可以表达为char*的形式
  • char*不一定是字符串 --本意是指向字符的指针,可能指向的是字符的数组(就像int*一样)--这里有一个指针,这个指针指向一个字节或者一串连续的字节

只有它所指的字符数组组有结尾的0,才能说它所指的是字符串。

 

字符串数组

int main(void)
{
  a[0]-->char[10]
  char a[][10]={
    "Hello",
    "World",
    "shwjhfiuwhfi"
  };
  return 0;
}
int main(void)
{
  a[0]-->char *
  char *a[]={
    "Hello",
    "World",
    "shwjhfiuwhfi"
  };
  return 0;
}

 

字符串函数strlen

#include <stdio.h>
size_t mylen(char *s)
{
    int index = 0;
    while (s[index] != '\0')
    {
        index++;
    }
    return index;
}
int main()
{
    char line[] = "Happy";
    printf("strlen = %lu\n", mylen(line));
    printf("sizeof = %lu\n", sizeof(line));

    return 0;
}

 

 

 

字符串函数strcmp

#include <stdio.h>
#include <string.h>

int mycmp(const char *s1, const char *s2)
{
    // int idx = 0;
    // while (s1[idx] == s2[idx] && s1[idx] != '\0')
    // {
    //     idx++;
    // }
    //return s1[idx] - s2[idx];

    while(*s1 == *s2 && *s1 != '\0')
    {
        s1++;
        s2++;
    }
    return *s1 - *s2;
}
int main(int argc, char const *argv)
{
    char s1[] = "abc";
    char s2[] = "Abc";
    printf("%d\n", mycmp(s1, s2));
}

 

字符串函数strcpy

  • char * strcpy(char *restrict dst, const char *restrict src);
  • 把src的字符串拷贝到dst
  • restrict表面src和dst不重叠(c99)
  • 返回dst
  • 为了能链起代码来
#include <stdio.h>
char* mycpy(char *dst,const char *src)
{
    // int idx = 0;
    // while(src[idx]){
    //     dst[idx] = src[idx];
    //     idx++;
    // }
    // dst[idx] = '\0';

    
} 

int main(int argc, char const *argv[])
{
    char s1[] = "abc";
    char s2[] = "abc";
    strcpy(s1, s2);
}

 

 

 

枚举

枚举是一种用户定义的数据类型,他用关键字 enum 以如下语法来声明:

enum 枚举类型名字 {名字0,名字1,名字n};

enum colors {red, yellow, green};

就创建了三个常量,red 的值是0,yellow是1,而green是2,它们的类型是int,值依次从 0 到 n 。

 

posted on 2022-06-17 10:56  ccxwyyjy  阅读(68)  评论(0)    收藏  举报