16位无符号整数数组,输出的元素多于c

问题描述

我试图将数据馈送到数组中,并且仅更改一些值,而将其余的值保留为0,但是我不断收到的数据超出了数组理论上应该容纳的数量。这是我的代码编译并运行

gcc -o arr arrayconst.c

#include <stdio.h>
#include <stdint.h>
int main(){    
    uint16_t buf_tx[101];
    for(int i = 0; i < sizeof(buf_tx);i++){buf_tx[i]=0;}
    //memset(buf_tx,sizeof(buf_tx)*sizeof(uint16_t)); //alternate way of zeroizing
    buf_tx[42] = 0x2000;
    buf_tx[46] = 0x2000;
    buf_tx[48] = 0x2000;
    buf_tx[50] = 0x2000;
    buf_tx[52] = 0x2000;
    buf_tx[54] = 0x2000;
    buf_tx[72] = 0x1000;
    buf_tx[76] = 0x0001;
    for(int i = 0; i < sizeof(buf_tx); i++){
        printf("|%d 0x%04X,",i,buf_tx[i]);
        
    }
}

和我的输出:

| 0 0x0000,| 1 0x0000,| 2 0x0000,| 3 0x0000,| 4 0x0000,| 5 0x0000,| 6 0x0000,| 7 0x0000,| 8 0x0000,| 9 0x0000,| 10 0x0000,| 11 0x0000,| 12 0x0000,| 13 0x0000,| 14 0x0000,| 15 0x0000,| 16 0x0000,| 17 0x0000, | 18 0x0000,| 19 0x0000,| 20 0x0000,| 21 0x0000,| 22 0x0000,| 23 0x0000,| 24 0x0000,| 25 0x0000,| 26 0x0000,| 27 0x0000,| 28 0x0000, | 29 0x0000,| 30 0x0000,| 31 0x0000,| 32 0x0000,| 33 0x0000,| 34 0x0000,| 35 0x0000,| 36 0x0000,| 37 0x0000,| 38 0x0000,| 39 0x0000, | 40 0x0000,| 41 0x0000,| 42 0x2000,| 43 0x0000,| 44 0x0000,| 45 0x0000,| 46 0x2000,| 47 0x0000,| 48 0x2000,| 49 0x0000,| 50 0x2000, | 51 0x0000,| 52 0x2000,| 53 0x0000,| 54 0x2000,| 55 0x0000,| 56 0x0000,| 57 0x0000,| 58 0x0000,| 59 0x0000,| 60 0x0000,| 61 0x0000, | 62 0x0000,| 63 0x0000,| 64 0x0000,| 65 0x0000,| 66 0x0000,| 67 0x0000,| 68 0x0000,| 69 0x0000,| 70 0x0000,| 71 0x0000,| 72 0x1000, | 73 0x0000,| 74 0x0000,| 75 0x0000,| 76 0x0001,| 77 0x0000,| 78 0x0000,| 79 0x0000,| 80 0x0000,| 81 0x0000,| 82 0x0000,| 83 0x0000, | 84 0x0000,| 85 0x0000,| 86 0x0000,| 87 0x0000,| 88 0x0000,| 89 0x0000,| 90 0x0000,| 91 0x0000,| 92 0x0000,| 93 0x0000,| 94 0x0000, | 95 0x0000,| 96 0x0000,| 97 0x0000,| 98 0x0000,| 99 0x0000,| 100 0x0000,| 101 0x0000,| 102 0x0000,| 103 0x0000,| 104 0x0000,| 105 0x0000,| 106 0x0000,| 107 0x0000,| 108 0x0000,| 109 0x0000,| 110 0x0000,| 111 0x0000,| 112 0x0000,| 113 0x0000,| 114 0x0000,| 115 0x0000,| 116 0x0000,| 117 0x0000,| 118 0x0000,| 119 0x0000,| 120 0x0000,| 121 0x0000,| 122 0x0000,| 123 0x0000,| 124 0x0000,| 125 0x0000,| 126 0x0000,| 127 0x0000,| 128 0x0000,| 129 0x0000,| 130 0x0000,| 131 0x0000,| 132 0x0000,| 133 0x0000,| 134 0x0000,| 135 0x0000,| 136 0x0000,| 137 0x0000,| 138 0x0000,| 139 0x0000,| 140 0x0000,| 141 0x0000,| 142 0x0000,| 143 0x0000,| 144 0x0000,| 145 0x0000,| 146 0x0000,| 147 0x0000,| 148 0x0000,| 149 0x0000,| 150 0x0000,| 151 0x0000,| 152 0x0000,| 153 0x0000,| 154 0x0000,| 155 0x0000,| 156 0x0000,| 157 0x0000,| 158 0x0000,| 159 0x0000,| 160 0x0000,| 161 0x0000,| 162 0x0000,| 163 0x0000,| 164 0x0000,| 165 0x0000,检测到***堆栈崩溃***:已终止中止(核心

任何帮助将不胜感激!

解决方法

sizeof给出其操作数中的字节数。在典型的C实现中,uint16_t是两个字节,因此uint16_t buf_tx[101]是202字节。

要获取数组x中的元素数,请使用sizeof x / sizeof *x

for (int i = 0; i < sizeof buf_tx / sizeof *buf_tx; i++)

这通常打包在宏中:

#define NumberOf(x) (sizeof (x) / sizeof *(x))
…
for (int i = 0; i < NumberOf(buf_tx); i++)

这仅适用于数组,不适用于指针,因为如果sizeof x是数组,x给出数组的大小,如果x是指针,则给出指针的大小。值得注意的是,声明为数组的函数参数(例如void foo(int x[]))会自动调整为指针,因此NumberOf(x)将不适用于它们。

,

如果您有一个数组array,则该表达式将确定数组中元素的数量

sizeof( array ) / sizeof( *array )

因此在循环中,您需要使用此表达式。

最好定义一个类似的常量

const size_t N = sizeof( buf_tx ) / sizeof( *buf_tx );

并在类似的循环中使用它

for ( size_t i = 0; i < N; i++ ) buf_tx[i] = 0; 

for ( size_t i = 0; i < N; i++ )
{
    printf("|%zu %#06x,",i,buf_tx[i] );
}

请注意,对于size_t类型的对象,您需要使用转换说明符%zu。并且,可以将标志0x用作#,而不必在转换说明符之前手动%#06x来写输出的数字。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...