char**

This is a discussion on char** within the C Programming forums, part of the General Programming Boards category; I am trying to use a char** in my program, but my program crashes. First I use this: char** Line; ...

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    Lightbulb char**

    I am trying to use a char** in my program, but my program crashes.

    First I use this:

    char** Line;

    FileSize = GetFileSize(hFile, NULL);
    Line = Memory(FileSize*sizeof(char*));

    Later in my program I use:

    strcpy(Line[CurLine], Buffer);

    This causes the program to crash. What is wrong here?
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char** Line;

    strcpy(Line[CurLine], Buffer);

    This causes the program to crash. What is wrong here?
    You're only allocating pointers not space. You're copying to some random pointer some place in memory. Allocate space for "Line[CurLine]" pointer then copy into it.

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I dont understand why your declaring a char **Line; if this is your declaration for your pointer then it should be simply char *Line

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I dont understand why your declaring a char **Line; if this is your declaration for your pointer then it should be simply char *Line
    Basicly they want an array of lines. Each line is a character pointer. Think of it like this:

    We have a text file. It has X number of lines in it. Each line is a different length (possibly) and we want to be memory-efficient.

    We create an array who's size is the exact size as the number of lines, then for each line, we allocate space for it, and read it into this line.

    His problem is that he just isn't allocating space for the line.

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

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    Thanks

    Ok, Thanks for your help.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

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