Thread: char * return

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    char * return

    i m srry can anyone post a code where it return char * coz i dont understand char * returns
    thanks alot me tried this but didnt work.
    Code:
    #include <stdio.h>
    char *buffer functionizer()
    {
    	scanf("%s",buffer);
    	return buffer;
    }
    int main(void)
    {
    	char buffer[100];
    	buffer=functionizer();
    	puts(buffer);
    	return getchar();
    }

  2. #2
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    There's nothing wrong with the return buffer; statement. But you do not have char * buffer in the scope of your function "functionizer() " You could deliver the buffer in functionizer() as an argument, or allocate it in that function (but not as local variable!).

    If you delivered the buffer in functionizer() as argument (which is better idea in my opinion), it will became
    Code:
    char *functionizer(char *buffer)
    {
    .
    .
    .
    }
    Note that you should not give name to values returned from a function. Eg.

    Code:
    //No name for returned int
     int sum(int first, int second)
    {
        return first+second;
    }
    
    //no name for returned char pointer.
    char *funcniz(char *buffer, size_t buffsize)
    {
         memset(buffer,o,buffsize);
         return buffer;
    }
    However, since in this example I pass pointer to buffer in function (Eg, location in memory where the buffer starts), I do not need to return the buffer (which once again is the location). Location has not changed, even though the contents of this location did change. In other words, modifications the function did to contents of buffer, will be visible in calling function whether we return the pointer or not.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Me tried this but still not work
    Code:
    #include <stdio.h>
    char *functionlizer()
    {
         char *buffer="HELLO";
         return buffer;
    }
    int main(void)
    {
        char buffer[100];
        buffer=functionlizer();
        puts(buffer);
        return getchar();
    }

  4. #4
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    in main replace char buffer[100] with char *buffer. You reserve the space for actual string in functionalizer. In functionalizer you use now string literal, which is not same as char array. Besides youre in danger returning a pointer to data which is not present in calling function.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah I understand now thanks

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    return getchar();
    I don’t really appreciate using this paradigm to return a value from main. IF the getchar fails you’re returning EOF which is not your main objective. Better to type in return 0.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better have a look at
    SourceForge.net: Common mistakes and errors - cpwiki
    Especially string literals and using pointers without allocating them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 10:33 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM