char* prob

Started by
5 comments, last by ph4ntasyph34r 22 years, 1 month ago
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
/* This is a comment *//* Abuse it */
Advertisement
Easy!
#Hehehe more Python advocacy!string_1="Whatever"string_2=string_1string_1+=string_1 


MWAHAHAHAHAHA!!!
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
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]
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
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.
If you''re going to bother to use C++, use std::string and std::getline.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
OH, I see what I did.
I allocated memory, just not the right amount
Thx for your help !
/* This is a comment *//* Abuse it */
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.

This topic is closed to new replies.

Advertisement