Thread: string.h functions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    string.h functions

    sorry, simple mistakes, fixed now

    From lesson 9 C strings (cprogrmming.com)

    This program builds with no errors, but 2 warnings.
    But the problem is when I run it, after I enter the first input (name) I get
    "Program received signal: “EXC_BAD_ACCESS”.
    sharedlibrary apply-load-rules all
    (gdb)"

    Code:
    #include <stdio.h>
    #include <string.h>
    
    //this fuction removes newline from string entered by fgets
    void strip_newline(char *str, int size){
    	int i;
    	for (i = 0; i < size; ++i) {
    		if (str[i] == '\n') {
    			str[i] = '\0';
    			return; // we're donr  so exit the function by returning
    		}
    	}
    	//if we get this there must be no newline
    }
    
    int main() 
    {
    	char name[50];
    	char lastname[50];
    	char fullname[100]; // big enough to hold name + last name
    	printf("Please enter your first name: ");
    	fgets(name, 50, stdin);
    	strip_newline(name, 50);
    	
    	//strcmp returns 0 when 2 names are equal
    	if (strcmp (name, 'Alex') == 0 ) {
    		printf("That's my name too!\n");
    	}
    	else {
    		printf("That's not my name.\n");
    	}
    
    	//find the length of your name
    	printf("Your name is %d letters long", strlen, name);
    	
    	printf("Enter last name");
    	fgets(lastname, 50, stdin);
    	strip_newline(lastname, 50);
    	
    	fullname[0] = '\0'; //strcat will look for 0 and add second string 
    						//starting from that location
    	strcat(fullname, name); //copy name to full name
    	strcat(fullname, " "); //add space between names
    	strcat(fullname, lastname);
    	printf("Your full name is %s\n", fullname);
    	getchar();
    	return 0;
    }
    Last edited by dunsta; 04-22-2010 at 08:31 AM. Reason: simple mistakes, should be "Alex" and strlen (name) no comma

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This looks like a simple error. Calling strlen(), with no parameters:
    Code:
    printf("Your name is %d letters long", strlen, name);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > This program builds with no errors, but 2 warnings.
    Yes, and I bet one of them was about a large character constant for 'Alex' right?

    > But the problem is when I run it, after I enter the first input (name) I get
    You ignored warnings you didn't understand.

    When you compile code, try this
    gcc -W -Wall -ansi -pedantic -Werror prog.c
    It will tell you about a lot of dumb things which should be avoided.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2009, 12:10 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM