Appending to a String Array

This is a discussion on Appending to a String Array within the C Programming forums, part of the General Programming Boards category; How would I append to the first element of a string array? So suppose I have this: Code: char *array[ ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Question Appending to a String Array

    How would I append to the first element of a string array? So suppose I have this:
    Code:
    char *array[ 10 ];
    Indexes 0-3 contain strings. The rest are empty. How do I create a function that reads every index in the array and writes to the first empty one?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    I don't get it. You described the problem and a method for solving it.

    Read every index in the array and write to the first empty one.

    What, do you want us to code it for you?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    You misunderstand what I'm asking. I want to know how I'd do this. What string function is appropriate for this sort of thing? Would I use strlen to test whether it's empty?

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    When creating the array set empty slots to NULL, then when searching for a space to write too just search for NULL.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    Thanks, I'll try it.

    [EDIT] Worked! Thanks, Valaris. Silly me for not thinking of something as simple as that...
    Last edited by Bomb; 08-24-2008 at 11:43 AM.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    np. Heres a simple example anyways.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void fillPrint(char ** testing);
    
    int main()
    {
    char * test[10];
    memset(test, 0, 10);
    test[0] = "Hi";
    
    fillPrint(test);
    }
    
    void fillPrint(char ** thesting)
    {
    int i;
    for ( i = 0; i <10 && thesting[i] != 0; i++)
    {
    printf ("&#37;s\n", thesting[i]);
    }
    thesting[i] = "sup";
    printf("%s\n", thesting[i]);
    }
    This example will need some array bound checking if it is going to really be used when adding strings.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Better make that const char to avoid silly mistakes as modifying it when no memory is actually allocated.
    That or actually allocate memory first.
    I'd also make a constant with the array size and re-use that instead of magic numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > memset(test, 0, 10);
    How many bytes does a pointer have?
    And how is it represented?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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