
c基础
lpl312905509
这个作者很懒,什么都没留下…
展开
-
c语言将int型数据写到char数组 或将char数组数据写到int
int main(){ unsigned int a = 65535; char* b = (char*)&a; printf("%d\n", (unsigned char)(*(b))); printf("%d\n", (unsigned char)(*(b + 1))); printf("%d\n", (unsigned char)(*(b + 2))); printf("%d\n", (unsigned char)*(b + 3)); unsigned char buff[4原创 2020-07-13 23:22:46 · 4696 阅读 · 1 评论 -
对于makefile不支持c++11的处理
错误信息error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compil...原创 2020-02-18 18:02:03 · 2467 阅读 · 0 评论 -
C实现删除指定长度的字符
char* deleteChars(char* str, int pos, int len){ char *p = str + pos - 1; //指向pos位置字符 int tt = strlen(str); //计算字符长度 if ((pos < 1) || (p - str) > tt) //检查pos是否大于1 检查pos超出字符串长度 {...原创 2020-01-26 21:59:19 · 1010 阅读 · 0 评论 -
C编程实现字符串的循环右移
void loopMove(char* str,int n){ int i = 0; char* temp = NULL; int strLen = 0; char *head = str; //指向字符串头部 while (*str++); strLen = str - head - 1; //计算字符串长度 n = n % strLen; //计算字符串尾部...原创 2020-01-26 21:47:36 · 544 阅读 · 0 评论 -
C编程实现字符串的替换
const char* findStr(const char* src, const char* sub){ const char* bp; const char* sp; if (src == NULL || sub == NULL) return NULL; while (*src != '\0') //遍历src字符串 { bp = src; //用于src的遍历 ...原创 2020-01-26 18:43:54 · 423 阅读 · 0 评论 -
找出01字符串中0和1连续出现的最大次数
void calculate(const char* str,int* max0,int * max1){ int temp0 = 0; //保存连续是'0'的最大长度 int temp1 = 0; //保存连续是'1'的最大长度 while (*str) //遍历01字符串 { if (*str == '0') //当前字符串是'0' { (*max0)++; i...原创 2020-01-26 17:34:04 · 686 阅读 · 0 评论 -
C实现库函数strcat
char* strcat(char* dest,const char* src){ char* ret; ret = dest; //保存目的字符串首地址以便返回 while (*dest++); dest--; while (*dest++ = *src++); return ret;}int main(){ char dest[100] = "hello"; ...原创 2020-01-26 16:01:31 · 148 阅读 · 0 评论 -
C编程实现删除字符串中所有指定的字符
void deleteChar(char* str, char deChar){ char* p = NULL; if (str == NULL) return; p = str; while (*p) { if (*p != deChar) { *str++ = *p; } p++; } *str = '\0';}int main(){ ...原创 2020-01-26 15:54:56 · 1204 阅读 · 0 评论 -
C编程实现两个字符串正整数相加
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>char* addBitInt(const char* num1, const char* num2){ int c = 0; //进位,开始最低进位为0 int length1 = st...原创 2020-01-26 09:38:14 · 2495 阅读 · 0 评论 -
C函数strrev实现字符串反转
// 第一个与倒数一个个交换 第二个与倒数第二个交换 以此类推//方法1:数组操作void strrev1(const char* str,char* buffer){ int len = strlen(str); strcpy(buffer,str); for (int i = 0; i < len / 2; ++i) { char c = buffer[i]; ...原创 2020-01-25 14:55:49 · 744 阅读 · 0 评论 -
C编程实现反转字符串,但其指定的子串不反转
给定一个字符串、一个字符串的子串,将第一个字符串反转,但保留子串的顺序不变例如:输入:第一个字符串: “Welcome you, my friend”子串:“you”输出:“dneirf ym,you emocleW”解析(1)扫描一遍第一个字符串,然后用stack把它反转,同时记录下子串的位置。(2)扫描一遍把记录下来的子串再用stack反转(3)将堆栈的字符串弹出,这样子串又恢...原创 2020-01-25 14:08:25 · 375 阅读 · 0 评论 -
C编程实现转换字符串、插入字符的个数
#include <stdlib.h>#include <stdio.h>#include <string>char* transfromion(char* str){ int len = strlen(str); char* buffer = new char[len + 1]; char* p = str; char* q = p + 1...原创 2020-01-24 22:53:00 · 135 阅读 · 0 评论 -
C不使用printf,将十进制数以二进制形式和十六进制的形式输出
void get2string(int num,char* buffer){ int i = 0; for (i = 0; i < 32; ++i) { buffer[i] = num & (1 << (31 - i)); buffer[i] = buffer[i] >> (31 - i); buffer[i] = buffer[i] =...原创 2020-01-24 21:27:12 · 565 阅读 · 0 评论 -
C函数查找两个字符串的最大公共子串
//对于两个字符串A和B,如果A = "aocdfe",B = "pmcdfa",则输出"cdf"const char* findStr(const char* src, const char* sub){ const char* bp; const char* sp; if (src == NULL || sub == NULL) return NULL; while (*sr...原创 2020-01-24 17:39:38 · 1648 阅读 · 0 评论 -
C函数strcmp实现
int strcmp(const char* src, const char* dst){ int ret = 0; while (!(ret = *src - *dst) && *dst) { ++src; ++dst; } if (ret < 0) ret = -1; else if (ret > 0) ret = 1; retu...原创 2020-01-24 15:55:10 · 293 阅读 · 0 评论 -
C函数判断是否是回文字符串
int isHuiWenStr(const char* str){ int i, len; int found = true; if (str == NULL) return -1; len = strlen(str); for (i = 0; i < len / 2; i++) { if (*(str + i) != *(str + len - i - 1)) ...原创 2020-01-24 15:30:21 · 485 阅读 · 0 评论 -
C编程实现字符串中各单词的翻转
//字符串中各单词的翻转void reverseStr1(char* src){ char* start = src, *end = src, *ptr = src; while (*ptr++ != '\0') { if (*ptr == ' ' || *ptr == '\0') //找到一个单词 { end = ptr - 1; //end指向单词的结尾 ...原创 2020-01-24 11:03:21 · 395 阅读 · 0 评论 -
C编程实现字符串中子串的查找
//编程实现字符串中子串的查找const char* findStr(const char* src,const char* sub){ const char* bp; const char* sp; if (src == NULL || sub == NULL) return NULL; while (*src != '\0') //遍历src字符串 { bp = src...原创 2020-01-24 10:32:34 · 917 阅读 · 0 评论 -
c函数memcpy实现
void *memcpy(void* memTo, const void* memFrom, size_t size){ assert((memTo != NULL)&&(memFrom != NULL)); //memTo和memFrom必须有效 char* tempFrom = (char*)memFrom; //保存memFrom首地址 char* tempTo =...原创 2020-01-23 22:59:40 · 266 阅读 · 0 评论 -
c函数strcpy实现
char *strcpy(char* strDest, const char* strSrc){ if ((strDest == NULL) || (strSrc == NULL)) { return NULL; } char * strDestCopy = strDest; //保存目标字符串首地址 while ((*strDest++ = *strSrc++) != 0);...原创 2020-01-23 22:48:16 · 223 阅读 · 1 评论 -
c函数itoa和atoi实现
#include <stdio.h>int atoi( char *string){ int sum = 0; int sign = 1; for (int i = 0; string[i] != '\0'; i++) { if (string[i] >= '0' && string[i] <= '9') //去掉一些特殊符号 { ...原创 2020-01-20 11:33:53 · 142 阅读 · 0 评论 -
C/C++实现位操作函数
```cpp#define MAKEINT16(a, b) ((uint16_t)(((uint8_t)((uint16_t)(a) & 0xff)) | ((uint16_t)((uint8_t)((uint16_t)(b) & 0xff))) << 8))#define MAKEINT32(a, b) ((uint32_t)(((uint16_t)((ui...原创 2019-10-18 14:24:10 · 934 阅读 · 0 评论 -
memmove 和 memcpy的区别以及处理内存重叠问题
https://blog.youkuaiyun.com/Li_Ning_/article/details/51418400?tdsourcetag=s_pcqq_aiomsg转载 2019-07-13 13:59:20 · 171 阅读 · 0 评论 -
一级指针的简单运算代码及案例
#include <stdio.h>#include <iostream>//指针 类型(步长)+地址(物理类型)// int 数值+1,就是加1// T *指针 +1,加的是步长,sizeof(T)大小using namespace std;int main(){ int *p = (int*)0x0001; //十六进制数 int data = ...原创 2019-06-26 23:42:13 · 160 阅读 · 0 评论 -
二维数组代码及案例
#include <stdio.h>#include <iostream>using namespace std;//二维数组的本质也是一维数组,只不过,一维数组中的每一个元素,又是一个一维数组而已// int[4] array[3] ==> int array[3][4]// 关键在于 牢记 A[x] 等价于 *(A+x)int main()...原创 2019-06-26 10:57:49 · 1348 阅读 · 0 评论 -
指针变量代码及案例
#include<stdio.h>#include<iostream>using namespace std;//声明一个指针变量,必须要保存两种东西,一个地址数据,而是类型//type * pointerName// *表明了本变量为指针变量,大小固定,此处的*仅用于表示声明// 类型决定了该变量中存储的地址的寻址能力int main(){ in...原创 2019-06-26 10:10:43 · 311 阅读 · 0 评论 -
c计算utf8字符串的长度,中文算一个字符
#includeusing namespace std;int getStrLenUtf8(const char* str){if (!str) return 0;int len = (int)strlen(str);int ret = 0;for (const char* sptr = str; (sptr - str) < len && *sptr;)...原创 2019-06-29 09:45:51 · 1740 阅读 · 0 评论 -
二维数组的使用分析及案例
#include <stdio.h>#include <iostream>using namespace std;//a->a[i] 可以使用*(a+i) 取内容 内容是一块连续的内存//a ->a[i][j] 可以使用 *(*(a+i)+j) 连续的内存可以++操作//a[i]->a 可以使用&a[i]int main(...原创 2019-07-14 17:56:21 · 584 阅读 · 0 评论 -
gcc编译的四个阶段
gcc编译的四个阶段如下图:参数意义:-E Preprocess only; do not compile, assemble or link;只预处理,不会编译、汇编、链接-S Compile only; do not assemble or link;只编译,不会汇编、链接-c Compile and assemble, but do not link; 编译和汇...原创 2019-09-04 23:42:08 · 3447 阅读 · 0 评论 -
linux 静态库的制作
1、静态库的制作1)命名规则 lib + 库的名字 + .a 例如 libmytest.a 库的名字就是mytest2)制作步骤 生成对应的.o文件 .c 文件生成 .o 文件 添加 -c参数 然后将生成的.o文件打包 ar rcs + 静态库的名字(libmytest.a) + 生成的所有的.o 文件首先创建好如下的目录结构://main.c#include &...原创 2019-09-10 23:19:38 · 654 阅读 · 0 评论 -
C/C++ 实现随机数
#include <iostream>#include <map>#include <stdlib.h> #include <time.h>unsigned int getRandomNumber(int nModule){ return rand() % nModule;}unsigned int getRandomNumber...原创 2019-09-23 14:07:47 · 263 阅读 · 0 评论 -
C/C++ 二维数组作为函数参数传递都会退化为指针
void printArray(int *array, int high, int wide){ for (int i = 0; i < high; ++i) { for (int j = 0; j < wide; ++j) { cout << *(array + i*wide + j) << " "; } cout <<...原创 2019-09-24 14:07:02 · 544 阅读 · 0 评论 -
指针常量与常量指针的区别
#include <stdio.h>#include <iostream>//指针常量->指针变量//对变量取地址,取出的地址,就是一个指针,且常量指针//常量指针 不是一个单纯的地址 而是有类型的//指针的本质,是一个有类型的地址.类型决定了,从这个地址开始寻址的能力.//& * reference derefer 取地址 取内容in...原创 2019-06-26 09:43:51 · 309 阅读 · 0 评论