Thread: storing into an ARRAY OF STRUCT

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    storing into an ARRAY OF STRUCT

    Code:
    typedef enum tType
       {VAR_TYPE,VAR_NAME,INT_VALUE,TERMINATOR}
    TokenType;
    
    typedef struct{
       TokenType type;
       char *value;
    }TokenEntry;
    
    /*copying from source to destination*/
    void iStrCpy(char *src,char *dest){
       while(*src != '\0'){
          *dest++ = *src++;
       }
       *dest = *src;
    }
    
    /*check whether the input given ranfges from small
    a to small z and capital A to capital Z
    0 is return if it is true and 1 is return if it is false*/
    int isVar(char *strVar){
       while (*strVar != '\0'){
          if((*strVar >= 'a' && *strVar <= 'z') ||
             (*strVar >= 'A' && *strVar <= 'Z')){
             strVar++;
          }
          else{
             return 1;
          }
       }
       return 0;
    } 
    
    
    int isNum(char *intVar){
       while (*intVar != '\0'){
          if(*intVar >= 0 && *intVar <= 9){
             intVar++;
          }
          else{
             return 1;
          }
       }
       return 0;
    } 
    
    int getAllToken(char *line,char tokenList[20][30]){
       char token[50];
       char *ptr = line;
       int i = 0;
      
       
       /*while is not the end of line get the
       token and store it into tokenList*/
       while(*ptr != '\0'){
          ptr = getNextToken(ptr,token);
          iStrCpy(token,tokenList[i]);
          i++;
       }
       return i;
    
    }
    
    char *getNextToken(char *ptr,char *token){
       while((*ptr != '\0') && (*ptr == ' ')){
          ptr++;
       }
       
       while((*ptr != '\0') && (*ptr != ' ')){
          *token++ = *ptr++;
       }
       *token = '\0';
       return ptr;
    }
    
    
    /*passing in token by token*/
    void checkReserveWord(char tokenListSize,char tokenList[20][30]){
       int i;
       int j = 0;
       int k;
       int l = 0;
       TokenEntry eType[20];
       char *reserveWord[] = {"int","isVar","isNum",";"};
       TokenType tType[20] = {VAR_TYPE,VAR_NAME,INT_VALUE,TERMINATOR};
       
       
       for(i = 0; i <= tokenListSize; i++){
          j = 0;
          while(j != 4){
             k = iStrCmp(tokenList[i],reserveWord[j]);
             if(k == 0){
                /*storing into an array of struct*/
                eType[l].type = tType[j];
                eType[l].value = tokenList[i];
                l++;
                j = 4;
             }
    
             /*return 0 is true*/
             else if(!(isVar(tokenList[i]))){
                eType[l].type = tType[j];
                eType[l].value = tokenList[i];
                l++;
                j = 4;
             }
             else if(!(isNum(tokenList[i]))){
                eType[l].type = tType[j];
                eType[l].value = tokenList[i];
                l++;
                j = 4;
             }
             else{
                j++;
             }
          }
       }
    }
    
    void interpret(char *line){
       char tokenList[20][30];
       int count;
       
       count = getAllToken(line,tokenList);
       checkReserveWord(count,tokenList);
    
    }
    
    
    int main(int argc,char *argv[]){
       FILE *fp;
       char line[1000];
             
       if(argc != 2){
          printf("Invalid number of files\n");
          exit(1);
       }
    
       fp = fopen(argv[1],"r");
       if(fp == NULL){
          printf("Invalid file");
       }
    
       /*get line by line from the file*/
       while(!feof(fp)){
          fgets(line,1000,fp);
          interpret(line);
     
       }
       
       return 0;
    } 
    
    sample FILE
    
    int x ;

    This program i wrote is suppose to store the the words tokenize from the file into an array of struct.I am also trying my best to write my own library functions as u can see which is the first few functions.I am also not using string tokenizer to extract the wrods but doin it in my own function.As much as it can be, functions in string.h library is avoided from being used.
    To my knowledge everything works well untill tokenizing.However there is some problem when i m trying to retrieve the information in the array of struct.Can someone please advice me on this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Several problems

    1. while(!feof(fp))
    Read the FAQ - feof() in control loops is not a good idea
    Use
    while ( fgets(line,sizeof line,fp) != NULL )

    2. for(i = 0; i <= tokenListSize; i++)
    Almost always, you want < size when accessing some kind of array. I see nothing else in the code which would indicate otherwise.

    3. eType[l].value = tokenList[i];
    eType is a local variable in this function, and it contains a pointer to another local variable in another function. All sorts of 'fun' ensue when you say return from this function.

    > As much as it can be, functions in string.h library is avoided from being used.
    Is there a particular reason for doing this?
    I mean, compared to strcpy(), your iStrCpy() has the parameters exactly the wrong way round. It sure makes it hard for people to read the code when you replace standard functions in non-standard ways.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    int isNum(char *intVar){
       while (*intVar != '\0'){
          if(*intVar >= 0 && *intVar <= 9)
    I think you are comparing ascii values.

    ptr = getNextToken(ptr,token);
    You should declare the function prototype before using.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Code:
    2. for(i = 0; i <= tokenListSize; i++)
    Almost always, you want < size when accessing some kind of array. I see nothing else in the code which would indicate otherwise.
    this is done so that checking wont be done more times than the size of array

    Code:
    3. eType[l].value = tokenList[i];
    eType is a local variable in this function, and it contains a pointer to another local variable in another function. All sorts of 'fun' ensue when you say return from this function.
    I dont really get wat u mean here...cant i do like this to store the value i want to into the array of struct?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > this is done so that checking wont be done more times than the size of array
    My point being you fail, and go round the loop once too often.
    Step through your code (in your head) with only 1 token resulting from
    count = getAllToken(line,tokenList);

    > cant i do like this to store the value i want to into the array of struct?
    You're not storing values, you're storing pointers.
    A value would be the result of say
    Code:
    typedef struct{
       TokenType type;
       char value[20];
    }TokenEntry;
    And eType[l].value = tokenList[i] would be iStrCpy( tokenList[i], eType[l].value )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    >My point being you fail, and go round the loop once too often.
    Step through your code (in your head) with only 1 token resulting from
    count = getAllToken(line,tokenList);

    i got this part n i have change it.

    >You're not storing values, you're storing pointers.
    A value would be the result of say
    And eType[l].value = tokenList[i] would be iStrCpy( tokenList[i], eType[l].value

    i understand that value is an array of char therefore i need to iStrCpy
    how about for type?type is an enum so it will return int how am i suppose to
    copy it into the array of struct.cant use iStrCpy(tried n return lots of error)

    besides wats
    Code:
    warning: ISO C90 forbids mixed declarations and code
    i got this warning during compilation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  4. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM