#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Arrays and Strings


    Greetings

    I need a program to do the following:

    To write a function that displays a simple message to the user. The function must be called displayMessage. It must have one parameter; a pointer to a character. This parameter is the name of the user. The function must return a string that says hello to the user and addressing him/her using the first name.

    Here is my code , it gives me a "segmentation fault" when I enter a name of a person and press enter during the "run" stage.


    Code:
    #include <stdio.h>
    #define SIZE 100
    
    char displayMessage (char *Ptr);
    
    int main(void)
    {
        
        char name[SIZE];
        
        printf("Please enter your name: ");
        //Add statements here for retrieving the name, calling your function and displaying the resulting message
        scanf("%s",name);
        
        
    
        
        printf("Hello" "%s",displayMessage(name));
        
        return 0;
    }
    
    char displayMessage (char *Ptr)
    {
    int i;
    char temp[100];
    char *tptr;
        
        tptr= &temp[0];
        
                
                        for (i=0;*(Ptr+i) != ' ' && *(Ptr+i) != '0' ;i++){
                                 temp[i]= *(Ptr+i);
                                  }
                      
                      return *tptr;
                                  
                      
    }
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    One thing you're doing wrong is returning a pointer to a local stack-created array in your function. That's undefined behavior. You're also comparing your character to '0', which is not the null terminator character I believe you're looking for, which is '\0'.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by ptr2void
    One thing you're doing wrong is returning a pointer to a local stack-created array in your function. That's undefined behavior. You're also comparing your character to '0', which is not the null terminator character I believe you're looking for, which is '\0'.
    how would you return the temp array to the calling function? without using that pointer? an im still getting the Segmentation Fault after correcting the Null error could you please correct the code?

    thanks
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Try moving that char array off the stack with static.

IMN logo majestic logo threadwatch logo seochat tools logo