Disclosure: This is university work. I am not expecting code to accomplish my task rather I want to understand how best to manipulate strings in C.
I need to write a function to manipulate a string in a certain way (I’m not saying what so as to ensure that no-one provides exact code).
In python I’d just do the following
def foo(str):
return str
Clearly things aren’t as easy as that in C.
Can anyone tell me how best to achieve this. Should I use pointers to simulate passing by reference and just manipulate the original string? Any help / resources would be greatly appreciated.
Update: I do want to preform an operation on the string and return the result of that operation (also a string). I am happy to manipulate the original string or return it. Which ever would be considered best practice.
The task I’ve been set is based on how to do that operation so I didn’t want to make that explicit.
So the Python would be:
def foo(str):
#do something to str (which doesn't change it's length)
return str
There are two ways:
strdupormalloc + strcpyand return a pointer (char *) to it. The caller of course mustfreeit when done.It all rests on the question: will the caller need the original string ?