C基础、细节、学习、笔记

1、变量交换

void Swap1(int* a, int* b)
{
	*a = *a + *b;
	*b = *a - *b;
	*a = *a - *b;
}

void Swap2(int* a, int* b)
{
	*a = *a ^ *b;
	*b = *a ^ *b;
	*a = *a ^ *b;
}

2、字符串定义char *str 与char str[] 区别

char str[]

定义方式:        这是一种定义字符数组的方式

修改性:            可修改

存储区:            可存储与栈区或者全局/静态数据区

char *str

定义方式:        定义指向字符串的指针的方式

可修改性:        不可修改

存储区:           指向一个字符串字面量,内存是在静态存储区分配 

3、程序存储4区

  1. 代码区(Code Segment 或 Text Segment)

    • 存储程序的机器指令,即程序的二进制代码。
    • 通常只读,以防止程序无意中修改自己的指令。
    • 这个区域通常是由操作系统映射到进程的地址空间中的。
  2. 全局/静态数据区(Global/Static Data Segment 或 BSS Segment)

    • 存储全局变量和静态变量。
    • 这些变量在整个程序运行期间都存在。
    • 初始值为零的全局变量和静态变量占用的内存空间在这个区域。
    • 初始值非零的全局变量和静态变量占用的内存空间通常在这个区域的初始化部分。
  3. 堆区(Heap Segment)

    • 由程序员动态分配和释放内存的空间。
    • 使用 malloccallocreallocfree 等函数进行分配和释放。
    • 堆区的内存分配和释放由程序员控制,通常用于动态数据结构,如动态数组、链表等。
  4. 栈区(Stack Segment)

    • 存储函数的局部变量、函数参数以及返回地址等。
    • 栈区的内存是自动分配和释放的,每当函数调用时就会在栈上分配空间,函数结束时就会自动释放这部分空间。
    • 栈区遵循后进先出(LIFO)原则。

4、函数形参接收二维数组

/*     
 将masterBuf里的数据,逐个存到buffers二维数组内,例如如下说明:
 masterBuf[0]->buffers[0][0],masterBuf[1]->buffers[1][0]....masterBuf[5]->buffers[5][0]
 masterBuf[6]->buffers[0][1],masterBuf[7]->buffers[1][1]....masterBuf[11]->buffers[5][1]
        
 */
#include <stdio.h>
#include <stdint.h>

#define BUF_SIZE 100
#define NUM_BUFFERS 6

uint16_t masterBuf[BUF_SIZE];         // 主缓冲区,假定已经被填充
uint16_t buffers[NUM_BUFFERS][64];     // 数组,存储各个子缓冲区

void GetOneChData(uint16_t(*pOut)[64], const uint16_t* pIn, uint8_t numElements)
{
    for (uint16_t i = 0; i < numElements; i++)
    {
        uint8_t bufferIndex = i % NUM_BUFFERS; // 确定目标子缓冲区
        pOut[bufferIndex][i / NUM_BUFFERS] = pIn[i];
    }
}

int main() {
    for (int i = 0; i < BUF_SIZE; i++)
    {
        masterBuf[i] = i;
    }

    GetOneChData(buffers, masterBuf, BUF_SIZE);

    // 示例:打印前两个子缓冲区的部分内容
    printf("Data in the first sub-buffer:\n");
    for (int i = 0; i < 20; i++) {
        printf("%02X ", buffers[0][i]);
    }
    printf("\n");

    printf("Data in the second sub-buffer:\n");
    for (int i = 0; i < 20; i++) {
        printf("%02X ", buffers[1][i]);
    }
    printf("\n");

    return 0;
}

5、双指针

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

void ReverseStr(char* str)
{
    uint16_t len = 0;
    char* start = str;
    char* end = NULL;
    char temp;

    while (str[len] != '\0')
    {
        len++;
    }

    end = start + len - 1;
    while (start < end)
    {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int main()
{
    char str[] = "qwert";  // 使用字符数组而非字符串字面量
    printf("Original str: %s\n", str);
    ReverseStr(str);
    printf("Reverse str: %s\n", str);
    return 0;
}

6、#define宏定义比大小

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

7、位操作

num |= (1 << (bitPosition - 1));    //置位
num &= ~(1 << (bitPosition - 1));   //清零 

8、联合判断字节序

#include <stdio.h>
#include <stdint.h>

union EndiannessTest {
    uint32_t asInt;
    uint8_t asBytes[4];
    struct Bit {
        uint32_t val1 : 8;
        uint32_t val2 : 8;
        uint32_t val3 : 8;
        uint32_t val4 : 8;
    };
};

int main() {
    union EndiannessTest test;
    test.asInt = 0x01020304;    /* 低地址为04,高地址为01;低字节asBytes[0],高字节asBytes[3] */    

    /* 若是小端则asBytes[0]为0x04,大端则0x01 */
    printf("%02x %02x %02x %02x\n", test.asBytes[0], test.asBytes[1], test.asBytes[2], test.asBytes[3]);

    /* 位域的存储方式为val1存储在最低8位,后续为val2、val3、val4 */
    test.val1 = 0xF1;
    test.val2 = 0x01;
    test.val3 = 0x02;
    test.val4 = 0x63;

    /* test.asInt:0x630201f1 */
    printf("test.asInt:%02x\n", test.asInt);
    
    /* f1 01 02 63 */
    printf("%02x %02x %02x %02x\n", test.asBytes[0], test.asBytes[1], test.asBytes[2], test.asBytes[3]);
    return 0;
}

9、字符串转数字

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

int main() {
    char str[] = "12345";
    long int result;
    int len = strlen(str);
    long int manualResult = 0;
    int i;

    /* 使用 strtol 函数转换字符串 */ 
    result = strtol(str, NULL, 10);
    printf("Using strtol: Converted value: %ld\n", result);

    /* 手动解析字符串 */ 
    for (i = 0; i < len; i++) {
        manualResult = manualResult * 10 + (str[i] - '0');
    }
    printf("Manual conversion: Converted value: %ld\n", manualResult);

    return 0;
}

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值