Syntax Error??

This is a discussion on Syntax Error?? within the C Programming forums, part of the General Programming Boards category; The setup: Code: struct s0 { SOMETYPE0 *p0, *p1; SOMETYPE1 *e; SOMETYPE2 *c; }; char *importfunction(char **listoffilenames, char *destinationpath) { ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Syntax Error??

    The setup:
    Code:
    struct s0 {
        SOMETYPE0 *p0, *p1;
        SOMETYPE1 *e; 
        SOMETYPE2 *c;
    };
    
    char *importfunction(char **listoffilenames, char *destinationpath)
    {
        struct s0 **currentfile, **newfile(s)info;
    
    // ... READS IN NEW DATA.
    // ... READS IN CURRENT FILE IF IT EXISTS.
    currentfile now is an array of pointers to struct s0 with the last of the array being NULL.
    // ... COMBINES THE TWO LISTS TOGETHER
    // ... CHECKS TO SEE IF ANY CHANGES WERE MADE TO currentfile
    // Okay, we made a change so we'll loop through and write out the data to the outfile (which
    //    was created from the data in SOMETYPE0).
    for (i = 0; currentfile[i] != NULL; i++){
    // writetostore works.  It brings in a fully qualified filename, a void and the enumerated type
    //    of the second parameter.
        writetostore(outfile, currentfile[i]->p0, enum_SOMETYPE0);
        // ... Rest of loop has if (currentfile[i]->WHATEVER != NULL) writetostore. . . 
    }
    Using calloc/realloc for creating the currentfile array. Wanted this in place of linked list so that I could use qsort() to sort the data.

    The error I'm getting in Code::Blocks is "error: syntax error before enum_SOMETYPE0".

    I've been staring at it for about 3 hours and cannot figure out what is wrong.

    Need more info?? Any ideas??
    Last edited by Kennedy; 09-05-2006 at 02:12 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    struct s0 **currentfile, **newfile(s)info;
    Why are those pointers to pointers? With the way this is, the second param to writetostore() is incorrect.

    And () aren't allowed in variable names.

    Also, you have a lot of comments that don't begin with a comment marker. e.g.:
    Code:
    // ... READS IN NEW DATA.
    // ... READS IN CURRENT FILE IF IT EXISTS.
    currentfile now is an array of pointers to struct s0 with the last of the array being NULL.
    // ... COMBINES THE TWO LISTS TOGETHER
    Unless that was just a copy & paste error, in which case you should figure out how to copy & paste correctly.
    If you understand what you're doing, you're not learning anything.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by itsme86
    Code:
    struct s0 **currentfile, **newfile(s)info;
    Why are those pointers to pointers? With the way this is, the second param to writetostore() is incorrect.
    Pointers to pointers so that I can have essentially a linked list, however, with the ease of use of an array.

    How is this not correct?
    Quote Originally Posted by itsme86
    And () aren't allowed in variable names.

    Also, you have a lot of comments that don't begin with a comment marker. e.g.:
    Code:
    // ... READS IN NEW DATA.
    // ... READS IN CURRENT FILE IF IT EXISTS.
    currentfile now is an array of pointers to struct s0 with the last of the array being NULL.
    // ... COMBINES THE TWO LISTS TOGETHER
    Unless that was just a copy & paste error, in which case you should figure out how to copy & paste correctly.
    Not in C, however, in psuedo-C it is prefectly readable and correct .

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Not in C, however, in psuedo-C it is prefectly readable and correct
    And absolutely bloody useless if you want people to tell you where a syntax error is

    Post actual code, not some paraphrased jibberish you though up and snipped to hell code.

    Copy/paste relevant lines to a temporary source file and keep compiling it until you either figure out your own mistake, or you have a nice handy file which you can post to the board which demonstrates the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    typedef struct _fullrec {
            TMRI_PARAMREC *p0, *p1;
            TMRI_CHROMREC *c;
            TMRI_ERRORREC *e;
    } FULLREC;
    
    void initfullrec(FULLREC *fr)
    /*
            Given:        A pointer to a FULLREC struct.
            Return:        Nothing.
            Task:        Set everything to NULL.
    */
    {
            fr->p0 = NULL;
            fr->p1 = NULL;
            fr->c = NULL;
            fr->e = NULL;
            return;
    }
    
    FULLREC **loadfiles(char **filenames)
    /*
            Given:        A list of strings.
            Return:        A list of FULLRECs.
            Task:        The given list of strings has a trailing NULL.  Step through the list of filenames and load all the data
                    from the filenames into a buffer.  Return that buffer.
    */
    {
            FULLREC **list = NULL;
            int listlen = 0;
            int i, next;
            void *p, *e, *c;
            FILE *file;
    
            if (filenames == NULL){
                    return NULL;
            }
            for (i = 0; filenames[i] != NULL; i++){
                    if ((file = fopen(filenames[i], "rb")) == NULL){
                            continue;
                    }
                    next = 0;
                    while (!feof(file) && !next){
                            if (getnexttype(file) != TYPE_PARAMREC){
                                    next++;
                                    continue;
                            }
                            p = NULL; e = NULL; c = NULL;
                            switch(getnextversion(file)){
                                    case 0:
                                            p = malloc(sizeof(TMRI_PARAMREC_R0));
                                            getnextchunk(p, file, TYPE_PARAMREC);
                                            break;
                                    default:
                                            p = NULL;
                                            break;
                            }
                            if (p == NULL){
                                    next++;
                                    break;
                            }
                            listlen++;
                            list = (FULLREC **) realloc(list, sizeof(FULLREC *) * (listlen + 1));
                            list[listlen - 1] = (FULLREC *) malloc(sizeof(FULLREC));
                            initfullrec(list[listlen - 1]);
                            list[listlen - 1]->p0 = (TMRI_PARAMREC *) p;
                            if ((getnexttype(file) == TYPE_PARAMREC) && (list[listlen - 1]->p0->bytes[BYTE_NUMAGENTS] == 1)){
                                    continue;
                            }
                            p = NULL;
                            if (getnexttype(file) == TYPE_PARAMREC){
                                    switch(getnextversion(file)){
                                            case 0:
                                                    p = malloc(sizeof(TMRI_PARAMREC_R0));
                                                    getnextchunk(p, file, TYPE_PARAMREC);
                                                    break;
                                            default:
                                                    p = NULL;
                                                    break;
                                    }
                                    if (p == NULL){
                                            next++;
                                            break;
                                    }
                            }
                            list[listlen - 1]->p1 = (TMRI_PARAMREC *) p;
                            if (getnexttype(file) == TYPE_ERRORREC){
                                    switch(getnextversion(file)){
                                            case 0:
                                                    e = malloc(sizeof(TMRI_ERRORREC_R0));
                                                    getnextchunk(e, file, TYPE_ERRORREC);
                                                    break;
                                            default:
                                                    e = NULL;
                                    }
                                    if (e == NULL){
                                            next++;
                                            break;
                                    }
                            }
                            list[listlen - 1]->e = (TMRI_ERRORREC *) e;
                            if (getnexttype(file) == TYPE_CHROMREC){
                                    switch(getnextversion(file)){
                                            case 0:
                                                    c = malloc(sizeof(TMRI_CHROMREC_R0));
                                                    getnextchunk(c, file, TYPE_CHROMREC);
                                                    break;
                                            default:
                                                    c = NULL;
                                    }
                                    if (c == NULL){
                                            next++;
                                            break;
                                    }
                            }
                            list[listlen - 1]->c = (TMRI_CHROMREC *) c;
                    }
            }
            if (listlen){
                    list = (FULLREC **) realloc(list, sizeof(FULLREC *) * (listlen + 1));
                    list[listlen] = NULL;
            }
            return list;
    }
    
    void freefullrec(FULLREC *fr)
    /*
            Given:        A pointer to a FULLREC struct.
            Return:        Nothing.
            Task:        fr may have nothing in it to free.  If there is something, however, free the parts correctly.
            Note:        FULLREC.e and FULLREC.c may have malloced memory which will also need to be free'ed.
    */
    {
            if (fr->p0 != NULL){
                    free(fr->p0);
            }
            if (fr->p1 != NULL){
                    free(fr->p1);
            }
            if (fr->e != NULL){
                    free(fr->e->list);
                    free(fr->e);
            }
            if (fr->c != NULL){
                    free(fr->c->chrom);
                    free(fr->c->extrabits);
                    free(fr->c);
            }
            return;
    }
    
    int inreclist(FULLREC **list, FULLREC *node)
    /*
            Given:        A FULLREC array and a FULLREC.
            Return:        0 for not found, 1+offset for found and complete, -1-offset for found and not complete.
            Task:        Locate node in list and determine if it is more complete than what is in the list.
    */
    {
            int i;
            if (list == NULL)
                    return 0;
            for (i = 0; list[i] != NULL; i++){
                    if (cmp_PARAMREC(node->p0, list[i]->p0))
                            break;
            }
            if (list[i] != NULL){
                    if (((list[i]->p1 == NULL) && (node->p1 != NULL)) ||
                        ((list[i]->e == NULL) && (node->e != NULL)) ||
                        ((list[i]->c == NULL) && (node->c != NULL))){
                                i++;
                            return i;
                    }
                    if (((list[i]->p1 != NULL) && (node->p1 == NULL)) ||
                        ((list[i]->e != NULL) && (node->e == NULL)) ||
                        ((list[i]->c != NULL) && (node->c == NULL))){
                                i++;
                            i *= -1;
                            return i;
                    }
                    if (list[i]->p0->shorts[SHORT_POINTSLEN] > node->p0->shorts[SHORT_POINTSLEN]){
                            i++;
                            i *= -1;
                            return i;
                    }
                    if (list[i]->p1 != NULL){
                            if (list[i]->p1->shorts[SHORT_POINTSLEN] > node->p1->shorts[SHORT_POINTSLEN]){
                                    i++;
                                    i *= -1;
                                    return i;
                            }
                    }
                    if (list[i]->c != NULL){
                            if (list[i]->c->shorts[CHROM_SHORTS_CHROMLEN] > node->c->shorts[CHROM_SHORTS_CHROMLEN]){
                                    i++;
                                    i *= -1;
                                    return i;
                            }
                    }
                    if (list[i]->e != NULL){
                            if (list[i]->e->len > node->e->len){
                                    i++;
                                    i *= -1;
                                    return i;
                            }
                    }
            }
            return 0;
    }
    
    char *importadf(char **infile, char *outfilepath)
    /*
            Given:        Two strings.
            Return:        A filename that is the new file that should be the currentfile (this will be the last filename to which we output).
            Task:        The given infile is a fully qualified filename to an existing ADF outfilepath is the path to the current
                    store directory.  The new file(s) will be located into the outfilepath directory.  The import will be preformed
                    by using the outfilepath, unit ID, the year, the month, the day, and ".adf" as the new filename.  It is possible, therefore,
                    that the file contain multiple dates within one file.
            NOTE:        No error checking is done on loading the files.  If a file/filename fails, we ditch out and continue.
    */
    {
            char *outfile, *retval;
            int infilelen, i, j, changed, curfilelen;
            int start, end, year, month, day, unit, preunit, preyear, premonth, preday;
            char **subfilelist;
            FULLREC **list, **curfile;
            FULLREC *fr;
            FILE *file;
            TMRI_PARAMREC *p;
    
            if (infile == NULL){
                    return NULL;
            }
            infilelen = 0;
            while (infile[infilelen] != NULL) infilelen++;
            infilelen++;
            qsort(infile, infilelen, sizeof(char *), strcmp);
            start = 0;
            outfile = (char *) malloc(sizeof(char) * 2048);
            while (start < infilelen){
                    sscanf(infile[start], "%5u%4i%2u%2u", &preunit, &preyear, &premonth, &preday);
                    // Set the number files to import in this batch.  We are only getting a single day's worth of data.
                    // e.g.  123452006010100.adf - 123452006010123.adf
                    for (i = start + 1; i < infilelen; i++){
                            sscanf(infile[i], "%5u%4i%2u%2u", &unit, &year, &premonth, &preday);
                            if (unit == preunit){
                                    if (year == preyear){
                                            if (month == premonth){
                                                    if (day == preday){
                                                            continue;
                                                    }
                                            }
                                    }
                            }
                            break;
                    }
                    if (i == infilelen)
                            i--;
                    end = i;
                    end++; // make the compare only <
                    subfilelist = (char **) malloc(sizeof(char *) * (end - start + 1));
                    subfilelist[end - start] = NULL;
                    for (i = start; i < end; i++){
                            subfilelist[i - start] = strdup(infile[i]);
                    }
                    list = loadfiles(subfilelist);
                    for (i = start; i < end; i++){
                            free(subfilelist[i - start]);
                    }
                    p = list[0]->p0;
                    sprintf(outfile, "%s\\%05i\\%i\\%02i\\%05i%i%02i%02i.adf", outfilepath, p->shorts[SHORT_UNITID],
                                                                               p->dates[DATE_DATE].year, p->dates[DATE_DATE].month,
                                                                               p->shorts[SHORT_UNITID], p->dates[DATE_DATE].year,
                                                                               p->dates[DATE_DATE].month, p->dates[DATE_DATE].day);
                    createdirs(outfile);
                    subfilelist[1] = NULL;
                    subfilelist[0] = outfile;
                    curfile = loadfiles(subfilelist);
                    free(subfilelist);
                    // Now we have two lists (maybe) and need to move items from the list -> curfile.  If we move ANYTHING, then
                    // we need to sort and remake this file.
                    changed = 0;
                    if (curfile == NULL){
                            curfile = list;
                            list = NULL;
                            changed = 1;
                    } else {
                            for (i = 0; curfile[i] != NULL; i++);
                            curfilelen = i;
                            for (i = 0; list[i] != NULL; i++){
                                    j = inreclist(curfile, list[i]);
                                    if (!j){
                                            changed = 1;
                                            curfilelen++;
                                            curfile = (FULLREC **) realloc(curfile, sizeof(FULLREC *) * (curfilelen + 1));
                                            curfile[curfilelen] = NULL;
                                            curfile[curfilelen - 1] = list[i];
                                            list[i] = NULL;
                                    } else if (j < 0){
                                            changed = 1;
                                            j *= -1;
                                            j--;
                                            freefullrec(curfile[j]);
                                            free(curfile[j]);
                                            curfile[j] = list[i];
                                            list[i] = NULL;
                                    }
                                    if (list[i] != NULL){
                                            freefullrec(list[i]);
                                            free(list[i]);
                                    }
                            }
                            free(list);
                    }
                    if (changed){
                            file = fopen(outfile, "w");
                            fclose(file);
                            for (i = 0; curfile[i] != NULL; i++){
                                    fr = curfile[i];
                                    writetostore(outfile, fr->p0, TMRI_PARAMREC);
                                    if (fr->p1 != NULL){
                                            writetostore(outfile, fr->p1, TMRI_PARAMREC);
                                    }
                                    if (curfile[i]->e != NULL){
                                            writetostore(outfile, fr->e, TMRI_ERRORREC);
                                    }
                                    if (curfile[i]->c != NULL){
                                            writetostore(outfile, fr->c, TMRI_CHROMREC);
                                    }
                                    freefullrec(curfile[i]);
                                    free(curfile[i]);
                            }
                            free(curfile);
                    }
                    start = end;
            }
            if (outfile[0] != '\0'){
                    retval = strdup(outfile);
            } else {
                    retval = NULL;
            }
            free(outfile);
            return retval;

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Where exactly is the error? I searched your code for "enum" and didn't turn up anything.

    Code:
    while (!feof(file) && !next){
    Read the FAQ to see why this is wrong: FAQ > Explanations of... > Why it's bad to use feof() to control a loop

    Code:
    subfilelist = (char **) malloc(sizeof(char *) * (end - start + 1));
    FAQ > Explanations of... > Casting malloc

    You have a lot of switch statements that have only case 0 and default; these might be more readable as if/else statements.

    Code:
    void func(void) {
        /* ... */
        return;
    }
    void functions automatically return when the end of the function is reached. You don't really need a return statement.

    Code:
                            if (unit == preunit){
                                    if (year == preyear){
                                            if (month == premonth){
                                                    if (day == preday){
                                                            continue;
                                                    }
                                            }
                                    }
                            }
    You might want to consider using &&.

    Again, post the exact line that has the syntax error or highlight the line so we can tell where the problem is.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > writetostore(outfile, fr->p1, TMRI_PARAMREC);
    Probably because TMRI_PARAMREC is your typedef struct, not an enum

    > getnextchunk(p, file, TYPE_PARAMREC);
    This appears to be correct use of the enum.

    Oh and all your realloc calls are poor.
    Never do p = realloc( p, size );
    Always assign to a temp pointer first.

    That's some f-ugly code there - those functions are way too long.

    Next time, add a comment to the code along the lines of
    /* This line generates the foo error message */

    Also, try compiling more often than once a day, or however long it took you to type in that code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    > writetostore(outfile, fr->p1, TMRI_PARAMREC);
    Probably because TMRI_PARAMREC is your typedef struct, not an enum

    > getnextchunk(p, file, TYPE_PARAMREC);
    This appears to be correct use of the enum.
    8 hours of looking at it didn't reveal that to me. I hate it when that happens. I, of course, kept reading that TYPE_PARAMREC not TMRI_PARAMREC -- this is why I'll be hiring someone to work with me next year. . .
    Quote Originally Posted by Salem
    Also, try compiling more often than once a day, or however long it took you to type in that code.
    Unfortunately, I couldn't. This was a re-write of the original code from six weeks ago. I couldn't compile it until I had all the functions complete. That is also the reason for the length of the importadf function. I had to scrap the old one and put in this new one.

    Thanks for spotting that for me. If you notice in the first post I had it right. . . but I was giving you the way it should be instead of the way it was.

    Thanks again!

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > I couldn't compile it until I had all the functions complete.
    All compilers I know have a "compile only" mode, there's no need to have complete functions.

    Code:
    void foo ( int a ) {
    }
    Code:
    void foo ( int a ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
      }
    }
    Code:
    void foo ( int a ) {
      int i;
      for ( i = 0 ; i < 10 ; i++ ) {
        printf( "%d\n", i );
      }
    }
    Each is a syntactically valid code fragment which any compiler would happily validate, say
    gcc -c prog.c

    Never type in more code than you're prepared to deal with should it throw out a bunch of errors. Obviously as your skill improves, you can type in more. Even now, I compile several times per hour just to make sure, and also write code to actually test it as I go as well.

    50% written and 100% tested is a hell of a lot better than 100% written and 0% tested.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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