Thread: return char array

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    return char array

    I need to create a function that returns a char array. I am not sure how to do this. I designed a custom string class for school and one of the functions has to convert the string to a char array and return the char array. TIA

    Edit:
    This now works in a way. I still have a problem though:
    Code:
    char * swcString::swcC_Str()
    {
    	char * charArray = new char[swcSize()+1];
    
    	for (int i=0; i <= swcSize(); i++)
    	{
    		if (i < swcSize())
    		{
    			charArray[i] = myString[i];
    			cout << "charArray[i] = " << charArray[i] << endl;
    		}
    		else
    		{
    			charArray[i] = '\n';
    		}
    	}
    
    	return charArray;
    }
    Everything here works fine. I get the string converted into a char array and even newline works but then there is a bunch of garbage and i dont know why. a sample output would be:

    First Name: MyFirstName
    Middle Name: MyMiddleName
    Last Name: MyLastName

    Your are MyFirstName MyMiddleName MyLastName!

    Your first name is 11 characters long!
    Your middle name is 12 characters long!
    Your last name is 10 characters long!

    MyFirstName MyMiddleName MyLastName is 35 characters long!

    Charater 4 in your First name: i
    charArray[i] = M
    charArray[i] = y
    charArray[i] = F
    charArray[i] = i
    charArray[i] = r
    charArray[i] = s
    charArray[i] = t
    charArray[i] = N
    charArray[i] = a
    charArray[i] = m
    charArray[i] = e

    First Name as char array: MyFirstName
    ²²²²▌▌▌▌▌▌▌▌A
    Press any key to continue
    Last edited by Bitphire; 03-06-2005 at 07:06 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Post has been Edited.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    c-strings, i.e. character arrays, end in '\0' not '\n'. The '\0' is a signal to cout<< that the end of the string has been reached. When a c-string doesn't have that flag, you can't output the string using the array name like this:

    cout<<charArray<<endl;

    which I assume is what you did. When you do that, cout<< doesn't know when to stop, so it keeps outputting values at index positions that are beyond the length of the charArray.

    In addition, you don't need to have those if statements in your for loop, which just slows down your program. Make the loop control:

    i < swcSize()

    and the first line after the for-loop tack on a '\0' to the string at position charArray[swcSize()]. If an array size is 10, then the index values of the array are 0-9, and a for loop traversing the whole array would be:

    i < size(=10)

    If you need to create an array that is 1 larger to accomodate a '\0', then the array will be size 11, and the index values will be 0-10. So, if you use the same loop control as above:

    i < size(=10)

    you will traverse every element of the array except the last one, namely the element at index position 10. So, after the for loop ends, you can just assign a '\0' to that element, which is at index position 'size'(=10).
    Last edited by 7stud; 03-06-2005 at 08:00 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Thanks for the reply. I had just found another forum from google and just solved the problem. I also originally had the '\n' part after the for loop but was trying everything to get this to work and that was one thing i did. Everything is back the way it should be and works. Thanks again for the reply even if it was a little late :P.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I had just found another forum from google and just solved the problem.
    That's called cross posting, and the reason you shouldn't do that is so people don't waste time posting solutions to questions that have already been answered on other forums.

    I'll be sure to avoid your posts in the future. Come to think of it, that's probably why no one posted a solution before this.
    Last edited by 7stud; 03-06-2005 at 08:04 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    no, i did not make the post. i found the answer in some one elses post from some other forum. I was doing a google search and i found someone elses post. Is that wrong? I did not post on another forum asking the same question.

    Also as soon as i found the answer i was coming here to let others know i had found a solution

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok. Sorry for jumping all over you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM