Can anyone show me what is wrong here? Segmentation fault

This is a discussion on Can anyone show me what is wrong here? Segmentation fault within the C Programming forums, part of the General Programming Boards category; Anyone have any ideas on how to alphabetize by last name the list of names in the file?...

  1. #16
    Registered User
    Join Date
    Nov 2007
    Posts
    39
    Anyone have any ideas on how to alphabetize by last name the list of names in the file?

  2. #17
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I got your code working. Tough assignment for me. (still learning C). Been working on it since noon, about 9-10 hours of work I guess. Thought I'd share.

    One of the issues you had was the fscanf() reading the count. It was leaving the CRLF in the input stream, and the first fgets() to read a record read only the CRLF.

    Also, your pointers are a level deeper than the need to be.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int read_file(char **first_name[], char **last_name[], char **age[]) {
    	int count, i;
    	char line[80];
    	FILE *fp = fopen("names.txt", "r");
    	if (fp==NULL) return 0 ; 
    	
    	//printf("fp = %08X, *fp = %08X\n", &fp, fp );
    
    	fgets(line, 80, fp); //count = how many names in the file on the first line
    	count = atoi(line) ; 
    	
    	*last_name =  malloc(count * sizeof(char *));
    	//printf(" 3 word area for last name pointers starts @ %p\n", *last_name);
    
    	*first_name = malloc(count * sizeof(char *));
    	//printf(" 3 word area for first name pointers starts @ %p\n", *first_name);
    	
    	*age = malloc(count * sizeof(char *));
    	//printf(" 3 word area for age pointers starts @ %p\n", *age);
    
    	for (i = 0; i < count; i++) {
    
    		fgets(line, 80, fp);
    		(*last_name)[i]  = malloc( 21 * sizeof(last_name) ); // area for last name 
    		(*first_name)[i] = malloc( 21 * sizeof(first_name) ); // area for first name 
    		(*age)[i]        = malloc(  4 * sizeof(age) ); // area for age 
    		strcpy( (*last_name)[i] , strtok(line,", \n") ) ; 
    		strcpy( (*first_name)[i], strtok(NULL,", \n") ) ; 
    		strcpy( (*age)[i]       , strtok(NULL,", \n") ) ; 
    	}
    	return count ; 
    }
    
    int main() { 
    	int i, count ; 
    	char **ages, **first_names, **last_names ;
    	char **ln, **fn, **age ; 
    	
    	count = read_file(&first_names, &last_names, &ages);
    
    	ln  = (char **) last_names  ; 
    	fn  = (char **) first_names ; 
    	age = (char **) ages ; 
    		
    	for ( i = 0 ; i < count ; i++ ) {
    
    		printf("Lastname=%s, Firstname=%s, Age=%s\n", 
    			*ln,  
    			*fn , 
    			*age ) ; 
    			ln  += 1 ; 
    			fn  += 1 ; 
    			age += 1 ; 
    	}
    
    	return 0;
    }
    I added code at the end to print out the records too. It's really not that much code when it comes down to it.

    Todd

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Using triple-pointers can be okay, at a push.
    The reason you're using them in this case though is simply to pass double-pointers as output parameters. That being the case, inside your function you should begin by declaring some double-pointers, and then work with those throughout the function. Then when it comes time to return them you can simply do an assignment to *first_name *last_name and *age from the double-pointers.
    And viola, you're down to double-pointer for most of the code.
    Then, you can take that a step furthur doing the same thing within your loops, and all of a sudden you're down to the familiar plain old single-pointer and the whole thing seems so much more comprehensable!

    Not to mention it will probably be faster then as well, since the compiler will have less trouble working out what pointer aliasing could be going on, and can hence do a better job of optimisation.
    Last edited by iMalc; 11-22-2007 at 12:25 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21