3

Need help with the error: expected identifier or '(' before '{' token. I have a main file and a sort heap file. This three files are all separate and I am trying to run this files on a terminal. The command line I use to run this files on terminal is:

gcc -std=c99 -DRAND -DHEAP -DTYPE=double -DPRINT main.c srtheap.c

This is my code:

/*
 *
 *  srt.h file
 *
 */

#ifndef SRT_H  
#define SRT_H  
#include < string.h >  
#define MAX_BUF 256

#define swap(qx,qy,sz) 

{                                             
  do                                                                 
   char buf[MAX_BUF];                                              
   char *q1 = qx;                                                  
   char *q2 = qy;                                                  
   for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {    
       m = ms < sizeof(buf) ? ms : sizeof(buf);                    
       memcpy(buf, q1, m);                                         
       memcpy(q1, q2, m);                                          
       memcpy(q2, buf, m);                                         

  while (0)
}

void srtbubb(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtheap(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtinsr(void *, size_t, size_t, int (*)(const void *, const void *));  
void srtmerg(void *, size_t, size_t, int (*)(const void *, const void *));  

#endif /* SRT_H */
jh314
  • 27,144
  • 16
  • 62
  • 82
user2479019
  • 31
  • 1
  • 5

4 Answers4

6

Your do-while loop has bad syntax regarding the braces (need a pair for the do-while, and a pair for the for-loop. Also, you are missing semicolon after the while. And you need the \ for multiline macros. Try this:

#define swap(qx,qy,sz)                                              \
do                                                                  \
{                                                                   \ 
   char buf[MAX_BUF];                                               \
   char *q1 = qx;                                                   \ 
   char *q2 = qy;                                                   \ 
   for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {     \
       m = ms < sizeof(buf) ? ms : sizeof(buf);                     \
       memcpy(buf, q1, m);                                          \
       memcpy(q1, q2, m);                                           \
       memcpy(q2, buf, m);                                          \
   }                                                                \
} while (0)
jh314
  • 27,144
  • 16
  • 62
  • 82
  • srt.h:17: error: expected identifier or '(' before 'do' srt.h:28: error: expected identifier or '(' before 'while' In file included from srtheap.c:6: srt.h:17: error: expected identifier or '(' before 'do' srt.h:28: error: expected identifier or '(' before 'while' afsconnect1-27 ma524>: – user2479019 Jun 27 '13 at 14:48
  • In a macro definition like that, you *don't* want a semicolon after the `while (0)`. The semicolon will be added when the macro is invoked: `swap(x, y, z);` – Keith Thompson Jun 27 '13 at 15:03
6

A #define of a multi-line macro requires that you end each (except the last) line with \. So you'd get:

#define swap(qx,qy,sz)                                              \
do {                                                                \
    char buf[MAX_BUF];                                              \
    char *q1 = qx;                                                  \
    char *q2 = qy;                                                  \
    for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {    \
        m = ms < sizeof(buf) ? ms : sizeof(buf);                    \
        memcpy(buf, q1, m);                                         \
        memcpy(q1, q2, m);                                          \
        memcpy(q2, buf, m);                                         \
    }                                                               \
} while (0)

Be very careful to not have any white-space after the \ because this will cancel the \ and break the multi-line #define, which will result in hard to find compiler errors.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
1

You are trying to #define a multi-line macro, which may or may not be what you intended. In any case, you need to add the continuation character \ to the end of each line (and fix your brackets):

#define swap(qx,qy,sz)   \
  do {                   \
   char buf[MAX_BUF];    \
   char *q1 = qx;        \
   char *q2 = qy;        \
   for (size_t m, ms = sz; ms > 0; ms -= m, q1 += m, q2 += m) {    \
       m = ms < sizeof(buf) ? ms : sizeof(buf);                    \
       memcpy(buf, q1, m);                                         \
       memcpy(q1, q2, m);                                          \
       memcpy(q2, buf, m);                                         \
       }                                                           \
   }                     \
while (0)
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1

There are several mistakes/typos in #defineing swap():

This should do it (at least compile):

#define swap(qx,qy,sz) \
  do { \ 
    char buf[MAX_BUF]; \
    char *q1 = (qx); \
    char *q2 = (qy); \
    for (size_t m, ms = (sz); ms > 0; ms -= m, q1 += m, q2 += m) \
    { \
      m = ms < sizeof(buf) ? ms : sizeof(buf); \
      memcpy(buf, q1, m); \
      memcpy(q1, q2, m); \
      memcpy(q2, buf, m); \
    } \
  } while (0) 

Note that defines may not have line break, so \is used to tell the preprocessor to joinf the lines.

Also it is advisable to put macro' s arguments in () when it is used, to avoid unwanted side effect after macro expansion.

alk
  • 69,737
  • 10
  • 105
  • 255