appending to an array

This is a discussion on appending to an array within the C Programming forums, part of the General Programming Boards category; I am going to sound really thick, so sorry!! I have an array and I have filled it with elements ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    appending to an array

    I am going to sound really thick, so sorry!!

    I have an array and I have filled it with elements (as in E in the example below) I would like then to add that array to another array and just add an element at the beginning or the end and would like to deal with the array as the whole instead of using a for loop to deal with each individual element in the array.

    Is there a way I have tried F={23,E} but obviously this doesn't work. Any help would be great

    nice

    int i;
    int E[9];
    int Fup[10];
    int Fdown[10];

    for(i=0;i<9;i++)
    E[i]=i;

    Fup= {E,23};
    Fdown={23,E};

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C doesn't support such a thing. You'll need to use a function to do it for you. Conveniently enough, memcpy will do this for you:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int
    main(void)
    {
      int a[10] = {1,2,3,4,5};
      int b[] = {6,7,8,9,10};
      int i;
    
      memcpy(a + 5, b, 5 * sizeof(int));
      for (i = 0; i < 10; i++) {
        printf("%d\n", a[i]);
      }
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21