Results 1 to 6 of 6

Thread: c string array question

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    c string array question

    I'm trying to write a program where I need to have a function manipulate an array of strings. Currently, I have something like this set up:

    Code:
    void stringManipFunc (char *strings[]){
    
    
    // add some user input data into the 
    // strings array
    
    }
    
    int main (){
    
    char *mystrings[256];
    
    stringManipFunc (mystrings);
    
    // do some other stuff with mystrings
    
    }
    But this setup doesn't work. The string array is passed to the function, but what ever the function does is not reflected in the string array when it gets back to main's control. What am I doing wrong? Is there a better way to do this? Perhaps stringManipFunc could return something rather than messing with the array by reference. Any ideas? Thanks for your help.

  2. #2
    Guest

    Re:c string array question

    I would at least be nice to return and int for error handling.

    But how about something like this (no garanties it will work, coding as while typing)

    Code:
    #include <iostream>
    #include <cstring>
    
    /* C would #define these right */
    const short int good = 1;
    const short int bad = 0;
    const short int unknown = -1;
    
    // Prototype
    short int stringManipFunc (char *string);
    
    int main (void)
    {
         unsigned int lenght; 
         cin >> lenght;  /* would be stdin in C? */
       
        /* does C understand new and delete? C++ does
    anyways */
    
        new = char mystring[lenght];
        assert( mystring != 0)
    
        short int returned; 
        returned = stringManipFunc(&mystring);
          
        cout << mystring;
    
        delete mystring
        return 0
       }
    
    short int stringManipFunc (char *string)
    {   
       /* uppercase all these */
       toupper(string);
       return good;
    }

  3. #3

    Re:c string array question

    [quote author=t048 link=board=9;threadid=4100;start=0#40621 date=1026167495]
    What am I doing wrong?
    [/quote]

    Post your code - we'll need to know what you're trying to do inside the stringManipFunc(). Ya know, my Crystall Ball is broken ;-)

    Lovechild - you've written your example in C++ !

  4. #4
    Guest

    Re:c string array question

    [quote author=andrzej link=board=9;threadid=4100;start=0#40980 date=1026417283]
    [quote author=t048 link=board=9;threadid=4100;start=0#40621 date=1026167495]
    What am I doing wrong?
    [/quote]

    Post your code - we'll need to know what you're trying to do inside the stringManipFunc(). Ya know, my Crystall Ball is broken ;-)

    Lovechild - you've written your example in C++ !
    [/quote]

    I know but the nearly the same when it comes to string handling, I just dynamically allocate them because that's the way I like to do it, he can use [256] or malloc if he wants to.

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    Re:c string array question

    I figured it out. Thanks for the hints, all. The problem wasn't in my function! The really weird part was I wasn't have the problem on my home machine (running Mandrake), but I was having problems on Solaris. I don't know of that is good or bad. Here is what I had:

    Code:
    // includes omitted
    
    void stringFunc (char *strarr[]){
    
       char string[256];
       int i;
    
       gets (string);
    
       i = 0;
       strarr[i] = strtok (string, " ");
       while (strarr [i] != 0){
          ++i;
          strarr[i] = strtok (NULL, " ");
       }
    
    }
    
    void main (){
    
       char *mystringarray[256];
    
       stringFunc (mystringarray);
    
       // do stuff with mystringarray
    }
    I'm sure you c/c++ gurus can see the error already, but for someone with no c/c++ training such as myself, the error wasn't easy to find. However, in the process of trying to figure this out, I read a large amount about memory and pointers and so on, so I learn quite a bit. Here are the changes I made:

    Code:
    // includes omitted
    
    void stringFunc (char *strarr[], char *string){
    
       int i;
    
       gets (string);
    
       i = 0;
       strarr[i] = strtok (string, " ");
       while (strarr [i] != 0){
          ++i;
          strarr[i] = strtok (NULL, " ");
       }
    
    }
    
    void main (){
    
       char *mystringarray[256];
       char string[256];
    
       stringFunc (mystringarray, string);
    
       // do stuff with mystringarray
    }
    Now it seems to work OK. This sure wasn't a fun problem to track down. I went to my instructor for help and he wouldn't help me because he never saw me in the computer lab (he is only in the computer lab in late afternoons and I'm in there in the mornings), so he didn't think I was really working hard enough.

    Just a side question that came up during all of this:

    Code:
    char *mystringarray[256]
    Essentially creates a two dimensional array of characters, which we call a string array. Is this an array of 256 strings or an array of strings each with a length of 256? I'm pretty sure its the former because it is an array of character pointers (which point to the strings). Is that correct?

  6. #6

    Re:c string array question

    [quote author=t048 link=board=9;threadid=4100;start=0#41071 date=1026485135]
    Code:
    char *mystringarray[256]
    Essentially creates a two dimensional array of characters, which we call a string array. Is this an array of 256 strings or an array of strings each with a length of 256? I'm pretty sure its the former because it is an array of character pointers (which point to the strings). Is that correct?
    [/quote]

    It's an array of 256 pointers to variables of type char. It allocates memory for 256 pointers to chars only.

Similar Threads

  1. find a string
    By svar in forum Linux - General Topics
    Replies: 2
    Last Post: 05-01-2009, 08:21 PM
  2. San Array slowdown
    By DotHQ in forum Linux - Hardware, Networking & Security
    Replies: 4
    Last Post: 03-07-2006, 02:23 PM
  3. Converting from an int to a string in C?
    By norfolknchance in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 03-19-2003, 11:05 PM
  4. String/Char Array commands ...
    By bafalouk in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 09-26-2002, 12:10 PM
  5. String of text into file?
    By MZGuy in forum Linux - General Topics
    Replies: 8
    Last Post: 01-03-2002, 09:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •