0

我正在尝试将数据输入数组并仅更改几个值并将其余的值保留为 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,0,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, |23 0x0000, |25 0x0000, |26 0x0000, |27 0x0000, |28 0x0000, |29 0x0000, |30 0x0000, |31 0x0000, |32 0x0000, |33 0x0000, |34 0x0000, |35 0x00000, |36 0, | 0x0000,|38 0x0000,|39 0x0000,|40 0x0000,|41 0x0000,|42 0x2000,|43 0x0000,|44 0x0000,|45 0x0000,|46 0x2000,|47 0x0000,|48 0x0000,|48 0x0000, |50 0x2000,|51 0x0000,|52 0x2000,|53 0x0000,|54 0x2000,|55 0x0000,|56 0x0000,|57 0x0000,|58 0x0000,|59 0x0000,|60 0x00000,|21 06 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 0x00000,|83 08 0x0000, |85 0x0000, |86 0x0000, |87 0x0000, |88 0x0000, |89 0x0000, |90 0x0000, |91 0x0000, |92 0x0000, |93 0x0000, |94 0x0000, |95 0x0000, |95 0x0000, |97 0x0000, |98 0x0000, |99 0x0000, |100 0x0000, |101 0x0000, |102 0x0000, |103 0x0000, |104 0x0000, |105 0x0000, |106 0x0000, |10907 0x000008 0x0000,| 110 0x0000,| 111 0x0000,| 112 0x0000,| 113 0x0000,| 114 0x0000,| 115 0x0000,| 116 0x0000,| 117 0x0000,| 118 0x0000,| 118 0x0000,| 119 0x0000,| 119 0x0000,| 120 0x0000000000,1210x0000,1210000,1210x0000, | 121 0x0000, | 121 0x0000, | 121 0x0000, | 121 0x0000,cr |122 0x0000, |123 0x0000, |124 0x0000, |125 0x0000, |126 0x0000, |127 0x0000, |128 0x0000, |129 0x0000, |130 0x0000, |131 0x00000, |1334000, |133400x 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 0x00000, |152000, |152000, | 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, *** 检测到堆栈破坏 ***: 终止 Aborted (core dumped)

任何帮助将不胜感激!

4

2 回答 2

0

如果你有一个数组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.

于 2020-11-03T21:01:28.897 回答
0

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)不适用于它们。

于 2020-11-03T20:58:02.820 回答