I have been trying to get a variable from an input. So I get each word, and check it if it is part of a reserved array. If it is part of the reserved array, I get the next word from input, which is a variable.
Below is the reserved array:
char *reserved[] = {
"char", NULL
};
The program is presented with two inputs. One with type.h, and another with ctype.h.The first input works, but the second doesn't:
#include <stdio.h> /*works*/
#include <type.h>
char *reserved[] = {
#include <stdio.h> /* doesn't work */
#include <ctype.h>
char *reserved[] = {
So a function gets a word from the input, and checks to see if it is a reserved word. If it is, it is added to a variable array, but if it isn't a reserved word, the function is run again:
for(w = word, r = reserved; *r!= NULL; w=word, r++) /* Go to the next reserved word*/
for (; *w == **r; (*r)++, w++) /* check if the reserved word matches the word */
if (*w == '\0') { /* if it does, what follows is a variable */
getchar(); /* get the space */
for (v = var, (c=getchar()) =='*'? c = getchar(): c; c != ';' && c != '['; *v = '\0', c = getchar())
*v++ = c; /* add the variable to the variable array */
return var; /* return start of variable array */
}
if ((isalpha(*var)) == 0) { /* if variable array is empty, recursively run the function */
var = getvar(var, reserved);
}
*r gets incremented when the program checks the input ctype, since ctype's first character and the first character of char reserved word match. But the words don't match and the function runs again this time checking the input char. The pointer r gets set to the start of the reserved function when the function gets called again:
for(w = word, r = reserved; *r!= NULL; w=word, r++)
So **r should point to the character c of the char reserved word, but it points to h of char.
Could you please explain, why the pointer doesn't get pointed to the start, even though I just set it to point to the start of the array, when I said r = reserved.
I have attached the code below, if you would like to run it through a debugger.
#include <stdio.h>
#include <ctype.h>
char *getvar(char *var, char *reserved[]);
int main() {
char var[100]; /* stored a variable */
char *v; /* pointer to the start of variable array variable */
char *reserved[] = {
"char", NULL
};
v = getvar(var, reserved); /* returns the start of a variable array */
printf("%s", v); /* prints the variable*/
return 0;
}
char *getvar(char *var, char *reserved[])
{
int c; /* a character*/
char *v, *w, **r; /* a variable, word, and a reserved word character*/
char word[100]; /* an array with a word */
while(((isalpha(c = getchar())) == 0) && c != EOF ); /* skip till you get to a name */
for(w = word; c != ' ' && c!='\n' && c!='\t' && c != EOF; c=getchar(), *w = '\0')
*w++ = c; /* add name to a word */
for(w = word, r = reserved; *r!= NULL; w=word, r++) /* Go to the next reserved word*/
for (; *w == **r; (*r)++, w++) /* check if the reserved word matches the word */
if (*w == '\0') { /* if it does, what follows is a variable */
getchar(); /* get the space */
for (v = var, (c=getchar()) =='*'? c = getchar(): c; c != ';' && c != '['; *v = '\0', c = getchar())
*v++ = c; /* add the variable to the variable array */
return var; /* return start of variable array */
}
if ((isalpha(*var)) == 0) { /* if variable array is empty, recursively run the function */
var = getvar(var, reserved);
}
return var; /* return start of variable array */
}
type.hhas changed toctype.h. Is that really what you are trying to highlight?for (v = var, (c=getchar()) =='*'? c = getchar(): c; c != ';' && c != '['; *v = '\0', c = getchar())this line is not "hacky" - it is error prone, horrible and after one day you will forget what it is supposed to do. Do not save keyboard. Split it into multiline logical block.(*r)++modifies the contents ofreservedwhich means that the recursive call receives a corruptedreservedarray.