I think the most significant problem is that you're incrementing code
as you read stuff in, and then returning the final value of code
, i.e. you'll be returning a pointer to the end of the string. You probably want to make a copy of code
before the loop, and return that instead.
Also, C strings need to be null-terminated. You need to make sure that you place a '\0'
directly after the final character that you read in.
Note: You could just use fgets()
to get the entire line in one hit.