gpt4 book ai didi

从一个字符指针复制到另一个字符指针

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:00 25 4
gpt4 key购买 nike

我正在尝试将一个 char 数组指针的内容复制到另一个数组。在 for 循环内,它显示所有元素。但在 for 循环之外,它只显示整个数组中的最后一个元素。以下是代码片段

char *outhex[81] = {'\0'};
char *temp[81] = {'\0'};

int a, i, k=0, j=0;
uint8_t quotient[81] ;
uint8_t outdec[81];

quotient[81] = {65,00,01,134,160,00,00,01,194,00,00,00,00,00,00,00,01,00,00,01,32,00,00,00,01,00,00,00,00,00,00,00,15,00,00,00,100,00,00,00,00,00,00,00,06,00,00,8,00,00,00,21,124,00,00,58,152,00,00,03,32,00,00,00,25,00,00,00,00,00,00,00,01,00,00,00,00,00,00,01,244};

for(i = 0; i < 81; i++)
{
char remainder;
j = 0;
char hexadecimalnum[3] = {'0'};
while (quotient[i] != 0)
{
remainder = quotient[i] % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient[i] = quotient[i] / 16;
}
printf("%s", hexadecimalnum);

int p =strlen(hexadecimalnum)-1,q=0;

char ch;
while(p > q)
{
ch = hexadecimalnum[p];
hexadecimalnum[p] = hexadecimalnum[q];
hexadecimalnum[q] = ch;
p--;
q++;
}
printf("\t%s", hexadecimalnum);
outhex[i] = (&hexadecimalnum);

printf(" = %d ", i);
temp[i] = outhex[i];
printf("\t outhex[%d] = %s \t %s \t %d \t %d\n", i, outhex[i], temp[i], &outhex[i], &temp[i]);
}

for(a = 0; a< 81; a++)
{
printf("temp = %s %d \n", temp[a], &temp[a]);
}

输出是:

