Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:04:18+00:00 2026-05-15T10:04:18+00:00

Basically in the code below, my final array does not seem to have the

  • 0

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]);
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:04:19+00:00Added an answer on May 15, 2026 at 10:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 429k
  • Answers 429k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Presumably you are somewhere inside a ChangeListener's stateChanged method -… May 15, 2026 at 1:39 pm
  • Editorial Team
    Editorial Team added an answer This answer is more focused on the theory aspect of… May 15, 2026 at 1:39 pm
  • Editorial Team
    Editorial Team added an answer Your "host" isn't causing the issue, PHP is appending this… May 15, 2026 at 1:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.