Thread: array of strings + more

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Question array of strings + more

    hi. got a question on arrays of strings once again...

    the declaration:

    char *str_array[4]

    means undefined length or individual strings, but 4 strings in total right?

    if this is the case, how do you declare the opposite, ie fixed (staticaly allocated) mem for the individual strings (same length all of them), but dynamic mumber of strings?


    also another thing. suppose that you wanna make console menu and you got say submenu1 and submenu2 called from the main menu.

    is there a way to change from submenu1 to submenu2 without calling say submenu2 from within submenu1 and the opposite? one way would be to go back to the main menu and from there to another sub, but is there any way not to go through the parent menu and without calling sub2 from sub1 etc?

    both sub1 and sub2 contain link to eachother but calling sub2 from sub1 and then sub1 from within sub2 etc doesnt look right...


    thanx in advance for help.

    null

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    strings

    You should check out malloc(), calloc() and friends. You might be able to try something like

    Code:
    char **str_array;
    str_array[0] = (char *) malloc(10);
    to make the first string in str_array a string of 10 bytes.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how do you declare the opposite, ie fixed (staticaly allocated) mem for the individual strings
    Well you can't allocate them statically, but you can tell the compiler that all the strings will be the same length.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STR_LEN     9
    #define NUM_STR     4
    
    int main ( ) {
        char    *str1[NUM_STR];   // an array of pointers to chars
        char    (*str2)[STR_LEN]; // a pointer to an array of chars
        int     i;
    
        // allocate space for 4 pointers
        for ( i = 0 ; i < NUM_STR ; i++ ) {
            str1[i] = malloc( STR_LEN * sizeof(char) );
        }
    
        // allocate space for 1 pointer
        str2 = malloc( NUM_STR * sizeof(char[STR_LEN]) );
    
        // both are now equivalent to strings[NUM_STR][STR_LEN]
        for ( i = 0 ; i < NUM_STR ; i++ ) {
            sprintf( str1[i], "string %d", i );
            strcpy( str2[i], str1[i] );
            printf( "%s\n", str2[i] );
        }
    
        return 0;
    }
    > suppose that you wanna make console menu and you got say submenu1 and submenu2
    If the menus are linked by forward and reverse pointers, then the routine you use for navigating/drawing menus could automatically insert the appropriate links for you, rather than you having to do it all manually.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Thumbs up

    thank you for your answers.

    Salem, you are da man. didnt know this type of declaration:
    (*str2)[STR_LEN];

    not quite sure what you mean for the second part though. are you talking about pointers to functions?

    thanks

    null

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (*str2)[STR_LEN];
    Read the comment again - this is a pointer to an array of chars. You need the extra () to get the precedence right, otherwise its just an array of pointers.

    Try and see past the array part, and just think about it in terms of a pointer to any multi-byte object like an int or a structure. The pointer still points to the first byte, but in terms of indexing, pointer arithmetic etc, C treats the whole lot as a single indivisible entity.
    str1++ would point to the next char in the current string
    str2++ would point to the next string (as you would normally expect if it was a pointer to an int or a struct).

    Here's a clearer example without the malloc...
    Code:
    int main ( ) {
        char    *str1;          // pointer to a char array (of any size)
        char    (*str2)[10];    // pointer to a char[10] array
        char    string[10] = "hello";
    
    //  char    bogus[20] = "world";
    //  str1 = bogus;           // this is legal
    //  str2 = &bogus;          // this isn't legal - the array sizes differ
    
        // point to first element
        // there is no information in the pointer to tell us how big the
        // thing being pointed at is.
        str1 = string;          // array names are pointers to the first element
        str1 = &string[0];      // ie, equivalent to this
    
        // point to the whole array
        str2 = &string;         // note - no [0]
    
        printf( "str1 points to %s\n", str1 );
        printf( "str1 points to %s\n", &str1[0] );
        printf( "str2 points to %s\n", *str2 );
        printf( "str2 points to %s\n", str2[0] );
    
        return 0;
    }
    > are you talking about pointers to functions?
    Pointers to functions have () instead of [] in the declaration
    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.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Salem thanx again for the info. got it now. this is a realy good example, i already printed it out... (i keep folder with usefull c examples for quick reference.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There is nothing wrong with calling one function from within the other and and from that one to the first, etc. You can even call a function from within itself, very useful for menus.

    Not only are these Legal, they are the foundations of routine writing!

    A few things to watch out for though when doing this. Sometimes you "work yourself into a corner" where you cannot proceed because you are being redirected to the next which redirects you to the last and so on, but you will be able to iron out these fairly easily.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Sebastiani, see what you mean. do you know, if you call a function say func2() from whithin funct1(), is there any difference between calling func2() by name and calling func2() through a pointer to it?

    In particular, if func2 has a number of staticaly allocated local variables is there any difference in the memory allocation of them between the two cases? is memory allocated by the compiler in the same way?

    if you have an embedded platform with fixed stack size etc, do you gain anything in using function pointers instead of calling by name? i'm talking about the same case where func1 calls func2 which in turn calls func1 and so on.

    thanks a lot

    null

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there any difference between calling func2() by name and calling func2() through a pointer to it?
    Functionally no.
    There's probably a couple of clock ticks difference in performance, but its hard to say which would be quicker in any given implementation. You'd be hard pushed to ever see a visible difference between the two.

    > is memory allocated by the compiler in the same way?
    Yes
    You can call the same function directly, and via a pointer in the same program, and the same thing will happen in both cases.

    > do you gain anything in using function pointers instead of calling by name?
    Nope - stack space usage is the same
    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.

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Post Re: strings

    before using str_array[0]
    dont you need to do something like

    str_array = malloc( 3*sizeof (*str_array) );

    Originally posted by Tiger
    You should check out malloc(), calloc() and friends. You might be able to try something like

    Code:
    char **str_array;
    str_array[0] = (char *) malloc(10);
    to make the first string in str_array a string of 10 bytes.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    32
    Salem, once again thank you for your answer. that's all i wanted to know about if (for the moment...) :-)

    pinko_liberal, yes you got to do that. first goes the array of pointers and then the individual strings. got to remember to free them as well in reverse order...

    regards

    null

Popular pages Recent additions subscribe to a feed

Similar Threads

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