"Segment fault?"

This is a discussion on "Segment fault?" within the C Programming forums, part of the General Programming Boards category; This is a C program that I'm making, just to see if I can. It is supposed to make a ...

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    "Segment fault?"

    This is a C program that I'm making, just to see if I can. It is supposed to make a 'button' with ASCII borders. I compiled it with GCC, with no errors/warnings, and when I run it all is prints is "Segment Fault". What does that mean?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Here's the code...
    Code:
    #include <stdio.h>
    
    int make_button(char *name)
    {
    	int length;
    	int length_count;
    	
    	length = strlen(*name);
    	length_count = 0;
    	
    	/* Print Top */
    	printf("╔");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╗");
    	
    	/* Print Middle */
    	printf("║");
    	printf(" ");
    	while(length_count <= length)
    	{
    		printf("%s",*name);length_count++;
    	}
    	printf(" ");
    	printf("║");
    	length_count = 0;
    	
    	/* print bottom */
    		/* Print Top */
    	printf("╚");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╝");
    	return 0;
    }
    
    int main()
    {
    	make_button("Hello");
    	printf("\n\n");
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int make_button(char *name)
    {
    	int length;
    	int length_count;
    	
    	length = strlen(name);
    	length_count = 0;
    	
    	/* Print Top */
    	printf("╔");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╗");
    	
    	/* Print Middle */
    	printf("║");
    	printf(" ");
    	printf("%s",name);
    	printf(" ");
    	printf("║");
    	length_count = 0;
    	
    	/* print bottom */
    		/* Print Top */
    	printf("╚");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╝");
    	return 0;
    }
    
    int main()
    {
    	make_button("Hello");
    	printf("\n\n");
    	return 0;
    }
    Last edited by swoopy; 09-26-2003 at 10:23 PM.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by kinghajj
    Here's the code...
    Code:
    #include <stdio.h>
    
    int make_button(char *name)
    {
    	int length;
    	int length_count;
    	
    	length = strlen(*name);
    	length_count = 0;
    	
    	/* Print Top */
    	printf("╔");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╗");
    	
    	/* Print Middle */
    	printf("║");
    	printf(" ");
    	while(length_count <= length)
    	{
    		printf("%s",*name);length_count++;
    	}
    	printf(" ");
    	printf("║");
    	length_count = 0;
    	
    	/* print bottom */
    		/* Print Top */
    	printf("╚");length_count++;
    	while(length_count <= (length-1))
    	{
    		
    		printf("═");length_count++;
    	}
    	length_count = 0;
    	printf("╝");
    	return 0;
    }
    
    int main()
    {
    	make_button("Hello");
    	printf("\n\n");
    	return 0;
    }
    In your function you keep referring to *name. But since name is a pointer you don't need the *. You're referencing incorrectly.

    1) printf() returns the number of characters output so you could use
    length_count += printf("═");
    to keep track of the characters output.

    2) the width of your button should be
    length = strlen(name) + 2;
    to take into account the spaces you placed on each side of the label

    3) for the middle section:
    Code:
    	/* Print Middle */
    	printf("║");
    	printf(" ");
    	while(length_count <= length)
    	{
    		printf("%s",*name);length_count++;
    	}
    	printf(" ");
    	printf("║");
    you need to remove the loop, and to make it easy, one print statement should do it::
    Code:
    	/* Print Middle */
    	printf("║ %s ║", name);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    hi
    why don't you try to use "ncurses". You can create boxes, windows etc.
    man ncurses

Popular pages Recent additions subscribe to a feed

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