C与指针和数组相关的问题

C与指针和数组相关的问题,c,pointers,memcpy,C,Pointers,Memcpy,基本上,在下面的代码中,我的最终数组似乎没有function1()中的内容。你知道我为什么不能让它工作吗?谢谢 #include <stdio.h> #include <string.h> #include<stdlib.h> unsigned char *function1() { unsigned char array2[] = { 0x4a,0xb2 }; return (array2 ); } m

基本上,在下面的代码中,我的最终数组似乎没有function1()中的内容。你知道我为什么不能让它工作吗?谢谢

    #include <stdio.h>
    #include <string.h>
    #include<stdlib.h>

  unsigned char *function1() 
 {
  unsigned char array2[] = { 0x4a,0xb2 };
  return (array2 );

  }

   main()

     {
unsigned char temp[] = { 0xaa, 0x0b, 0x03,0x04,0x05,0x06,0x07,0x08,0x09 };
unsigned char x[2];
unsigned char *packet;
int pkt_len;
pkt_len = sizeof(temp) + sizeof(x);
packet = (unsigned char*) malloc ( pkt_len +1);

memset( packet, 0x00, pkt_len +1);
unsigned char *pointer1 = malloc ( sizeof(temp) + 1);

memset( pointer1, 0x00, sizeof(temp) +1);
memcpy (pointer1, temp, sizeof(temp) );

memcpy (packet, pointer1, sizeof(temp) );
printf("\nPacket before copy is 0x%x\n", packet[8]);

    unsigned char *array2 = malloc ( sizeof (x) + 1)  ;
    array2 = (char *)function1();
printf("\nArray2 is 0x%x\n", array2[0]);
    memcpy (packet + sizeof(temp), array2, sizeof(x) );
printf("After copy, Packet contents are 0x%x\n", packet[9]);
  }
#包括
#包括
#包括
无符号字符*function1()
{
无符号字符数组2[]={0x4a,0xb2};
返回(array2);
}
main()
{
无符号字符温度[]={0xaa,0x0b,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
无符号字符x[2];
无符号字符*数据包;
国际邮包;
pkt_len=sizeof(temp)+sizeof(x);
数据包=(无符号字符*)malloc(pkt_len+1);
memset(数据包,0x00,数据包长度+1);
无符号字符*pointer1=malloc(sizeof(temp)+1);
memset(指针1,0x00,大小为(临时)+1);
memcpy(指针1、温度、大小(温度));
memcpy(数据包、指针1、大小f(临时));
printf(“\n拷贝前的包是0x%x\n”,包[8]);
无符号字符*array2=malloc(sizeof(x)+1);
array2=(char*)function1();
printf(“\n数组2是0x%x\n”,数组2[0]);
memcpy(数据包+sizeof(temp)、array2、sizeof(x));
printf(“复制后,数据包内容为0x%x\n”,数据包[9]);
}

不太清楚你到底想做什么,但我认为这就是你想要的。我对它进行了评论,以说明它在做什么

void function1(char *dest, size_t len);

int main() 
{
  /* Allocate an array 'temp' */
  char temp[] = { 'a', 's', 'd', 'f', 'g', 'h' };

  /* Allocate an array 'array1', the same size as 'temp' */
  char array1[sizeof temp];

  /* Copy the contents of 'temp' into 'array1' */
  memcpy(array1, temp, sizeof temp);

  /* Call a function to copy new contents into 'array1' */
  function1(array1, sizeof array1);

  return 0;
}

void function1(char *dest, size_t len)
{
  char temp2[] = { 1, 2, 3, 4, 5 };

  /* Determine how much to copy - the _minimum_ of 'len' and 'sizeof temp2' */
  if (len > sizeof temp2)
  {
      len = sizeof temp2;
  }

  /* Copy contents of 'temp2' into 'dest' */
  memcpy(dest, temp2, len);
}

我似乎无法理解你的代码。问题很简单,但代码不起作用。 接下来是这段代码
memcpy(指针1+sizeof(临时)、指针3、sizeof(临时数组)

我猜您希望最后一个数组包含{a,b,c,d,e,1,2,3,4,5}。
我建议您检查K&R中的指针和数组,然后再次尝试这个问题。

以下是我在代码中观察到的错误。 你写的

  char temp [] = { a,s,d,f,g,h};
  char * pointer1, *array1;
  pointer1 = &temp;  
  memcpy (array1, pointer1, sizeof( temp) );
现在无需执行此操作,任何数组本身的名称都是指针。 因此,你可以简单地做

  char temp [] = { a,s,d,f,g,h};
  char *pointer1;
  memcpy (pointer1, temp , sizeof( temp) );
同样,指针1是否有足够的空间容纳指针3的内容?您是否拥有由
pointer1+sizeof(temp)
指向的内存区域?它也会使您的程序崩溃。
现在您可以使用
realloc()
或在早期阶段使用
malloc()
为指针1分配更大的空间。

为什么要在这里
sizeof(临时数组)
?您不认为它应该有指针3中的字节数吗

最后在
function1()的定义中

array2
有什么作用?什么都没有!那么它应该被删除。
还用

return temp2;
这意味着
指针2
也没用


希望有帮助。

很抱歉,那是非常可怕的代码。看起来你刚刚复制了它,犯了错误等等,现在正试图让它正常工作。在让它正常工作后,你应该很好地理解指针。@talk2vogon-这是Vogon poetryThere
function1()中有一个严重的错误
您返回一个指向局部变量的指针,这是一个很大的nono,因为您调用的下一个函数将用它自己的局部变量覆盖堆栈空间。Andrew,感谢您的出色回答。清除了这么多内容。所以我在这里添加了一些新代码。您能告诉我为什么要使用数据包[9]即使array2[0]具有正确的内容,也不显示正确的内容?谢谢caf。唯一的区别是我想将数组的内容合并到主循环中,而不是function1循环中。(查看我上传的更改代码)。谢谢您的时间。
memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)
char *pointer2, *array2;
 // Now i need to have pointer2 point to contents of array2.
 pointer2 = &temp2;
 return pointer2 
return temp2;