views:

54

answers:

3

I'm trying to complete an analexical validation but I'm facing some trouble with pointers, this is my code

 case 6: c = next_carac(file);
   for(handle=0;(words[handle] != "NULL");handle++)
   {
    strcpy(message, words[handle]);
    if(!strcmp(token,message))
     strcpy(message, "words");
   }
   if(isdigit(c) && strcmp(message,"words"))
   {
    step=6;
    token[auxtoken]=c;
    auxtoken++;
   }
   else if(isalpha(c) && strcmp(message,"words"))
    {
     step=6;
     token[auxtoken]=c;
     auxtoken++;
    }
    else
    {
     step=7;
     return_carac(file);
    }
   break;

and these are the variable declared

const char *words[]={
     "program",
     "label",
     "integer",
     "word",
     "char",
     "byte",
     "shortint",
     "logint",
     "real",
     "single",
     "double",
     "string",
     "boolean",
     "var",
     "procedure",
     "function",
     "begin",
     "end",
     "if",
     "then",
     "else",
     "or",
     "and",
     "div",
     "not",
     "do",
     "while",
     "mod",
     "NULL"
};

char token[80],message[30];
    int step=0;
    char c;
auxtoken=0;

but it is generating the following errors which I couldn't find a way to mitigate

Loaded 'ntdll.dll', no matching symbolic information found. Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found. First-chance exception in Main.exe: 0xC0000005: Access Violation.

Would you know how could I fix it?

+3  A: 

It should be NULL, not "NULL" (in 2 places).

Explanation: the condition words[handle] != "NULL" is not doing what you expect. It compares pointers, not strings. So, it's possible that you never go out of that loop and it results in illegal memory access. NULL, on the other hand, is a pointer, not a string, and NULL is always NULL.

Igor Krivokon
The condition words[handle] != "NULL" is not always true. In fact, often string literals are only stored once in an executable, in which case the code behaves the same as when one would replace the occurrences of "NULL" by NULL.
mweerden
Technically, you're right - it's not always. However, for VC++ (this is what I guess OP is using) you have to turn this feature on explicitly, it's not on by default. So, I'm pretty sure this is the reason for the problem. I'm correcting my answer, anyway, thanks!
Igor Krivokon
A: 

To add to what Igor wrote you should also add one more entry in your array of pointers (if "NULL" is part of your grammar):

  ...
  "not",
  "do",
  "while",
  "mod",
  "NULL",
  NULL      // marks end of array
};

that way you can search until words[handle]!=NULL otherwise it may continue searching beyond the array. A still better approach is probably to instead have some form of hash table and do a lookup instead.

Anders K.
+1  A: 

The above answers are great, but as a more general bit of advice, when you hit a runtime error like this, a good first step in working out the cause of the problem is to look at your program in a debugger, and see which line of code is causing the problem (i.e., by looking at the call stack).

jskinner