char* - char[] - sprintf-issues

This is a discussion on char* - char[] - sprintf-issues within the C++ Programming forums, part of the General Programming Boards category; heyo community! got a small problem here. i defined an array of char* Code: class CGameText { public: CGameText(void); ~CGameText(void); ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    united kingdom of austria
    Posts
    2

    char* - char[] - sprintf-issues

    heyo community!

    got a small problem here.

    i defined an array of char*
    Code:
    class CGameText
    {
    public:
    	CGameText(void);
    	~CGameText(void);
    ....
    
    	char* getCowText(char* cCowName, int nRow);
    private:
    ...
    	char* cowText[12];
    	int nRan;
    ....
    };
    the function getcowtext should change a field in my char*-array by the use of sprintf but i get an access-violation. this is the code:

    Code:
    char* CGameText::getCowText(char* cCowName, int nRow)
    {
    	if(nRow == 9)
    	{
    		if(cowText[9])
    			delete(cowText[9]);
    
    		sprintf(cowText[9],"the cow's name is... %s",cCowName);
    	}
    
    	return cowText[nRow];
    }
    why isn't this working?

    please help!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You didn't say how it's not working. From the code, one thing is pretty obvious, though.

    You need to allocate space for the characters. I don't see any code that calls new on each pointer in cowText. You must do this before you use those pointers. Even if you have that code, in your function, you delete the memory at index 9, but then try to use it. The sprintf function doesn't allocate memory, so you have to actually call new to allocate new space for the new string.

    I'd suggest using the C++ string class instead of C style character arrays. It handles the memory for you.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well cowText is an array of pointers, not a pointer to an array.

    I didn't check your code, but that's what caught my eye at first. If you want a pointer to an array, you have to do something like this:
    Code:
    char*cowText=new char[20];
    ...
    delete[]cowText;	//don't forget to deallocate
    now you'll notice that you can't exactly do that in a class, but just throw the assignment of the pointer into the constructor or something, and the destuctor is usually a good place for the delete line.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    united kingdom of austria
    Posts
    2
    hey, thanks!

    this new char[20]-solution worked fine for me!

    ohhh...and my "justification": i'm not new to programming but new to c++ and this memory-allocate-stuffidiestuff.

    sooo...thanks a lot!

    ohhh...and another memory-question.

    i'm allocating my cowarray in the constructor with values such as
    cowText[6] = "blablabla";

    do i also have to delete all this cowtext-stuff in the destructor?...ohhhh...no wait...this delete[] with [] deletes the whole array, right? wow, great!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> cowText[6] = "blablabla";

    Show your code again. I think you're still confused. The code above doesn't work with major_small's solution, and it won't work if you later change the string with sprintf.

    And if you're new to C++, you should probably start with using C++, which is why you should be using C++ strings instead of C style character arrays.

    If you'd still prefer to use the C way, then you need to be more clear about what cowText is supposed to be. Is it supposed to hold one string, or many? It looks like it should hold many since you have the nRow parameter, in which case your original code is correct, but you need to do the new char[20] for each pointer in the array of pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question regarding char *argv[]
    By cnb in forum C Programming
    Replies: 6
    Last Post: 10-08-2008, 04:50 AM
  2. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  3. sprintf giving me issues
    By ramparts in forum C Programming
    Replies: 35
    Last Post: 11-14-2006, 11:40 AM
  4. Replies: 18
    Last Post: 06-21-2003, 10:57 AM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

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