-1

I'm using a string received from the arg to return another string with characters shuffled.

int size = strlen(argv[4]);
char* res = malloc(size+1);

Then res is filled with the characters of argv[4], but when I type 'test' in the console, it returns 'Test?' (when I'm not shuffling the letters but just copying the string, character by character).

Why is that ?

If I put size+100 instead, it works, but I don't get why.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Théo Winterhalter
  • 4,908
  • 2
  • 18
  • 34

1 Answers1

-2

That is because you did not take into account what the size of char is. This should do the trick:

char* res = (char*)malloc((size+1)*sizeof(char))