竟然改变不了const的值
嵌入式Linux
共 2656字,需浏览 6分钟
·
2023-11-10 13:54
#include "stdio.h"
#include "stdint.h"
void w_function(uint8_t page, uint8_t column, uint8_t reverse, uint8_t *dp) {
uint8_t i, j, *data_temp;
data_temp = dp;
for(j = 0; j < column; j++) {
*dp = (reverse == 1)? (~*dp) : (*dp);
dp++;
}
}
int main() {
int i = 0;
const uint8_t dp[8] = {1,2,3,4,5,6,7,8};
const int a = 10;
printf("Before: %d\n", a);
int *p = (int *)&a; // 创建一个指向 const 变量 a 的指针,并强制类型转换为 int *
*p = 20; // 通过指针修改 const 变量 a 的值
printf("After: %d\n", a);
for (i = 0; i < sizeof(dp)/sizeof(dp[0]); i++)
printf("%d\t", dp[i]);
w_function(0, sizeof(dp)/sizeof(dp[0]), 1, (uint8_t *)&dp);
for (i = 0; i < sizeof(dp)/sizeof(dp[0]); i++)
printf("%d\t", dp[i]);
return 0;
}
上面的代码输出是
评论