String function. Type?

This is a discussion on String function. Type? within the C Programming forums, part of the General Programming Boards category; I want to declare a function that outputs a string, but how do I name the type of the function? ...

String function. Type?

I want to declare a function that outputs a string, but how do I name the type of the function? For example int function() returns an integer, char function() returns a char, but what if I want to output a string? I tried using "string" as the type but it wasn't rcognised, so do I have to use char along with the square brackets somehow?

Also, I then want to use this function in a for loop and concatenate them into one. If I do this, does it ignore the newline '\n' at the end of each string?...so the concatenated string will be all on one line when I print it.

If you return a string, you will be returning a char *, but very often it is better to pass in a string to be filled in, as it's tricky to pass back anything other than constant strings from a function.

If you return a string, you will be returning a char *, but very often it is better to pass in a string to be filled in, as it's tricky to pass back anything other than constant strings from a function.

--
Mats

I'll probably have to pass in a string then and fill it in, then can I just use int as type and return 0?...will it still operate on the string?

Also, what are all the *'s that you use, is it something to do witrh pointing to the contents of a memory, like '&' points to the adress in scanf? so how does this relate to a string being 'char *'?

Yep. A char * is a pointer to a memory block containing chars. It can either be treated as a reference to a single block of memory or treated as a block of memory that is many units in size. That portion is always implementation specific. Usually with strings though, a char * is considered to be an array of chars. Typically one does not interpret a function with char * as a parameter to be a situation of passing a char by reference.