#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    8
    Rep Power
    0

    arrays and pointers


    Ok: I'm having issues with pointers and multidimensional arrays.
    I need to decide on run-time which array to use.
    In : syntax.h:
    char *c_syntax[2] = {"word", "test" };
    char *d_syntax[2] = {"c", "php" };
    (add 18 more arrays here)

    In : file.c:
    Code:
    #include <stdio.h>
    #include "syntax.h"
    int main()
    {
        int i;
        int j;
        /* 
         * Need to point 'syntax' to a double array (decided on run-time) 
         * I have 10-20 arrays that *syntax can point to
         * This is where it causes problems: 
         */
        char *syntax;
        syntax = c_syntax; /* just for testing purposes*/
        
        printf("Starting test\n");
        /* 
         * Loop through and print everything in c_syntax 
         * If I use c_syntax in the loop here the code works
         */
        for(i = 0; i < 2; i++){
            for(j = 0; j < strlen(syntax[i]); j++){
                printf("%c", syntax[i][j]);
            }
            printf("\n");
        }
        return 0;
    }
    Now: what can I do to just work with the 'syntax' pointer instead of 20 different arrays in my program??

    Thanks for any help on this issue, I've just started with C, pointeres normally don't confuse me, but this has bugged me a while now.
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    First off, since c_syntax is an array of pointer to char (char *c_syntax[]), syntax should be either a pointer to pointer to char (char **syntax), or an array of pointer to char (char *syntax[]).

    Here's an example that uses a single 'syntax' array:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main (int argc, char **argv)
    {
            char *syntax[4][2] = {
                    { "word", "test" },
                    { "c", "php" },
                    { "c++", "java" },
                    { "python", "perl" }
            };
    
            int i;
    
            for (i = 0; i < 4; i++) {
                            printf("%s and %s\n", syntax[i][0], syntax[i][1]);
            }
    
            return 0;
    }
    I hope this makes sense. If not, ask away :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    8
    Rep Power
    0
    Thanks!!
    I'm getting there now, I have the arrays in the header file going smooth (a couple of them are 2500 variables+)
    So: how would I go about to malloc all these? I know how to parse stuff in C, but memory management is *not* my best side.
  6. #4
  7. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    I'm not quite following you, there. If syntax.h contains array declarations like c_syntax, with initial values and all, then there's no sense in using malloc() on them.

    If you're not initialising them in the header file, then you'll need to populate the arrays yourself. Assuming declarations like char *c_syntax[2], you end up arrays of pointer-to-char. That means you only need to allocate space for the actual strings; the array declarations create room for the pointers.

    For example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 2
    
    int
    main (int argc, char **argv)
    {
            char *c_syntax[SIZE];
            char buf[8192]; /* should be enough for most strings :) */
            int i;
    
            for (i = 0; i < SIZE; i++) {
                    fgets(buf, sizeof(buf), stdin); /* should check return code */
                    c_syntax[i] = malloc(strlen(buf) + 1); /* again, should check for errors */
                    strcpy(c_syntax[i], buf);
            }
    
            /* print array in reverse */
            for (i = SIZE; i > 0; i--) {
                    printf("%s", c_syntax[i-1]);
            }
    
            return 0;
    }
    Instead of using malloc() and strcpy() you could also use strdup(), which both allocates memory and copies the string in one fell swoop :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    8
    Rep Power
    0
    Thanks a lot for that!
    When you're coding in PHP/Java all day, it is a bit of a head change to go to C :)
    Everything is indeed declared in the syntax.h file, so I should be alright here.
    This is my first serious C project ( just wait :) , it's actually a useful application!), so thanks for the help again!

IMN logo majestic logo threadwatch logo seochat tools logo