Results 1 to 6 of 6

Thread: Simple C code with an error... uhh

  1. #1

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Post Simple C code with an error... uhh

    Hi all.

    I'm having some trouble with some simple C code. It looks absolutely fine to me and a couple of other people I've asked, but the compiler is insisting there's something wrong with it.

    Here's the code:
    Code:
    /* highlighter.c */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    
    void printtofile(char data[128])
    {
      FILE *f = fopen("high_log.log", "a");
      fprintf(f, data);
      fprintf(f, "\n");
      fclose(f);
    }
    
    
    /* function to highlight certain words in an HTML string */
    char *highlight(char *htmlstring, char *word, char *highlightedstring)
    {
    
      char datastring[128];
      sprintf(datastring, "string to highlight is '%s', word is '%s'", htmlstring, word);
      printtofile(datastring);
      char *currentchar = htmlstring;      //error is on this line
      int wordlen = strlen(word);
      char *htmlstringend = htmlstring + strlen(htmlstring);
    
      char *openbracket = strchr(currentchar, (int)'<');
      char *closebracket = strchr(openbracket, (int)'>');
      char *matchedword = strstr(currentchar, word);
    
      const char *highlighttagstart = "<font color=\"#FF0000\">";
      const char *highlighttagend   = "</font>";
    
      const int highlighttagslength = strlen(highlighttagstart) + strlen(highlighttagend);
    
      highlightedstring[0] = '\0';
    
    
      while( *currentchar && (currentchar < htmlstringend) )
      {
        if ( openbracket < matchedword )
        {
          currentchar = closebracket + 1;
          openbracket = strchr(currentchar, (int)'<');
          closebracket = strchr(openbracket, (int)'>');
    
          if ( closebracket > matchedword )
          {
            matchedword = strstr(currentchar, word);
          }
          continue;
        }
        /* at this point, we should have a match */
    
        strcat(highlightedstring, highlighttagstart);
        strncat(highlightedstring, currentchar, wordlen);
        strcat(highlightedstring, highlighttagend);
    
        currentchar+=4;
    
      };
    
      return( highlightedstring );
    }
    I get this error:

    highlighter.c:24: parse error before `char'

    I'm compiling this on Linux using gcc. I don't think that should be any problem.

    Anybody see any syntax errors? The syntax errors go away if I remove the first 3 lines of the highlight() function (they are just for logging) but the reason I'm trying to use a log is because I'm getting run-time errors.
    Harry.

    "From one thing, know ten thousand things."

  2. #2
    Member
    Join Date
    Jul 2001
    Posts
    38
    i think this the one?

    Code:
    char datastring[128];
    
    =>
    
    char * datastring[128];
    I could not verify this, because i don't have the files that you refer to.
    I think that is the error.

  3. #3
    jim mcnamara
    Guest
    Harry I fed this code to my compiler at work.

    Doesn't like the first three lines, and really hates the same char your compiler finds offensive.

    If I turn off ansi c checks and some other stuff, it does compile with warnings.

    Wha it doesn't like is that you are declaring a char pointer to htmlstring, all in one line. It does like:

    char *currentstring;
    currentstring = htmlstring;

    it also likes
    static char *currentstring = htmlstring;

    FWIW - most compilers get upset with some kinds of initializers, especially after you have referenced a variable and then try to assign it to a variable in the declaration statement.

    I had to separate almost all of your declarations out, move 'em to the top, then assign them later to get rid of warnings. a C++ compiler should like this a lot more.

  4. #4

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827

    Thumbs up Cheers Jim

    Thanks a lot Jim I guess I should have tried that, but I just assumed it would be clever enough to do it. I don't suppose you tested it out? I was writing this for a C Python module, since the native Python code doesn't seem to be doing the job fast enough. Any optimisation tips would be welcome too I know I have some useless variables declared in there; they're remnants from other code I was trying.

    Thanks for the advice anyway, I'll get that fixed up at work tomorrow
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    jsut one question, if this is C, why does it accept variable declaration after real code?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    jim mcnamara
    Guest
    That's what I had to change.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width