1、用到的相关头文件
#include <stdio.h>
2、数组移位函数简介
void shift(unsigned char shift_value, unsigned char *shift_group, int shift_lenth);
功能:
1.装入一个新的值
2.数组整体左移
3.新的值装入数组的高位地址、低位数组地址丢弃
参数:
unsigned char shift_value :要装入数组的新值
unsigned char *shift_group :要移位数组的首地址
int shift_lenth :要进行移位的长度
3、实现代码
/*
所有数据左移,低位仍掉,新数据放在数组末端
*/
#include <stdio.h>
void shift(unsigned char shift_value, unsigned char *shift_group, int shift_lenth)
{
int shift_num;
for (shift_num = 0; shift_num < shift_lenth - 1; shift_num++)
shift_group[shift_num] = shift_group[shift_num + 1];
shift_group[shift_num] = shift_value;
}
4、测试代码
/*
所有数据左移,低位仍掉,新数据放在数组末端
*/
#include <stdio.h>
void shift(unsigned char shift_value, unsigned char *shift_group, int shift_lenth)
{
int shift_num;
for (shift_num = 0; shift_num < shift_lenth - 1; shift_num++)
shift_group[shift_num] = shift_group[shift_num + 1];
shift_group[shift_num] = shift_value;
}
void main()
{
int i;
unsigned char a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //要被移位的数组
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\r\n");
shift(10, a, 10);
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\r\n");
}
5、实现效果
![]()
程序先输出未移动的数组a
再输出移动后的数组a
这篇博客介绍了如何使用C语言编写一个数组左移函数,该函数将数组元素整体左移,新值填充到数组末尾,低位元素丢失。文章包含函数定义、实现代码以及测试用例,展示了函数的实际效果。

1179

被折叠的 条评论
为什么被折叠?



