new member + array of structs question

This is a discussion on new member + array of structs question within the C Programming forums, part of the General Programming Boards category; Hello all, I'm a new member in this website, hope I can help and be helped in this wonderful place. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Jerusalem
    Posts
    3

    new member + array of structs question

    Hello all,
    I'm a new member in this website, hope I can help and be helped in this wonderful place.
    My name is Maroun, a computer engineering student. My age is 25.

    Now to the question.

    I have this struct:
    Code:
    typedef struct cell {
        char _name[3];
        int _calcResult;
        int _depend[MAX_CELLS];
    } Cell;
    and this is my main:
    Code:
    int main(int argc, char* argv[]) {
        FILE *fp;
        int i;
        fp=fopen(argv[1], "r");
        Cell Cells[MAX_CELLS];
        fillNames(fp,Cells); 
        for(i=0;i<4;i++)
            printf("%s\n", Cells[i]._name);
    }
    Code:
    static void fillNames(FILE* fp, Cell *Cells) {
        char line[3];
        int i=0;
        while(fgets(line, sizeof line, fp)!=NULL) {
            strcpy(Cells[i]._name, line);
            i++;
        }
    }

    I have an array of structs and I'm trying to insert the first two chars from each line from the file into the _name field in the struct inside the array.

    I get this message:
    Segmentation fault.

    (I checked the file, and checked the parameters.. I just didn't wrote it here)

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Where is MAX_CELLS defined?

    Also you should lose the underscores inside the struct... cell.name is adequate the underscore is superfluous.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Jerusalem
    Posts
    3
    Thanks for the reply.
    I didn't post the whole code, all the constants are defined.. there are no errors in the code.
    I just keep getting "Segmentation fault" when I'm trying to run it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >all the constants are defined
    I'd be more worried about MAX_CELLS not being large enough. Your loop isn't limited by the size of the array, so it'll keep going until fgets says there's nothing left. Does your seg fault go away if you use this function?
    Code:
    static void fillNames(FILE* fp, Cell *Cells) {
        char line[3];
        int i=0;
        while(i < MAX_CELLS && fgets(line, sizeof line, fp)!=NULL) {
            strcpy(Cells[i]._name, line);
            i++;
        }
    }
    My best code is written with the delete key.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by maroun View Post
    Thanks for the reply.
    I didn't post the whole code, all the constants are defined.. there are no errors in the code.
    I just keep getting "Segmentation fault" when I'm trying to run it.
    Believe me, if you are getting seg-faults, there is an error in the code.

    Lood at your fillnames() procedure...
    What happens if there are more names in the file than cells in your array?

    Code:
    static void fillNames(FILE* fp, Cell *Cells) {
        char line[3];
        int i=0;
        while((fgets(line, sizeof(line), fp)!=NULL) && (i < MAX_CELLS)) {
            strcpy(Cells[i]._name, line);
            i++;
        }
    }
    Also in the struct itself and in your subroutine is 3 characters enough space for every name? If a name is longer than 2 chars it will overflow the char array... Don't forget C strings have trailling nulls on them... so your string buffers always have to be long enough to hold the longest string plus 1 extra character for the null.

    What is the longest input line you might read from your file?
    Last edited by CommonTater; 11-03-2010 at 12:53 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If a name is longer than 2 chars it will overflow the char array...
    If a name is longer than two characters the buffer will simply be filled with two characters. That's why fgets is recommended after all. There may be a logical error involved here, where the results are completely whack due to longer names than expected, but there's no buffer overflow when reading names.
    My best code is written with the delete key.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prelude View Post
    >If a name is longer than 2 chars it will overflow the char array...
    If a name is longer than two characters the buffer will simply be filled with two characters. That's why fgets is recommended after all. There may be a logical error involved here, where the results are completely whack due to longer names than expected, but there's no buffer overflow when reading names.
    What part of

    "If a name is longer than 2 chars it will overflow the char array... "
    and
    "so your string buffers always "

    did you not understand?
    Last edited by CommonTater; 11-03-2010 at 01:08 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by CommonTater View Post
    What part of

    "If a name is longer than 2 chars it will overflow the char array... "
    and
    "so your string buffers always "

    did you not understand?
    Rather than being an ass, why don't you clarify what you meant by "will overflow the char array", because that usually means writing to an index that doesn't exist. I can come to only two conclusions:
    1. You're using terminology in an unconventional manner and shouldn't get your panties in a bunch when people misinterpret what you say.
    2. What you said is flat out wrong.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Jerusalem
    Posts
    3
    MAX_CELLS is 2574.
    You are right guys, the reason is due to too much memory allocation on the stack.
    I need to go over the file, which is something like that:
    (for example)
    A1: 5+4
    B2: A1+5
    C3: B2+5

    and fill the _name field in the array of structs with (A1, B2, C3 and so on..)
    how should I do it in order to overcome this problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Structs Question
    By djg1991 in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2010, 02:15 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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