Returning char array pointer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Returning char array pointer

  1. #1
    Join Date
    Jun 2009
    Posts
    6

    Returning char array pointer

    Hi!

    In an effort to expand my C++ knowledge, I'm writing a little application that generates a random string of lowercase characters, and then returns a pointer that that array. I've got a majority of the app working, it generates the character array fine, but when I try to read the character array outside of the function, I get a bunch of jargon characters.

    Here is my code:
    Code:
    /*
    	 	 * Create a random string
    	 	 */
    	 	char* randfname() {
    		 	int len = 16;				// len of str 
    		 	srand(time(0));				// to get a true random number
    		 	char fname[len];			// char arr that stores file name
    	 		char* ptr = fname;			// pointer to fname
    		 	for(int i = 0; i < len; i++) {
    			 	int ascii = (rand() % 25) + 97;		// Randomally generate ascii lower case
    			 	*(ptr+i) = ascii;
    //			 	fname[i] = ascii;
    		 	}
    		 	for(int i = 0; i < len; i++) {			// Test print char arr w/ ptr
    			 	cout << *(ptr+i);					// Works here
    		 	}
    	 		return ptr;
    	 	}
    
    ...
    
    /*
    	 	 * Test function to ensure that randfname()
    	 	 * is working.
    	 	 */
    	 	 void testrandfname() {
    		 	 char* ptr = randfname();	// call randfname
    		 	 cout << endl;
    		 	 for(int i = 0; i < 16; i++) {
    			 	 cout << i << ": " << *(ptr+i) << endl;
    		 	 }
    	 	 }
    When I print out the array in the randfname() function, it works fine, however, when I pass the pointer to the testrandfname() function and I attempt to print out the array, it doesn't work and I get rubbish data.

    I think there is an error where I assign the char* ptr to the pointer returned from randfname() but I'm not sure.

    I've tried several different things, but I always arrive at printing rubbish data. Could anybody help me properly read the character array that is returned?

    Thanks in advice!
    BWiz

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,275

    Re: Returning char array pointer

    The problem is that ptr points to the first element of the local array named fname. When control returns from this function, the array is destroyed, so the caller gets a pointer to an element that no longer exists.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Posts
    6

    Re: Returning char array pointer

    Thanks for the response laserlight.

    So if the array is destroyed, how would I save the array so that other functions could read it? And how would I point to that array?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,275

    Re: Returning char array pointer

    Use and return a std::vector<char> instead.

    Oh, incidentally, you were using a compiler extension earlier: fname is actually a variable length array. If you really intend a fixed size array, you could consider a std::tr1::array<int, 16>, but a TR1 implementation may not be available for you to use.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2009
    Posts
    6

    Re: Returning char array pointer

    Actually, if I passed a pointer to the array (which is set by the calling function) to randfname, and the length of the array, the array would be preserved right? And I could use that array in the calling function right?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,275

    Re: Returning char array pointer

    Quote Originally Posted by BWiz
    Actually, if I passed a pointer to the array (which is set by the calling function) to randfname, and the length of the array, the array would be preserved right? And I could use that array in the calling function right?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2009
    Posts
    6

    Re: Returning char array pointer

    Great! Thanks a lot laserlight!

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

    Re: Returning char array pointer

    For completeness: Another "solution" would be to declare fname as static, thus it would continue to exist after the function exited.

    Solution is written in quotes, as this approach imposes a complete new set of problems, i.e. dependencies between multiple calls to this function and the unability to use this function in multi-threaded program. Therefore the "For completeness".
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center