C/C++学习记录

版权声明:文章只是自己学习的记录,本人不能保证全部正确。希望大神多多指点,帮助菜鸟进步。 https://blog.csdn.net/Emptynest/article/details/78147358

第一部分 C语言基础

字符串库函数

gets(char * _str)

char str[100] = {0};
gets(str);//结束标志:回车(不是空格)
/*
    存在缓冲区溢出的问题
*/

fgets()

char str[100] = {0};
fgets(str, sizeof(str),stdin);

puts()

char str[100] = "hello c";
puts(str);
/*
    puts会自动添加一个'\n',输出到屏幕上
*/

fputs()

char str[100] = "hello c";
fputs(str, stdout);

strlen()

返回不包含字符串结尾’\0’的长度

char str[100] = "hello c";
int len = strlen(str);
printf("%d\n", len);//7

strcat(char * _str1, const char * _str2);

将字符串_str2追加到 _str1后面

char str1 = "heoll ";
char str2 = "world";
strcat(str1, str2);
printf("%s\n",str1);
/*
    strcat存在缓冲区溢出的问题
*/

strncat(char * _str1, const char * _str2, unsigned int len)

将字符串_str2前len个字符追加到 _str1后面

char str1 = "heoll ";
char str2 = "world";
strcat(str1, str2,3);
printf("%s\n",str1);

strcmp(const char * _str1, const char * _str2)

char str1[100] = "bbb";
char str2[100] = "aaa";
int  a = strcmp(str1, str2);
printf("%d\n", a);//1

strcpy(str2,"bbb");
a = strcmp(str1, str2);
printf("%d\n",a);//0

strcpy(str2,"ccc");
a = strcmp(str1, str2);
printf("%d\n",a);//-1

strncmp(const char* _str1, const char * _str2, size_t len)

char str1[100] = "aaa";
char str2[100] = "bbbbb";
int a = strncmp(str1, str2, 3);

strcpy(char * _str1, const char * _str2)

将参数_str2拷贝到 _str1,

char str1[100] = {0};
char str2 = "hello c";
strcpy(str1,str2);
printf("%s\n",str1);//"hello c"

strncpy(char * _str1, const char * _str2, size_t len)

char str1[100] = {0};
char str2 = "hello c";
strncpy(str1,str2,5);
printf("%s\n",str1);//"hello"

sprintf()

将格式化结果输出到字符串中

int i = 200;
char str[100] = {0};
sprintf(str, "i = %d", i);
printf("%s\n", str);//"i = 200"

sscanf()

从指定格式化字符串读取输入,而scanf是从键盘中读取输入

char str[100] = "60+50=";
int a = 0;
int b = 0;
sscanf(str,"%d+%d=",&a,&b);
printf("a + b = %d\n", a + b);//"a + b = 110"

strchr(char * _str, int _ch)

在参数_str中查找参数 _ch指定字符,找到返回字符 _ch在 _str中所在位置,没有找到返回NULL

char ch = 'o';
char str[100] = "hello c";
char *p = strchr(str, ch);
printf("%s\n",p);//"o c"

strstr(char * _str, const char * _substr)

在参数_str中查找参数 _substr指定子串,找到返回子串在 _str中所在位置,没有找到返回NULL

char str2 = 'll';
char str1[100] = "hello c";
char *p = strstr(str1, str2);
printf("%s\n",p);//"llo c"

strtok(char * _str1, char * _str2)

字符在第一次调用时strtok()必需给予参数_str字符串,往后的调用则将参数 _str设置成NULL每次调用成功则返回指向被分割出片段的指针

char buf[] = "abc@defg@igk";
char *p = strtok(buf, "@");
while(p)
{
    printf("%s\n",p);
    p = strtok(NULL,"@");
}

atoi(const char * _str)、atof(const char * _str)、atol(const char * _str)

需要包含头文件stdlib.h,atoi是c语言的标准库函数,itoa不是

int a = atoi("123");
printf("%d\n",a);

子函数中申请堆的错误方法

//错误方法
#include<stdio.h>
#include<stdlib.h>

void getheap(int *p)
{
    p = (int*)malloc(sizeof(int)*10);
}
int main()
{
    int *p = NULL;
    int i = 0;
    getheap(p);//p的生命周期在getheap运行完就结束了
    for(i = 0; i<10;i++)
    {
        p[i] = i;
    }
    for(i = 0; i < 10; i++)
    {
        printf("%d\n", p[i]);
    }
    free(p);
    return 0;
}
//正确的方法
#include<stdio.h>
#include<stdlib.h>

void getheap(int **p)
{
    *p = (int*)malloc(sizeof(int)*10);
}
int main()
{
    int *p = NULL;
    int i = 0;
    getheap(&p);//通过指针间接改变变量的值
    for(i = 0; i<10;i++)
    {
        p[i] = i;
    }
    for(i = 0; i < 10; i++)
    {
        printf("%d\n", p[i]);
    }
    free(p);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Emptynest/article/details/78147358