14  41     = 0   outhex[0]    = 41   41      6299904     6300576
0 0 = 1 outhex[1] = 0 0 6299912 6300584
1 1 = 2 outhex[2] = 1 1 6299920 6300592
68 86 = 3 outhex[3] = 86 86 6299928 6300600
0A A0 = 4 outhex[4] = A0 A0 6299936 6300608
0 0 = 5 outhex[5] = 0 0 6299944 6300616
0 0 = 6 outhex[6] = 0 0 6299952 6300624
1 1 = 7 outhex[7] = 1 1 6299960 6300632
2C C2 = 8 outhex[8] = C2 C2 6299968 6300640
0 0 = 9 outhex[9] = 0 0 6299976 6300648
0 0 = 10 outhex[10] = 0 0 6299984 6300656
0 0 = 11 outhex[11] = 0 0 6299992 6300664
0 0 = 12 outhex[12] = 0 0 6300000 6300672
0 0 = 13 outhex[13] = 0 0 6300008 6300680
0 0 = 14 outhex[14] = 0 0 6300016 6300688
0 0 = 15 outhex[15] = 0 0 6300024 6300696
0 0 = 16 outhex[16] = 0 0 6300032 6300704
0 0 = 17 outhex[17] = 0 0 6300040 6300712
0 0 = 18 outhex[18] = 0 0 6300048 6300720
1 1 = 19 outhex[19] = 1 1 6300056 6300728
02 20 = 20 outhex[20] = 20 20 6300064 6300736
0 0 = 21 outhex[21] = 0 0 6300072 6300744
0 0 = 22 outhex[22] = 0 0 6300080 6300752
0 0 = 23 outhex[23] = 0 0 6300088 6300760
1 1 = 24 outhex[24] = 1 1 6300096 6300768
0 0 = 25 outhex[25] = 0 0 6300104 6300776
0 0 = 26 outhex[26] = 0 0 6300112 6300784
0 0 = 27 outhex[27] = 0 0 6300120 6300792
0 0 = 28 outhex[28] = 0 0 6300128 6300800
0 0 = 29 outhex[29] = 0 0 6300136 6300808
0 0 = 30 outhex[30] = 0 0 6300144 6300816
0 0 = 31 outhex[31] = 0 0 6300152 6300824
F F = 32 outhex[32] = F F 6300160 6300832
0 0 = 33 outhex[33] = 0 0 6300168 6300840
0 0 = 34 outhex[34] = 0 0 6300176 6300848
0 0 = 35 outhex[35] = 0 0 6300184 6300856
46 64 = 36 outhex[36] = 64 64 6300192 6300864
0 0 = 37 outhex[37] = 0 0 6300200 6300872
0 0 = 38 outhex[38] = 0 0 6300208 6300880
0 0 = 39 outhex[39] = 0 0 6300216 6300888
0 0 = 40 outhex[40] = 0 0 6300224 6300896
0 0 = 41 outhex[41] = 0 0 6300232 6300904
0 0 = 42 outhex[42] = 0 0 6300240 6300912
0 0 = 43 outhex[43] = 0 0 6300248 6300920
6 6 = 44 outhex[44] = 6 6 6300256 6300928
0 0 = 45 outhex[45] = 0 0 6300264 6300936
0 0 = 46 outhex[46] = 0 0 6300272 6300944
8 8 = 47 outhex[47] = 8 8 6300280 6300952
0 0 = 48 outhex[48] = 0 0 6300288 6300960
0 0 = 49 outhex[49] = 0 0 6300296 6300968
0 0 = 50 outhex[50] = 0 0 6300304 6300976
51 15 = 51 outhex[51] = 15 15 6300312 6300984
C7 7C = 52 outhex[52] = 7C 7C 6300320 6300992
0 0 = 53 outhex[53] = 0 0 6300328 6301000
0 0 = 54 outhex[54] = 0 0 6300336 6301008
A3 3A = 55 outhex[55] = 3A 3A 6300344 6301016
89 98 = 56 outhex[56] = 98 98 6300352 6301024
0 0 = 57 outhex[57] = 0 0 6300360 6301032
0 0 = 58 outhex[58] = 0 0 6300368 6301040
3 3 = 59 outhex[59] = 3 3 6300376 6301048
02 20 = 60 outhex[60] = 20 20 6300384 6301056
0 0 = 61 outhex[61] = 0 0 6300392 6301064
0 0 = 62 outhex[62] = 0 0 6300400 6301072
0 0 = 63 outhex[63] = 0 0 6300408 6301080
91 19 = 64 outhex[64] = 19 19 6300416 6301088
0 0 = 65 outhex[65] = 0 0 6300424 6301096
0 0 = 66 outhex[66] = 0 0 6300432 6301104
0 0 = 67 outhex[67] = 0 0 6300440 6301112
0 0 = 68 outhex[68] = 0 0 6300448 6301120
0 0 = 69 outhex[69] = 0 0 6300456 6301128
0 0 = 70 outhex[70] = 0 0 6300464 6301136
0 0 = 71 outhex[71] = 0 0 6300472 6301144
1 1 = 72 outhex[72] = 1 1 6300480 6301152
0 0 = 73 outhex[73] = 0 0 6300488 6301160
0 0 = 74 outhex[74] = 0 0 6300496 6301168
0 0 = 75 outhex[75] = 0 0 6300504 6301176
0 0 = 76 outhex[76] = 0 0 6300512 6301184
0 0 = 77 outhex[77] = 0 0 6300520 6301192
0 0 = 78 outhex[78] = 0 0 6300528 6301200
1 1 = 79 outhex[79] = 1 1 6300536 6301208
4F F4 = 80 outhex[80] = F4 F4 6300544 6301216
temp = F4 6300576
temp = F4 6300584
temp = F4 6300592
temp = F4 6300600
temp = F4 6300608
temp = F4 6300616
temp = F4 6300624
temp = F4 6300632
temp = F4 6300640
temp = F4 6300648
temp = F4 6300656
temp = F4 6300664
temp = F4 6300672
temp = F4 6300680
temp = F4 6300688
temp = F4 6300696
temp = F4 6300704
temp = F4 6300712
temp = F4 6300720
temp = F4 6300728
temp = F4 6300736
temp = F4 6300744
temp = F4 6300752
temp = F4 6300760
temp = F4 6300768
temp = F4 6300776
temp = F4 6300784
temp = F4 6300792
temp = F4 6300800
temp = F4 6300808
temp = F4 6300816
temp = F4 6300824
temp = F4 6300832
temp = F4 6300840
temp = F4 6300848
temp = F4 6300856
temp = F4 6300864
temp = F4 6300872
temp = F4 6300880
temp = F4 6300888
temp = F4 6300896
temp = F4 6300904
temp = F4 6300912
temp = F4 6300920
temp = F4 6300928
temp = F4 6300936
temp = F4 6300944
temp = F4 6300952
temp = F4 6300960
temp = F4 6300968
temp = F4 6300976
temp = F4 6300984
temp = F4 6300992
temp = F4 6301000
temp = F4 6301008
temp = F4 6301016
temp = F4 6301024
temp = F4 6301032
temp = F4 6301040
temp = F4 6301048
temp = F4 6301056
temp = F4 6301064
temp = F4 6301072
temp = F4 6301080
temp = F4 6301088
temp = F4 6301096
temp = F4 6301104
temp = F4 6301112
temp = F4 6301120
temp = F4 6301128
temp = F4 6301136
temp = F4 6301144
temp = F4 6301152
temp = F4 6301160
temp = F4 6301168
temp = F4 6301176
temp = F4 6301184
temp = F4 6301192
temp = F4 6301200
temp = F4 6301208
temp = F4 6301216

在 for 循环之外,temp 数组的所有元素只包含最后一个数据。初始元素被最后一个元素覆盖。这是什么原因?是否有任何解决方案可以在“for 循环”之外获取所有内容数组。

最佳答案

这一行就是问题所在。

outhex[i] = (&hexadecimalnum);

对于 i 的任何值,它都指向同一个变量。例如,outhex[0] 将与 outhex[1] 相同。

您还为其分配了错误的值,&hexadecimalnumchar **outhex[i] 将是字符 *。您的编译器应该将此标记为警告。

执行此操作的正确方法是制作一个新的字符串副本并将其存储到 outhex[i] 中。像这样,例如...

outhex[i] = malloc(strlen(hexadecimalnum)+1);
strcpy(outhex[i],hexadecimalnum);

或者您可以使用 strdup,它在一个函数调用中完成分配和复制。

outhex[i] = strdup(hexadecimalnum);

我看不出 tempouthex 有任何区别,所以可能没有任何调用来制作新副本,所以 temp[i] =outhex[i] 可能不是问题……至少在提供的代码中不是。

这行也有错别字

char hexadecimalnum[3]  = {'0'};

这应该使您的字符串以 NUL 终止,并且可以与以前的修复一起使用。

char hexadecimalnum[3]  = {'\0'};

关于从一个字符指针复制到另一个字符指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com