intializing an array of strings

This is a discussion on intializing an array of strings within the C Programming forums, part of the General Programming Boards category; Hi I have a string , out of which iam trying to extract relevant substrings. the substrings are being stored ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    intializing an array of strings

    Hi I have a string , out of which iam trying to extract relevant substrings. the substrings are being stored in an array of strings, now my problem is that initially i need to push the initial string as one of the elements of the array of substrings ( the original string it self), i.e in my array of substrings, the first element should be the string it self.

    please help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Maybe if you posted some actual code, we could home in on the real issue.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    I am not sure what you are saying but here is a way to store strings in an array.

    Code:
    char aS[3][20];
    strcpy(aS[0],"First");
    strcpy(aS[1],"Second");
    strcpy(aS[2],"Third");
    printf("%s,%s,%s\n",aS[0],aS[1],aS[2]);
    The first set of [] is which string you are reading, the second [] is the number of chars it can hold. To pass it to another function you only need to put it as
    Code:
    function(&aS);
    Hope that helped
    RMDan

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Actually, to pass it to another function you'd more likely use
    Code:
    $ cat mda.c
    #include <stdio.h>
    
    void print(char data[][20], int number) {
        int x;
        for(x = 0; x < number; x ++) {
            printf("data[%d] = \"%s\"\n", x, data[x]);
        }
    }
    
    int main() {
        char data[][20] = {
            {"one"},
            {"two"},
            {"three"}
        };
        print(data, sizeof(data)/sizeof(*data));
        return 0;
    }
    $ ./mda
    data[0] = "one"
    data[1] = "two"
    data[2] = "three"
    $
    Note the lack of an ampersand, and the fact that you have to pass the size to the function. The function only receives a pointer; it has no way of knowing how many elements are in the array. Also, I've used array initialization here, but strcpy() works as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm guessing that if you need to "push" a string on the top then an array is the wrong thing to use; maybe a stack or a linked list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  2. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10:35 PM
  3. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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