|
-
Oct 23rd, 2001, 12:27 PM
#1
Thread Starter
Frenzied Member
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."
-
Oct 23rd, 2001, 12:31 PM
#2
Member
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.
-
Oct 23rd, 2001, 05:53 PM
#3
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.
-
Oct 23rd, 2001, 06:04 PM
#4
Thread Starter
Frenzied Member
-
Oct 24th, 2001, 08:12 AM
#5
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.
-
Oct 24th, 2001, 08:45 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|