Why does printf leave some random characters in the main function? [C program]

Viewed 336

I want to get info of someone in the getInfo function.

If I print the name when I'm IN the getInfo function, it prints it out correctly, but after that in the main it leaves some random characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* getLine(){
    char *line = malloc(256);
    fgets(line, 256, stdin);
    if ((strlen(line) > 0) && (line[strlen (line) - 1] == '\n'))
        line[strlen (line) - 1] = '\0';
    return line;
}

void getInfo(char* name,char* place,char* days[]){
    printf("Type in your name:\n");
    name = getLine();

    printf("%s",name);                      // <---- THIS PRINTS OUT CORRECTLY
}

int main( int argc, const char* argv[] )
{
    char* name = (char*)malloc(100*sizeof(char));
    char* place = (char*)malloc(100*sizeof(char));
    char* days[5];

    getInfo(name,place,days);

    printf("%s\n",name);                   // <----- THIS LEAVES SOME CHARACTERS
}

OUTPUT:

C:\Users\Felhasznalo\Desktop>a.exe
Type in your name:
Peter Smith
Peter Smith`\┴

These " `\┴ " characters are what I'm talking about. The first print in the function prints it well, but in the main it gives these ghost characters.

Why are these written out? Thanks.

2 Answers

The problem is that you are replacing the 'name' pointer that your main function passes to getInfo(). This is because getInfo() calls getLine(), which itself creates a new character array, and returns a pointer to this buffer to getInfo().

Inside getInfo(), this is printed correctly (as you note); however, the main function passes a copy of the name pointer (i.e. by value) - and this is not (cannot) be changed by the getInfo function. So, in main you are printing out an uninitialized character array (name).

There are several ways to fix this. One would be to pass the name (and, presumably, place) argument(s) to getInfo() as a pointer to a pointer, when you will need to de-reference this inside that function; like so:


void getInfo(char** name, char** place, char* days[])
{
    printf("Type in your name:\n");
    *name = getLine();
    printf("%s", *name);
}

Then, in your main, you: (a) don't need to allocate memory for name (and place), as this is done by the getLine() function; (b) pass the address of the name pointer to getInfo(). Like this, for example:

int main(int argc, const char* argv[])
{
    char* name;// = (char*)malloc(100 * sizeof(char));
    char* place;// = (char*)malloc(100 * sizeof(char));
    char* days[5]; // Don't know what you want to do with this!
    getInfo(&name, &place, days);
    printf("%s\n", name);
    // Don't forget to free the allocated buffers:
    free(name);
//  free(place);
    return 0;
}

Feel free to ask for further clarification and/or explanation.

name = getLine(); in getInfo() does not affect name in main().

Try returning name from getinfo() and in main() assign char *name = getline(...).


Aside: Alternative code for getline()

char* getLine(){
  char buffer[256];
  if (fgets(buffer, sizeof buffer, stdin) == NULL) {
    return NULL;
  }
  size_t len = strlen(buffer);
  if (len > 0 && buffer[len - 1] == '\n') {
    buffer[--len] = '\0';
  }
  char *line = malloc(len + 1);
  if (line) {
    memcpy(line, buffer, len + 1);
  }
  return line;
}
Related