CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: char*

  1. #1
    Join Date
    Jul 2007
    Posts
    20

    char*

    Just testing and need to know how to make this code work without using dynamic mem allocation for b?.
    Code:
    char* rchar()
    {
    	char b[100];
    	strcpy(b, "Hello World\n");
    	return (char*&)b;
    }
    int main(int argc, char* argv[])
    {
    	char *s = rchar();
    	printf("%s",s);
    	return 0;
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: char*

    Code:
     char* rchar()
    {
    	static char b[100];
    	strcpy(b, "Hello World\n");
    	return b;
    }
    Kurt

  3. #3
    Join Date
    Aug 2007
    Posts
    39

    Re: char*

    Quote Originally Posted by cs8
    Just testing and need to know how to make this code work without using dynamic mem allocation for b?.
    Code:
    char* rchar()
    {
    	char b[100];
    	strcpy(b, "Hello World\n");
    	return (char*&)b;
    }
    int main(int argc, char* argv[])
    {
    	char *s = rchar();
    	printf("%s",s);
    	return 0;
    }
    hello cs8,
    this is how one way you can work without error.
    Code:
    #include<cstdio>
    #include<cstring>
    
    char* rchar()
    {
    	char *b = "hello world";
            return b;
    }
    int main(int argc, char* argv[])
    {
    	char *s = rchar();
    	printf("%s",s);
    	return 0;
    }
    
    OUTPUT:
    hello world

  4. #4
    Join Date
    Jul 2007
    Posts
    20

    Re: char*

    How about if return is int*?.
    Code:
    int* f()
    {
    	int i[4];
    	i[0] = 1;
    	i[1] = 2;
    	i[2] = 3;
    	i[3] = 4;
    
    	int* u = i;
    	return u;
    }
    int main(void)
    {
    	int* g = f();
    	printf("%d\n", *g++);
    	printf("%d\n", *g++);
    	printf("%d\n", *g++);	
    	printf("%d\n", *g++);
    
    	return 0;
    }

  5. #5
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: char*

    Quote Originally Posted by cs8
    Code:
    char* rchar()
    {
    	char b[100];
    	strcpy(b, "Hello World\n");
    	return (char*&)b;
    }
    When a function returns, its automatic, local variables are discarded, so the returned pointer in this case is invalid (it points to an array that no longer exists).
    Quote Originally Posted by Zuk
    Code:
    char* rchar()
    {
    	static char b[100];
    	strcpy(b, "Hello World\n");
    	return b;
    }
    This fix is imperfect, since a function using static data is not reentrant.

    There can be many alternate ways like
    • Returning dynamically allocated data. In this case, it will be the caller's responsibility to free the storage.

    • Using caller-provided storage. This method is recommended, although the interface needs to be modified.
    • Combination of above two: if the caller passes a null pointer, the routine returns a dynamically-allocated pointer.

    • Combination of Zuk's method & caller-provided storage :if the caller passes a null pointer, the routine returns a pointer to static data(non-reentrant).
    Last edited by sunnypalsingh; August 18th, 2007 at 10:05 PM.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  6. #6
    Join Date
    Jul 2007
    Posts
    20

    Re: char*

    what's right way for this?:

    Code:
    int* f()
    {
    	int i[4];
    	i[0] = 1;
    	i[1] = 2;
    	i[2] = 3;
    	i[3] = 4;
    
    	int* u = i;
    	return u;
    }
    int main(void)
    {
    	int* g = f();
    	printf("%d\n", *g++);
    	printf("%d\n", *g++);
    	printf("%d\n", *g++);	
    	printf("%d\n", *g++);
    
    	return 0;
    }

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: char*

    Quote Originally Posted by klamath23
    hello cs8,
    this is how one way you can work without error.
    [code]
    There is a big difference between what the OP was trying to do, and what you're showing. That difference is that the OP declared an array of char, and attempted to return it (which of course wouldn't work). What you declared is a string-literal. This is different than an array of char, since you cannot modify a string-literal. If the OP later on wanted to modify the array, returning a string-literal is not recommended.

    The easiest thing to do if this is C++ is just return a std::string (there seems to be that a lot of posters lately are struggling to figure out how to return char *).

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: char*

    Quote Originally Posted by cs8
    what's right way for this?:
    You cannot return a pointer to a local variable.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: char*

    Quote Originally Posted by cs8
    what's right way for this?:
    Code:
    #include <vector>
    #include <cstdio>
    
    std::vector<int> f()
    {
       std::vector<int> i(4);
       i[0] = 1;
       i[1] = 2;
       i[2] = 3;
       i[3] = 4;
       return i;
    }
    
    int main()
    {
    	std::vector<int> g = f();
            int *pg = &g[0];
    	printf("%d\n", *pg++);
    	printf("%d\n", *pg++);
    	printf("%d\n", *pg++);	
    	printf("%d\n", *pg++);
    	return 0;
    }
    Another way.
    Code:
    #include <cstdio>
    
    void f(int *i)
    {
       i[0] = 1;
       i[1] = 2;
       i[2] = 3;
       i[3] = 4;
    }
    
    int main()
    {
       int g[4];
       f(g);
       printf("%d\n", *g++);
       printf("%d\n", *g++);
       printf("%d\n", *g++);	
       printf("%d\n", *g++);
       return 0;
    }
    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: char*

    You are assuming, Paul, that this is C++ but the OP has posted code that is pure C so let's assume that it is pure C because otherwise there is no reason not to return string. (Let's ignore "embedded C++" for now). Note that it may well be a C++ library with a C interface.

    There is another option which is to return a struct that has an array of chars big enough to hold this particular string.
    Code:
    struct char_array_32
    {
      char data[32];
    };
    
    struct char_array_32 rchar()
    {
       struct_char_array output;
       strcpy( output.data, "Hello world!" );
       return output;
    }
    Of course you may well define different varieties of this struct but it will allow you to get away with not having to make runtime allocations as long as you can be certain to return a struct big enough.

    There is a preferable way, which is commonly used in C interfaces and that is to get the user to provide the write buffer and a maximum write size.

    We will also return the size of the buffer that we have written or what we wanted to write.
    Code:
    size_t rchar( char * buffer, size_t insize )
    {
       strncpy( buffer, "Hello world!", insize );
       return 13; // in reality you would return variable data 
    }
    Note this technique can at times be very useful in C++ too.

  11. #11
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: char*

    Quote Originally Posted by Paul McKenzie
    What you declared is a string-literal. This is different than an array of char, since you cannot modify a string-literal.
    This is a bit off-topic, but as Paul has mentioned it, something I have been wondering for a long time:

    How come it is legal to assign string literals to (non-const) char*? I am not even getting a compiler warning for the following:
    Code:
    #include <iostream>
    
    int main() {
      char * c = "Hello World!";
      std::cout << c << std::endl;
    }
    Is c now really a char * or a const char * in disguise?

  12. #12
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: char*

    Quote Originally Posted by treuss
    This is a bit off-topic, but as Paul has mentioned it, something I have been wondering for a long time:

    How come it is legal to assign string literals to (non-const) char*? I am not even getting a compiler warning for the following:
    Code:
    #include <iostream>
    
    int main() {
      char * c = "Hello World!";
      std::cout << c << std::endl;
    }
    Is c now really a char * or a const char * in disguise?
    Because of historical reasons a string literal can be assigned to a char*. This is allowed because in previous definitions of C and C++ , the type of a string literal was char*. Allowing the assignment of a string literal to a char* ensures that millions of lines of C and C++ remain valid.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  13. #13
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: char*

    Quote Originally Posted by Paul McKenzie
    Another way.
    Code:
    #include <cstdio>
    
    void f(int *i)
    {
       i[0] = 1;
       i[1] = 2;
       i[2] = 3;
       i[3] = 4;
    }
    
    int main()
    {
       int g[4];
       f(g);
       printf("%d\n", *g++);
       printf("%d\n", *g++);
       printf("%d\n", *g++);	
       printf("%d\n", *g++);
       return 0;
    }
    That's what I was going to suggest as well, but I would pass to f the number of elements which the array contains.

    Something like:

    Code:
    void f(int *i, size_t n)
    {
        if(n >= 4)
        {
            i[0] = 1;
            i[1] = 2;
            i[2] = 3;
            i[3] = 4;
        }
    }
    Of course you would then also need some kind of error reporting if n does not have an appropriet value.
    Last edited by Zaccheus; August 20th, 2007 at 06:05 AM.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured