Thread: Help implementing function

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    10

    Help implementing function

    So I have a function:

    Code:
    char* getPlayerName();
    This function is supposed to make a string, read in a name, and then return the name. I have this function:
    ]
    Code:
    char* getPlayerName()
    {
        
        char* PlayerName;
        
        PlayerName = malloc(sizeof(char)*MAXPLAYERNAME);
        
        printf("Please input your name:");
        
        scanf("%s",PlayerName);
        
        return PlayerName;
    }
    Does anyone see problems with this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sahtopi
    Does anyone see problems with this?
    Yes:
    • You forgot to check the return value of malloc: malloc could return a null pointer.
    • You forgot to specify a field width for "%s" with scanf: without the field width, it is vulnerable to buffer overflow, i.e., the user could input MAXPLAYERNAME or more non-whitespace characters and they will all be stored in the dynamic array, even though there is only space for a string of length MAXPLAYERNAME - 1 at most.
    • You forgot to check the return value of scanf: if scanf does not return 1 (as there is one assignment to be made), perhaps you should free the memory allocated and return a null pointer to indicate the input error.

    Note that sizeof(char) is always equal to 1, so I suggest either:
    Code:
    char *PlayerName = malloc(sizeof(PlayerName[0]) * MAXPLAYERNAME);
    or:
    Code:
    char *PlayerName = malloc(MAXPLAYERNAME);
    Also, it is better to be explicit about an empty parameter list, i.e., this:
    Code:
    char* getPlayerName()
    should be:
    Code:
    char* getPlayerName(void)
    Other possible problems may involve things like whether you included <stdlib.h> and <stdio.h>, and whether you defined MAXPLAYERNAME correctly, but we cannot check these.
    Last edited by laserlight; 02-07-2016 at 11:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advanced task, implementing a function :]
    By chongmet in forum C Programming
    Replies: 18
    Last Post: 11-03-2014, 10:23 AM
  2. Replies: 9
    Last Post: 10-30-2014, 05:03 PM
  3. implementing a polymorphic function in C++
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2009, 03:13 PM
  4. Implementing Zlib function
    By vin_pll in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2008, 11:37 PM
  5. Problems implementing quicksort function
    By rvanbeusichem in forum C Programming
    Replies: 3
    Last Post: 11-24-2004, 09:00 PM