从一个char指针复制到另一个char指针

时间:2018-02-01 16:13:33

标签: c pointers

我正在尝试将一个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循环,临时数组的所有元素仅包含最后一个数据。初始元素被最后一个元素覆盖。这是什么原因?是否有任何解决方案可以将所有内容数组放在'for循环'之外。

1 个答案:

答案 0 :(得分:3)

这一行是问题所在。

outhex[i] = (&hexadecimalnum);

对于i的任何值,它指向同一个变量。因此,outhex[0]outhex[1]相同。

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

执行此操作的正确方法是创建字符串的新副本并将其存储到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'};