Thread: QUESTION!! parameter passing

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

    QUESTION!! parameter passing

    hey guys got a question...

    Code:
    function a(parameter){
       char a[20][30];
       other declaration;
    
       //parameter 2 is passing in the array
       function b(parameter1,parameter2);
    }
    
    function storing(parameter 3,parameter 4){
    
      //  storing values into array a without returning
      //  when accessing function a the array is filled with 
      //  values stored using this function
      // i know that pointer muz be used in this case to change the 
      //  actual value in the area in function a but i am not very sure 
      //  how to do it
    
    
    }
    so wat m i suppose to pass in parameter2,parameter3,parameter4

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Congratulations, that's the most incomprehensible and vague question I've seen in a long time, try again.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Code:
    #include <stdio.h>
    
    void getAllToken(char *line,char *tokenList[]){
       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);
          strcpy(token,tokenList[i]);
          i++;
       }
    }
    
    char *getNextToken(char *ptr,char *token){
       /*skipping all the spaces*/
       while((*ptr != '\0') && (*ptr == ' ')){
          ptr++;
       }
       
       while((*ptr != '\0') && (*ptr != ' ')){
          *token++ = *ptr++;
       }
       *token = '\0';
       return ptr;
    }
    
    void interpret(char *line){
       char tokenList[20][30];
          
       getAllToken(line,tokenList);
       printf("%s\n",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;
    }
    this segment of code is suppose to extract all the words in a file and to be stored in an array (without using strtok)
    the problem is i got segmentation fault for this piece of code and i am not too sure how to insert values into the array from another function

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    using feof() to control a loop is a bad idea.

    Do you know where the seg fault is occurring (what function)?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    use fscanf to read the file one word at a time -- you can then delete all those functions that attempt to parse the line.
    Code:
    char word[255];
    
    while( fscanf(fp,"%s",word) > 0)
    {
       // add the word to a linked list or array here
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well fixing some of the errors would help
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘getAllToken’:
    foo.c:12: warning: implicit declaration of function ‘getNextToken’
    foo.c:12: warning: assignment makes pointer from integer without a cast
    foo.c:13: warning: implicit declaration of function ‘strcpy’
    foo.c:13: warning: incompatible implicit declaration of built-in function ‘strcpy’
    foo.c: At top level:
    foo.c:18: error: conflicting types for ‘getNextToken’
    foo.c:12: error: previous implicit declaration of ‘getNextToken’ was here
    foo.c: In function ‘interpret’:
    foo.c:34: warning: passing argument 2 of ‘getAllToken’ from incompatible pointer type
    foo.c:35: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[29u]’
    foo.c: In function ‘main’:
    foo.c:46: warning: implicit declaration of function ‘exit’
    foo.c:46: warning: incompatible implicit declaration of built-in function ‘exit’
    The biggie problem is how you call this
    void getAllToken(char *line,char *tokenList[])
    With this
    getAllToken(line,tokenList);

    A 2D array of char is not an array of pointers (repeat after me, arrays and pointers are NOT the same thing).

    This would be better
    void getAllToken(char *line,char tokenList[20][30])
    Then you can pass that 2D array as you describe.

    > strcpy(token,tokenList[i]);
    I'd say you're copying the wrong way, it's strcpy(destinatio,source)
    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.

  7. #7
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    void getAllToken(char *line,char *tokenList[]){
       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);
          strcpy(token,tokenList[i]);
          i++;
       }
    }
    one more thing.
    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"

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    thankz so much!!
    after doing some altering from the advice u all gave i finally manage to get my code to work!!
    thanks again!!
    so much appreciated

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One last thing . . .
    Code:
    char *getNextToken(char *ptr,char *token){
       /*skipping all the spaces*/
       while((*ptr != '\0') && (*ptr == ' ')){
          ptr++;
       }
       
       while((*ptr != '\0') && (*ptr != ' ')){
          *token++ = *ptr++;
       }
       *token = '\0';
       return ptr;
    }
    You don't need that check . . . since if *ptr is NULL, it wouldn't match the other test anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2007, 01:01 AM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM
  5. Question about Templates and passing arguments
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:32 AM