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? ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    There is no string type in C although you could typedef it to be a pointer to char something like
    Code:
    typedef char *String
    and then return String from your function.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    There is no string type in C although you could typedef it to be a pointer to char something like
    Code:
    typedef char *String
    and then return String from your function.
    And Elysia will tell you off for using * in a typedef.

    I don't thin this particular case is a good use of typedef. Using typedef in this way just obfuscates normal code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by matsp View Post
    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 *'?

    Thanks

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21