Thread: Segmentation fault, how come?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course there is. Say something like
    facit[arrayIndex + sizeof(double) * matchIndex] = strtod(token, NULL);
    I take no validity for the code, however. But that's how you write to an array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Quote Originally Posted by Elysia View Post
    Of course there is. Say something like
    facit[arrayIndex + sizeof(double) * matchIndex] = strtod(token, NULL);
    I take no validity for the code, however. But that's how you write to an array.
    Thanks, but I don't think that's it. I would like to replace...
    Code:
    if (!matchIndex) ptr = &matchArray[arrayIndex].home;
    else if (matchIndex == 1) ptr = &matchArray[arrayIndex].draw;
    else ptr = &matchArray[arrayIndex].away;
    
    *ptr = strtod(token, NULL);
    ...with a one liner, if it's possible.

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
          
     /*.... */
     double *a[] = { &matchArray[arrayIndex].home , &matchArray[arrayIndex].draw, &matchArray[arrayIndex].away };           // grrrr wtf is wrong with i ,j , k????
    
     for (matchIndex = 0; matchIndex < 3; matchIndex ++) {
                if (!strIsFloat(token)) {
                    error(1, 0, "datafile parsing error -- expected float");
                }
                *a[matchIndex] = strtod(token, NULL);
                token = strtok(NULL, " ");
            }
       /* .... */
    Or
    Code:
     double a[3];
     for (matchIndex = 0; matchIndex < 3; matchIndex ++) {
                if (!strIsFloat(token)) {
                    error(1, 0, "datafile parsing error -- expected float");
                }
                a[matchIndex] = strtod(token, NULL);
                token = strtok(NULL, " ");
       }
       matchArray[arrayIndex] =  (match_t) { a[0] , a[1], a[2] };      // C99 feature. or assign to each member....
    Last edited by Bayint Naung; 08-31-2010 at 01:29 AM.

  4. #19
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    That's one nice solution. Thanks...

    Quote Originally Posted by Bayint Naung View Post
    Code:
    // grrrr wtf is wrong with i ,j , k????
    He, he, he... Nothing's wrong with it. My lousy brain just have a hard time cope with it. I leave the code for six moths, and when I come back, I'm totally lost. Sure having trouble to keep i, j and k apart, not knowing which one of the totally meaningless variable names holding what value. I've done it too many times. This way, I'm back in a couple of seconds.

    What I ain't got in my brains, I've got to put in my fingers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2010, 10:55 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM