I am having trouble passing an array to a function, here is my code.
Code:void get_fields(char *fld1);function codeCode:int main(void) char array[100]; get_fields(array);
Code:/*****************************************************************/ /* get_fields.c-- read sequential text file */ /* -- file has newline delimited "records" */ /* -- records have tab delimited "fields" */ /* a C Beginner's program: */ /* IF THE COMMENTS ARE WRONG PLEASE CORRECT THEM! */ /* 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); } fclose(fin); //return 0; }
I get a segmentation fault
thanks in advance Mick



LinkBack URL
About LinkBacks


