A little stuck.. is this possible?

This is a discussion on A little stuck.. is this possible? within the C++ Programming forums, part of the General Programming Boards category; I'm a little stuck on this, I need to declare an array of char pointers, but I want to do ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    A little stuck.. is this possible?

    I'm a little stuck on this, I need to declare an array of char pointers, but I want to do this in initialization, is this possible?
    I tried char* array[size]; But the program just crashes when I try to set an element to a string(char array), although syntactically it is correct. I'm guessing it is just another way of saying char* array = new char[size]? I'm not sure but is there anyway I can do this without going through for loops and such.

    Also, just as a remimder, you only need to delete a pointer when you allocate memory to it with new, correct? As with malloc() and free()?
    Using Dev-C++ on Windows

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Post a code sample of what you are trying to do that crashes. It is likely that you are not dynamically allocating memory to the individual array elements when you attempt to copy over your strings.

    Also, just as a remimder, you only need to delete a pointer when you allocate memory to it with new, correct? As with malloc() and free()?
    Yes.

    [edit]size is not a constant or define is it? In that case, in order to dynamically allocate an array of pointers, you would have to do this:

    Code:
    int size;
    ...
    char** array = new char*[size];
    Then you would still need to allocate memory to the individual cells of the array:

    Code:
    for( int a = 0; a < size; ++a )
        array[a] = new char[10];  // Or whatever length you want for the individual cells
    When you delete this memory you'd need to first loop through the cells of the array and free/delete the memory allocated to the cell, then you'd be able to free/delete the array variable.
    [/edit]
    Last edited by hk_mp5kpdw; 07-20-2005 at 01:43 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Well the problem I had it I was able to fix as I only need one char*, but I coming to needing it somewhere else now. Anyway I went back and changed the char* to a char* array of 50 and just used the 0 element. Here's the code:
    Code:
    char* lines[50];
        file = fopen("settings.cfg", "r");
        if (file == NULL) {
            writeConfig(*settings);
            return false;
        }
        while (fscanf(file, "%s", lines[0]) != EOF) {
            if (stricmp(lines[0], "screenRes") == 0) {
                fscanf(file, "%s", lines[0]);
                if (stricmp(lines[0], "=") == 0)
                    fscanf(file, "%d %s %d", &settings->video->width, lines+0, 
                        &settings->video->height);
                else
                    return false;      
            } else if (stricmp(lines[0], "bitDepth") == 0) {
                fscanf(file, "%s", lines+0);
                if (stricmp(lines[0], "=") == 0)
                    fscanf(file, "%d", &settings->video->bits);
                else
                    return false;
            } else if (stricmp(lines[0], "zBBuffBits") == 0) {
                fscanf(file, "%s", lines[0]);
                if (stricmp(lines[0], "=") == 0)
                    fscanf(file, "%d", &settings->video->zBits);
                else
                    return false;
            } else if (stricmp(lines[0], "viewDist") == 0) {
                fscanf(file, "%s", lines+0);
                if (stricmp(lines[0], "=") == 0)
                    fscanf(file, "%f", &settings->video->viewDistance);
                else
                    return false;
            } else if (stricmp(lines[0], "fullscreen") ==0) {
                fscanf(file, "%s", lines[0]);
                if (stricmp(lines[0], "=") == 0)
                    fscanf(file, "%d", &settings->video->fullscreen);
                else
                    return false;
            }
        }
    When I just use a char* and use stricmp() and fscanf() on it, it's fine. But declaring that array and trying to set or access an element in the array causes it to crash.
    Edit: Not sure if anyone noticed, but I had file+0 at the end, I took it out, and it still crashes, so that's not really the problem, if you caught it.
    Last edited by Ganoosh; 07-20-2005 at 02:01 PM.
    Using Dev-C++ on Windows

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char* lines[50];
        file = fopen("settings.cfg", "r");
        if (file == NULL) {
            writeConfig(*settings);
            return false;
        }
        while (fscanf(file, "%s", lines[0]) != EOF) {
    You haven't allocated any space to scan into. All you've got is 50 pointers to characters. You have to allocate space for each one of those.

    Let's assume you like malloc.
    Code:
    char *array[ SOMESIZE ]; //create an array of SOMESIZE pointers to charactes
    size_t x;
    
    for( x = 0; x < SOMESIZE; x++ )
         array[ x ] = malloc( SOMESTRINGLENGTH ); //allocate space for this array's string
    
    ...do whatever...
    
    for( x = 0; x < SOMESIZE; x++ )
        free( array[ x ] );
    That would be the correct way to do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    So I do have to use a for loop ok thanks.
    Using Dev-C++ on Windows

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You could use a while loop, or a do-while. Or you could do it manually one line at a time. It's just easy to do it with a for loop. But if you don't like loops for some reason...
    Code:
    array[ 0 ] = (char *) malloc( SOMELENGTH ); // C++ likes you to cast malloc.
    array[ 0 ] = (char *) malloc( SOMELENGTH );
    array[ 1 ] = (char *) malloc( SOMELENGTH );
    array[ 2 ] = (char *) malloc( SOMELENGTH );
    array[ 3 ] = (char *) malloc( SOMELENGTH );
    array[ 4 ] = (char *) malloc( SOMELENGTH );
    ... and so on ...
    Then just free them all one at a time. Though why you don't want to just use a loop is beyond me.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    lol, yeah I know I just mean I need to allocate each one in some form, I just wanted to see if it was possible to allocate the whole lot of them along with the array at initialization, but I guess it's not possible. I said for specifically for the reason you said, it's easier and usually you'd use a for loop for that kind of thing I was just trying to avoid allocating each elements pointers alltogether.
    Using Dev-C++ on Windows

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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