Thread: Will this work?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Will this work?

    Sorry, but I'm not at a computer with a compiler right now and I want to know if this algorithm will work in my latest program.
    Code:
    char * base[8] = {0};
    char * pBase = 0;
    char * buf[4];
    char   buf2[1024] = {0};
    int    i;
    
    fgets(buf2, 1024, '\n');
    pBase = strtok(buf2, ' ');
    for(i = 0; i < 8 && *p != '\0'; i++){
        base[i] = *p;
        pBase = strtok(NULL, ' ');
    }
    
    for(i = 0; i < atoi(base[7]); i++){
        fgets(buf[i], 1024, '\n');
    }
    This code reads a string into a buffer, then parses the tokens in that string into an array of pointers to char. One of the tokens will determine how much of the file to read after parsing the string and that value is used as a condition for a loop to read the rest of the file needed.

    The big question is, will this work?
    C code. C code run. Run code, run...please!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Apart from a bunch of syntax errors, and problems with your fgets calls - the algorithm itself seems OK

    > fgets(buf[i],
    except buf[i] doesn't point to a block of 1024 bytes

    And
    > atoi(base[7])
    had better be < 4, otherwise buf[i] has an out of range subscript problem as well.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    I can find the syntax errors, but I'm a bit confused by your other comments. I checked my book and found that fgets should be
    fgets(string, int, stream);
    so would that mean my call would have to be
    fgets(buf[i], '\n', stdin); ?
    The second argument is what confuses me, is it the number of characters to be read or something of a delimiter?

    >And
    >> atoi(base[7])
    >had better be < 4, otherwise buf[i] has an out of range >subscript problem as well.

    You lost me here. base[7] is a pointer to a string. Converted to an integer by atoi it would yield a value that could be used as a condition for the loop. Since the array of pointers is of size 8, making a call for the 7th element should access the last element, right?

    Just to double check,
    char * buf[4];
    is an array of 4 pointers that can be assigned strings?
    C code. C code run. Run code, run...please!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so would that mean my call would have to be
    > fgets(buf[i], '\n', stdin); ?
    No,
    fgets( buff, sizeof(buff), stdin );

    The 2nd parameter is the number of bytes which can be written into the buffer (the 1st parameter), reading from the FILE* stream (the 3rd parameter)

    You can't choose the delimiter for fgets - it's always '\n'

    > making a call for the 7th element should access the last element, right?
    Yeah, that bit is OK - base[7] is the last element of the base array,

    But, buf[i] (where you have char * buf[4]) has only 4 elements, so the limit of the loop should be 4

    > char * buf[4];
    > is an array of 4 pointers that can be assigned strings?
    Yes - but you don't assign any strings, and fgets won't do it for you either.
    Code:
    for(i = 0; i < atoi(base[7]); i++){
        buff[i] = malloc( 1024 );
        fgets(buf[i], 1024, stdin );
    }

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    >But, buf[i] (where you have char * buf[4]) has only 4 elements, >so the limit of the loop should be 4

    I see what you mean by that, but the value of base[7] wouldn't be any higher than 4, so I would essentially be saying:
    for(i = 0; i < j; i++){
    where j is an unknown value in the range of 1 to 4. So after parsing the buffer, the program reads at least one more line and then possibly more according to that value. In the final program I intend to error check this and make sure that the value is within the range of base.

    And for an array of pointers to char, I need to allocate memory for every pointer before assigning it, yes? Just the same as I would have to allocate memory for a single pointer to char that I want to point to a string later?
    Code:
    for(i = 0; i < atoi(base[7]); i++){
        buf[i] = malloc(1024); /*Allocates 1024 bytes?*/
        fgets(buf[i], sizeof(buf), stdin );
    }
    C code. C code run. Run code, run...please!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I see what you mean by that, but the value of base[7] wouldn't be any higher than 4
    That is fine then

    > I need to allocate memory for every pointer before assigning it, yes?
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM