
C
zxy131072
嵌入式linux
展开
-
C中计算某段代码运行时间框架
struct timeval start, end; gettimeofday(&start, NULL); /** to calculate code */gettimeofday(&end, NULL); long long total_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // get the run time by microsecondp原创 2020-10-28 15:55:21 · 188 阅读 · 0 评论 -
C实现从终端读入一行字符串,并计算其包含的单词个数
#include <stdio.h>/* 定义一个宏, 用来标记缓冲区长度 */#define BUFF_SIZE 1024#define F_SPACE 1 /* 标志位宏,标记空格 */#define F_LETTER 0 /* 标志位宏,标记字母 *//* 定义一个宏函数,用来判断是否是英文字母(包含大小写) */#define is_letter(_ch_) (_ch_ >= 'a' &&原创 2020-09-22 09:56:58 · 616 阅读 · 0 评论 -
C实现将两个字符串合并
#include <stdio.h>#define BUFF_SIZE (1024)int merge_data(char *dest,char *src1, char *src2){ if (NULL == dest || NULL == src1 || NULL == src2) return -1; while ('\0' != *src1 && '\0' != *src2) {#if 0 if(*src1 < *src2原创 2020-09-22 09:41:32 · 2358 阅读 · 0 评论 -
C实现统计C文件行数
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <dirent.h>#include <errno.h>#include <error.h>#include <string.h原创 2020-09-22 09:18:10 · 716 阅读 · 0 评论 -
C字符串拷贝字符串连接测试字符串长度自实现
#include <stdio.h>unsigned int mystrlen(char *src){ if(src == NULL) return 0;#if 1 char *head = src; while(*src != '\0') src ++; return src -head;#else int len = 0; while(*(src + len ++) != '\0'); return len - 1;#endif}char * my原创 2020-09-21 13:09:37 · 127 阅读 · 0 评论 -
C库函数strncat实现
#include <stdio.h>#define BUFF_SIZE (1024)char *mystrncat(char *dest, const char *src, unsigned int len){ char *p = dest; int index; while ('\0' != *p) p ++; for (index = 0; '\0' != src[index] && index < len; index ++) p[in原创 2020-09-13 20:52:51 · 174 阅读 · 0 评论 -
C实现数组的逆置
#include <stdio.h>void array_inversion(int * a, const int len){ int min = 0;//数组最小下标 int max = 0;//数组最大下标 max = len - 1; while (min < max) { int tmp = a[min]; a[min] = a[max]; a[max] = tmp;原创 2020-09-10 13:38:15 · 217 阅读 · 0 评论 -
C实现大小写字母的转换
#include <stdio.h>int main(int argc, const char *argv[]){ char ch; printf("input one charactor:"); ch = getchar();#if 0 if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') putchar(ch > 'Z' ? ch - 32 : ch + 32原创 2020-09-10 13:38:00 · 575 阅读 · 0 评论 -
C中 strtok函数的使用
#include <stdio.h>#include <string.h>int test01(){ char a[100] = "abc_123_def_564_ddddddd_qqqqqq_xxxxx"; char *s;//定义一个char的指针变量 s = strtok(a, "_"); printf("%s\n", s); s = strtok(NULL, "_");//第二次调用的时候,第一个参数写NULL prin原创 2020-09-10 13:37:35 · 144 阅读 · 0 评论 -
C中 strcpy/strncpy的使用
#include <stdio.h>#include <string.h>int main(int argc, const char *argv[]){ char a[10] = "abc"; char b[100] = "1234567890abcdefghijklmnopqrstuvwxyz"; //把b的内容拷贝到a里面 /* int index = 0; while(b[index]) { a原创 2020-09-10 13:37:26 · 145 阅读 · 0 评论 -
C中strcmp/strncmp和strcat/strncat函数的使用
#include <stdio.h>#include <string.h>int demo(void){ char a[10] = "abc"; char b[100] = "hello12345657689";// strcat(a, b);//将a和b合并为一个字符串,结果放入a strncat(a, b, 6); printf("%s\n", a); return 0;}int main(int arg原创 2020-09-10 13:37:08 · 171 阅读 · 0 评论 -
C中strchr和strstr函数的使用
#include <stdio.h>#include <string.h>int main(int argc, const char *argv[]){ char a[100] = "hello world"; char *s = NULL; s = strchr(a, 'd'); if (s != NULL) printf("%s\n", s); s = strstr(a, "wo"); p原创 2020-09-10 13:36:50 · 246 阅读 · 0 评论 -
sscanf和sprintf函数的使用
#include <stdio.h>#include <string.h>int demo1(void){ char a[100] = { 0 }; memset(a, 0, sizeof(a)); int i = 255; sprintf(a, "%s%d%x", "hello world", i, i); printf("%s\n", a); return 0;}int demo2(void){原创 2020-09-09 14:16:31 · 135 阅读 · 0 评论 -
C指针与指针之间的相减操作
指针与指针的相减操作,表示两个指针指向的内存位置之间相隔多少个元素(注意是元素,并不是字节数)。例如对于int类型的指针p和p1,p1-p的意义表示他们之间相隔多少个int类型的元素。同样对于其他类型的指针变量之间相减的意义也是一样。测试代码#include <stdio.h>int main(int argc,const char * argv[]){ int a[3] = {1,2,3}; int *p = a; printf("---------------原创 2020-08-27 13:49:15 · 6145 阅读 · 0 评论 -
Linux下C内存对齐规则
内存对齐规则内存对齐有三种单位: 字、半字、字节。宽度为4B的数据要存放在能够被4整除的地址上。 (例如32位系统中的int、long)宽度为2B的数据要存放在能够被2整除的地址上。 (例如32位系统中的short)宽度为1B的数据要存放在能够被1整除的地址上(任意存放)。 (例如char)结构体的整体大小是其中最大基本类型的整数倍(如果基本类型的大小超过了系统的字长,则按系统字长计算。例如double)。GCC提供了一些机制允许用户自己控制内存对齐方式:一、使用编译注解机制 pra原创 2020-08-19 21:49:21 · 617 阅读 · 0 评论 -
C中函数指针的典型用法
使用函数指针实现字符串的追加与字符串的拷贝功能mystrcat.c#include <stdio.h>char *mystrcat(char *dest, const char *src){ char *tmp = dest; while(*dest !='\0') { dest++; } while(*src != '\0') { *dest = *src; dest++; src++; } *dest =原创 2020-08-17 09:29:03 · 358 阅读 · 0 评论 -
水仙花小游戏
水仙花小游戏 输出100-1000以内的水仙花数水仙花数算法:一个数等于它各位的立方和,例如:153= 1*1*1 + 5*5*5 + 3*3*3 1 #include <stdio.h> 2 #include <math.h> 3 4 int main() 5 { 6 int flower; 7 int i,...原创 2018-08-04 18:33:00 · 185 阅读 · 0 评论 -
指针数组与数组指针
指针数组/*指针数组*/#include <stdio.h>int main(int argc, const char *argv[]){ int a[2][3] = {1,2,3,4,5,6}; int *p[2] = {a[0],a[1]}; printf("p[0] = %p\n",p[0]); //首行首元素地址 printf("a[0] = %p\n",a[0]); //首行首元素地址 printf("*a[0] = %d\n",*a[0]); //首行首元素的原创 2020-08-13 22:51:55 · 94 阅读 · 0 评论 -
C中++和指针操作相遇
#include <stdio.h>int main(int argc, const char *argv[]){ int a[3] = {1,4,7}; int *p = a;#if 0 printf("%p\n",a); printf("%d\n",a[0]); printf("%d\n",*a); printf("%p\n",p); printf("*p++ = %d\n",*p++); printf("%d\n",*p); printf("%d\n",(*p)+原创 2020-08-13 22:39:52 · 149 阅读 · 0 评论 -
C中main函数传参
在进行C语言编程时,有时候需要使用命令行传参,下面对其进行一个分析。int main(int argc,const char * agrgv[])int argc 命令行参数个数(包含文件名)const char* argv[] 指针数组:将命令行参数首地址作为元素,加上const关键字的意思是数组中的内容是不能修改的,很明显,我们进行传参的时候,里面的内容是不需要修改的,这就是const关键字的真正含义!#include <stdio.h>int main(int argc, con原创 2020-08-12 00:01:35 · 695 阅读 · 0 评论 -
C下使用指针作为函数参数完成两次不同操作
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>int getContentLen(const char *filename, char **buf, int *len){ char *tmp = NULL; tmp = (char *)malloc(100*sizeof(char)); if (buf == NULL)原创 2020-08-05 16:59:37 · 205 阅读 · 0 评论 -
C中#连接符的使用
转字符串连接2个符号组成一个新的符号#include <stdio.h>#define ABC(X) #X#define DAY(X) myday##Xint main(int argc, char** argv){ int myday1 = 100; int myday2 = 200; printf( ABC(12) ); printf("-----%d------\n", DAY(2)); return 0;}原创 2020-08-04 16:44:56 · 470 阅读 · 0 评论 -
C中结构体的地址偏移
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>typedef struct AdvTeacher{ char name[64]; //64 int age ; //4 int p; //4 char *pname2;}AdvTeacher;int main(int argc, const char * argv原创 2020-08-04 09:38:50 · 1533 阅读 · 0 评论 -
数组指针和二维数组与在堆区开辟二维数组综合
将指针数组和二维数组中的字符串放到一个堆开辟的内存空间中,并且进行排序#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>/* 将指针数组和二维数组中的字符串放到一个堆开辟的内存空间中,并且进行排序 */int sort(char **myp1 /*in*/, int num1, char (*myp2)[30], int num2,原创 2020-08-04 09:27:32 · 433 阅读 · 0 评论 -
C二级指针分配内存方式与释放
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>//通过函数返回值返回开辟的内存首地址char **getMem(int num){ int i = 0; char **p2 = NULL; p2 = (char **)malloc(sizeof(char *) * num); if (p2 == NULL) { r原创 2020-08-03 22:35:34 · 1362 阅读 · 0 评论 -
C中main函数作参数
main函数的参数可以为指针数组,实际上是操作系统在调用main函数之前已经给main函数 分配了内存。#include <stdio.h>//main函数是操作系统调用的函数//在程序执行的时候可以向main函数传递参数/*argc 命令行参数argv 命令行参数数组env 函数变量数组int main();int main(int argc);int main(int argc, char *argv[])*/int main(int argc, const原创 2020-08-03 22:22:03 · 196 阅读 · 0 评论 -
C数组指针变量的定义
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void demo(){ typedef int (MyArrayType)[5]; //定义了一个数据类型 数组数据类型 int i = 0; MyArrayType myArray; //int myArray[5]; for (i=0; i<5; i++)原创 2020-08-03 22:10:12 · 313 阅读 · 0 评论 -
C证明多维数组实际上是线性的
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>void printArray(int *array, int size){ int i = 0; for (i=0; i<size; i++) { printf("%d ", array[i]); } puts(""); return ;}int原创 2020-08-03 22:07:38 · 191 阅读 · 0 评论 -
C三级指针使用
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h>#include <string.h>#include <stdio.h>char **getMem(int num){ int i = 0; char **p2 = NULL; p2 = (char **)malloc(sizeof(char *) * num); if (p2 == NULL) { return NULL; } for原创 2020-08-03 10:52:43 · 257 阅读 · 0 评论 -
C中Socket模型框架
socketclient.h#ifndef _SOCKETCLIENT_H_#define _SOCKETCLIENT_H_//兼容C++编译器//如果是C++编译器,按c标准编译#ifdef __cplusplusextern "C"{#endif //init environment int socketclient_init(void **handle); //send message int socketclient_send(void *handle, vo原创 2020-07-31 11:09:58 · 526 阅读 · 0 评论 -
C打印log日志的参考模板
mylog.h文件#ifndef _MY_LOG_H_#define _MY_LOG_H_#define FILENAME "./debug.log"/*#define IC_NO_LOG_LEVEL 0#define IC_DEBUG_LEVEL 1#define IC_INFO_LEVEL 2#define IC_WARNING_LEVEL 3#define IC_ERROR_LEVEL 4;*//******************************原创 2020-07-31 09:14:41 · 760 阅读 · 0 评论 -
C函数指针的实际使用举例
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>int add(int a, int b){ return a + b;}int sub(int a, int b){ return a - b;}int mul(int a, int b){ return a * b;}int divide(原创 2020-07-30 16:59:56 · 148 阅读 · 0 评论 -
C结构体套结构体内存测试
使用的环境为64位机器,故指针类型为8B。#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct A{ int a; int b; char *p;} A;/* 结构体可以嵌套另外一个结构体的任何类型变量 结构体嵌套本结构体普通变量(不可以) 本结构体的类型大小无法确定,类型本质:原创 2020-07-30 16:05:20 · 176 阅读 · 0 评论 -
C中结构体使用
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>/*1、结构体类型定义2、结构体变量定义3、结构体变量的初始化4、typedef改类型名5、点运算符和指针法操作结构体6、结构体也是一种数据类型,复合类型,自定义类型*///1、结构体类型定义//struct关键字//struct Teacher合一起才是类型//{}原创 2020-07-28 14:51:44 · 185 阅读 · 0 评论 -
C结构体中的深拷贝
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct Teacher{ char *name; int age;}Teacher;//结构体中嵌套指针,而且动态分配空间//同类型结构体变量赋值//不同结构体成员指针变量指向同一块内存int main(void){ Teacher t1; t1原创 2020-07-28 14:43:57 · 706 阅读 · 0 评论 -
C下按照指定格式截取子串
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>// 有一个字符串符合以下特征(“abcdef,acccd,eeee,aaaa,e3eeee,ssss,”)//// 写两个函数(API),输出以下结果// 第一个API(第二种内存模型: 二维数组)// 1)以逗号分隔字符串,形成二维数组,并把结果传出// 2)把二维数组行数运原创 2020-07-28 13:33:34 · 240 阅读 · 0 评论 -
C实现在字符串下替换子串
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>/* 字符串有以下特征(“abcd11111abcd2222abcdqqqqq”), 把字符串替换成(dcba11111dcba2222dcbaqqqqq),并把结果传出。*//*src: 原字符串dst: 生成的或需要填充的字符串sub: 需要原创 2020-07-28 13:19:48 · 1484 阅读 · 0 评论 -
一维数组与指针的关系
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>//argc: 传参数的个数(包含可执行程序)//argv:指针数组,指向输入的参数int main(int argc, char *argv[]){ //指针数组,它是数组,每个元素都是指针 //[] 比 * 优先级高 char *a[] = { "aaaaaaa", "bb原创 2020-07-28 12:04:30 · 606 阅读 · 0 评论 -
二维数组的使用
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>int demo01(void){ int a1[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; int a2[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,原创 2020-07-28 11:00:17 · 389 阅读 · 0 评论 -
二级指针作为输入__在函数内部分配内存
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>int demo(void){ char *p0 = NULL; p0 = (char *)malloc(100); strcpy(p0, "agdsgds"); //10个char *, 每一个的值都是空 int i = 0; char *p[10] = { 0 };原创 2020-07-28 10:22:55 · 176 阅读 · 0 评论