Thread: How to copy the content of an array into a pointer

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Question How to copy the content of an array into a pointer

    good day ladies and gentlemen.
    i need your guide on how to copy the content of an array to a pointer.I want to send this command(<DC2>,1,S)%256 to a device.below are my codes but it was not giving me the desired resul when i ran it.i have very little experience in C programming and i will be very glad if somebody could guide me through.
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    #define  <DC2>    12
    #define    S         53
    
    char    SendCommand[3]={12,1,53};//  i arranged the command in                                                  //array format
    char   *TempData;
    char   *ActualData;
     
    int     i;
              TempData=SendCommand;
              for(i=0;i<3,i++)
           {  ActualData=TempData;
               *TempData++
          
           }
    
         getch();
         return 0;
    }
    what i intend to achieve with the above code is to copy the content of my array SendCommand[] into the pointer *ActualData.
    I have very little programming experience,i will be very grateful if someone could help me out.
    Best regards to all.
    firstoption.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Those macros don't even make sense.

    If you want to copy the array to a pointer, there are two main steps

    1) Ensure the pointer points at a valid area of memory that can hold a copy of all elements of the array.

    2) Copy each element of the array, individually, to the memory pointed to

    For example, in some function (using malloc() to allocate memory, and free() to deallocate).
    Code:
    char    SendCommand[3]={12,1,53};
    char   *CopyCommand = malloc(3);          /* assume <stdlib.h> is included */
    
    int i;
    
    for (i = 0; i < 3; ++i)
      CopyCommand[i] = SendCommand[i];
    
    /*   use CopyCommand for something */
    
    free(CopyCommand);   /*   release CopyCommand as we no longer need it */
    Try digging out a basic textbook on C. Even the worst such books out there will help you improve.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by firstoption View Post
    what i intend to achieve with the above code is to copy the content of my array SendCommand[] into the pointer *ActualData.
    I have very little programming experience,i will be very grateful if someone could help me out.
    Best regards to all.
    firstoption.
    Maybe you should solve your problem by trying to gain some more programming knowledge vice just throwing code at the computer. Here are some useful links, I suggest you read through them and then maybe start back with a simple "Hello World" program and work from there:
    1. C Made Easy Tutorials
    2. Teach Yourself C in 21 Days
    3. Steve Summit's Intro to C Notes

    That should get you going in the right direction. Additionally, getch() is non standard and should be avoided. You can read How to Keep my Console Open - FAQ for different alternatives.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to copy the content of two arrays in third?
    By Razgul in forum C Programming
    Replies: 3
    Last Post: 12-18-2010, 07:41 PM
  2. Copying content of file into a 2D array
    By trueman1991 in forum C Programming
    Replies: 10
    Last Post: 12-16-2009, 12:42 AM
  3. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM
  4. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 12:33 AM
  5. changing the array content !!
    By mehuldoshi in forum C Programming
    Replies: 2
    Last Post: 06-29-2002, 06:33 AM