Thread: Passing an array to a function

  1. #1
    Registered User
    Join Date
    Sep 2009

    Passing an array to a function

    I am having trouble passing an array to a function, here is my code.

    void get_fields(char *fld1);
    int main(void)
       char array[100];
    function code

    /* get_fields.c-- read sequential text file            */
    /*             -- file has newline delimited "records" */
    /*             -- records have tab delimited "fields"  */
    /* a C Beginner's program:                             */
    /* This is meant to be a learning/teaching program.    */
    /* This program sacrifices elegance for readability    */
    /* If suitable, this may be added to the C course.     */
    void get_fields(char *fld1)
      char record[100];                 /* array to hold each "record" */ 
           //*fld1, *fld2, *fld3, *fld4;         /* pointers to "fields" */
      FILE *fin;                              /* pointer to input file */
      fin = fopen(FILENAME, "r");             /* open the file to read */
                                                     /* print a header */
      printf("%-25s %-20s %-15s %-15s\n",           
             "Field 1\\t", "Field 2\\t", "Field 3\\t", "Field 4\\n");
      /* the while loop is used to read in the file that fin points to */
      /* use fgets() to read the file. fgets() reads up to a newline   */
      /* fgets() includes the newline in the string it reads and tacks */
      /* a '\0' onto the end of the string. It should be familiar.     */
      while (fgets(record, sizeof(record), fin))
        /* strtok() will be looking for a newline at the end of record */
        if (record[strlen(record) - 1] != '\n')
          /* make sure the newline is there */
          record[strlen(record) - 1] = '\n';
        /* use strtok() to get each "field". strtok()'s usage is:      */
        /* char *ptr = strtok(char *s1, const char *s2);               */
        /* the first call of strtok() searches the string s1 for the   */
        /* `token' s2. If found, it replaces the token with '\0' and   */
        /* sets a pointer to the next character                        */
        fld1 = strtok(record, "\t");
        /* after that each call has a NULL pointer as the 1st argument */
        /* & starts searching for another token from the saved pointer */
        /* if strtok() can't find the token, it returns a null pointer */
        /* strtok() is a tricky function to use. 8^D                   */
        //fld1+20 = strtok(NULL, "\t");
        //fld3 = strtok(NULL, "\t");
        //fld4 = strtok(NULL, "\n");
        /* finally, print each string using printf() for formatting    */
        //printf("%-25s %-20s %-15s %-15s\n", fld1, fld2, fld3, fld4);
      //return 0;

    I get a segmentation fault
    thanks in advance Mick

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You don't check whether the file was successfully opened. You probably want to do so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM