C语言中关于指针和数组的问题

发布于 2024-09-06 19:12:35 字数 1039 浏览 2 评论 0原文

基本上在下面的代码中,我的最终数组似乎没有 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]);
  }

Basically in the code below, my final array does not seem to have the contents from function1(). Any ideas on why I can't get this to work ? Thanks.

    #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]);
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2024-09-13 19:12:35

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

  char temp [] = { a,s,d,f,g,h};
  char * pointer1, *array1;
  pointer1 = &temp;  
  memcpy (array1, pointer1, sizeof( temp) );

现在无需执行此操作 pointer1 = &temp,任何数组的名称本身就是一个指针。
因此你可以简单地做


  char temp [] = { a,s,d,f,g,h};
  char *pointer1;
  memcpy (pointer1, temp , sizeof( temp) );

但是等等!

pointer1 有足够的空间来存储 temp[] 的内容吗?在您的代码中,您没有为pointer1分配任何空间,这可能会使您的程序崩溃。

正确的方法是


  char temp [] = { 'a','s','d','f','g','h'};
  char *pointer1 = malloc( sizeof(char) * (sizeof( temp) + 1) );
  memset( pointer1, 0x00, sizeof( temp) + 1 );
  memcpy (pointer1, temp , sizeof( temp) );

在将任何值复制到pointer1之前,我们确保它有足够的空间。

无需强制转换 malloc() 返回值。在 sizeof( temp) + 1 中,为空字符添加 1。然后我们执行memset(),将pointer1指向的内存填充为null。只是良好而健康的做法。

那么你

memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)

再说一遍,pointer1有足够的空间容纳pointer3的内容吗?您是否拥有pointer1 + sizeof(temp)指向的内存区域?它也会使你的程序崩溃。
现在您可以使用 realloc() 或在早期使用 malloc() 为指针1 分配更大的空间。

为什么在这里使用sizeof(临时数组)?你不认为它应该有pointer3中的字节数吗?

最后在function1()的定义中

char *pointer2, *array2;
 // Now i need to have pointer2 point to contents of array2.
 pointer2 = &temp2;
 return pointer2 

array2做了什么?没有什么!那么应该将其删除。
要返回,只需使用

return temp2;

,这意味着指针2也是无用的。

希望有帮助。

Following are the mistakes that I observed in your code.
You wrote

  char temp [] = { a,s,d,f,g,h};
  char * pointer1, *array1;
  pointer1 = &temp;  
  memcpy (array1, pointer1, sizeof( temp) );

Now there is no need to do this pointer1 = &temp, name of any array itself is a pointer.
Hence you can simply do


  char temp [] = { a,s,d,f,g,h};
  char *pointer1;
  memcpy (pointer1, temp , sizeof( temp) );

But wait!

Does pointer1 has enough space to store the contents of temp[]? In your code you have not assigned any space to pointer1, which is likely to crash your program.

The correct way to do it is


  char temp [] = { 'a','s','d','f','g','h'};
  char *pointer1 = malloc( sizeof(char) * (sizeof( temp) + 1) );
  memset( pointer1, 0x00, sizeof( temp) + 1 );
  memcpy (pointer1, temp , sizeof( temp) );

here before copying any value into pointer1 we made sure that it has got enough space.

No need to cast malloc() retrun value. In sizeof( temp) + 1 1 is added for null character. Then we did memset() which filled the memory pointed to by pointer1 with null. Just good and healthy practice.

Then you

memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)

Again, does pointer1 has enough space for contents of pointer3? Do you own the memory area pointed by pointer1 + sizeof(temp)? It too will crash your program.
Now you either use realloc() or assign a bigger space to pointer1 with malloc() at earlier stage.

Why sizeof ( the temp array) here? Don't you think it should have the number of bytes in pointer3?

Finally in the definition of function1()

char *pointer2, *array2;
 // Now i need to have pointer2 point to contents of array2.
 pointer2 = &temp2;
 return pointer2 

What does array2 do? Nothing! Then it should be removed.
To return just use

return temp2;

which means pointer2 is also useless.

Hope it helps.

傻比既视感 2024-09-13 19:12:35

目前还不清楚您想要做什么,但我认为这就是您想要的。我对其进行了评论以显示它在做什么。

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);
}

It's not really clear precisely what you're trying to do, but I think this is what you're after. I've commented it to show what it's doing.

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);
}
手长情犹 2024-09-13 19:12:35

我似乎无法理解你的代码。问题很简单,但是代码无法运行。
接下来是这段代码
memcpy (pointer1 + sizeof(temp),pointer3,sizeof(temp array)

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

I cant seem to understand your code. The problem is simple but the code wont work.
Next is this piece of code
memcpy ( pointer1 + sizeof(temp), pointer3, sizeof ( the temp array)

I guess you want the final array to contain { a,b,c,d,e,1,2,3,4,5}.
Well i would advise you to check pointers and arrays from K&R and then attempt this problem again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文