0

Click here to see the code, initially I've written a simplified version in the main function and it passed all the tests, and then I made the general function seen in the link provided and now it passes no tests except the "exists" and "complies".

EDIT: Note that in the terminal itself it works as intended, prints the initials of whatever string and a newline. The only problem is that it doesn't passes the tests.

Click here to see the details of the tests.

Any Idea what's going on ?

1

This is... a pointer and memory problem. Which is something you'll learn much more about in future weeks. Essentially the problem is this: each function has a portion of memory that it uses for local variables, including your: newAr and outstr vars. This memory is called a local stack frame. When the function is done executing, the program assumes this memory is no longer needed, and the next function call will overwrite its contents with its own local variables.

The confusion comes from the fact that a variable of type string does not actually contain a string, but a pointer to a memory location that contains a sequence of characters (and a NULL character). Your return value is a pointer into the stack frame that no longer exists. Once you call printf, that function starts overwriting that memory with its own variables. Your results will likely be garbage.

The solution you will eventually use is to create a character buffer in the main function and pass its address to your function, so that your string is stored in main's stack frame and not your function's stack frame. Another solution is dynamic memory allocation. Both of these are described more in depth later in the class.

For now, you might just want to move all of your code into the main function. After the lessons on pointers, come back and reimplement this function with what you've learned.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .