Archived

This topic is now archived and is closed to further replies.

char* prob

This topic is 5748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, My problem is this, I want to create a string, copy a string into that string and copy that string into another string...you follow me still? I've got some code down, but it crashes once i create the second string.
    
    void main(){
	char *temp1;
	sprintf(temp1,"STUFF");
	puts(temp1);
	char *temp2;
	sprintf(temp2,temp1);
	puts(temp2);
       }
    
I tried commenting out the strcpy and puts at the end but it still crashes, it works fine if i don't create the second temp2 variable. I've also tryed using malloc(...) and free(...), but they don't seem to do anything usefull. I am using MSCV++ 6.0, and building in release mode. Your help will be appreciated!!! Edited by - ph4ntasyph34r on March 18, 2002 10:41:42 PM

Share this post


Link to post
Share on other sites
When you do char *temp1; all you're doing is creating a pointer. You're not creating any space for your string. Change that to char temp1[40]; or something, depending on how much space you need. If you really want a pointer you can go

char *temp1;
temp1 = new char[40];
...
delete [] temp1;

That way you're allocating enough memory for the string.

[edited by - Tron3k on March 18, 2002 10:50:50 PM]

[edited by - Tron3k on March 18, 2002 10:51:33 PM]

Share this post


Link to post
Share on other sites
couldnt you use this?

// Gets the message from the user.
con << "Enter your secret message: " << ''\n'';

// This is the max input including the null terminator available.
const int NameSize = 500;

// Declares the message string.
char message[NameSize];

// Initializes the message string.
cin.get(message, NameSize);
cin.get();

// Computes the message / by 2 and adds 1 if it is an odd message.
int x = (strlen(message) + 1);

// Declares the first half of message.
char *string1 = new char[x + 1]; // Add one for the null terminator.
strcpy(string1,message);

// Declares the second half of message.
char *string2 = new char[x + 1];
// Initializes the second half of message.
strcpy(string2, string1);

// Declares the second half of message.
char *string3 = new char[x + 1];
// Initializes the second half of message.
strcpy(string3, string2);

con << ''\n'' << "Your message is: " << string3;


// Clears the message.
delete []string1;
delete []string2;
delete []string3;

Sorry about the long code but this is only my 2nd week in c++ class.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If your programming in C++, save yourself a lot of time and pain and use std::string e.g.


  
#include <string>
#include <iostream>

int main()
{
std::string string1 = "String";
std::string string2 = " concatenation.";
std::string string3 = string1 + string2;

std::cout << string3 << std::endl;

// Makes MSVC++ 6 happy

return 0;
}


This is much more robust than any C example you could come up with. For example, you don''t have to worry about dynamic memory or buffer overruns.

Share this post


Link to post
Share on other sites