Original Post
char blah[255];
blah = "foobar";
char* meh;
sprintf(meh,"%s",blah);
printf("%s",meh);
char blah[255];
blah = "foobar";
char* meh;
sprintf(meh,"%s",blah);
printf("%s",meh);
std::string meh() { std::string ret_val; // Assign ret_val in here somewhere. return ret_val;}char blah[255]; // creates an array of 255 chars on the stack (or if global, then in the data section of the programblah = "foobar"; // this is incorrect and should not even compile, you cannot change what an array points to.sprintf(blah,"%s","foobar"); // copy the contents of the constant string foobar into the character array blahchar blah2[255] = "foobar"; // a special case (using the = operator on a declaration line IS NOT the same as calling = later), this allocates 255 chars with the initial values of: {'f','o','o','b','a','r','\0', ???}// i can't remember if the values after the null terminator are undefined, or if they are all initialized to 0 (I think 0).char* meh; // declares a pointer to character(s)sprintf(meh,"%s",blah); // error, you are copying characters into where??? wherever meh points, which is either 0, or a random memory address ...char *meh2 = "foobar"; // this allocated a char pointer pointing to the constant string "foobar" - KIND OF like the character array earlier, except this does not allocate space on the stack, and you are not allowed to change the contents of "foobar"sprintf(meh2,"%s",blah); // error, you are copying characters into a contant string ... not legalmeh2 = blah2; // valid, this makes meh2 POINT to the character array blah2printf("%s",meh);char *meh3; // allocate a pointermeh3 = new char[strlen(blah)]; // allocated a character array just big enough to hold "foobar"strcpy(meh3,"%s",blah); // copy the contents of blah into the buffer pointed to by meh3Quote:
Original post by Doc
Use std::string instead. Seriously. You're just asking for trouble, otherwise.
*** Source Snippet Removed ***
Quote:
Original post by Anonymous Poster Quote:
Original post by Zeophlite
Whats wrong with my code?
You need to add "meh = &blah[0];" before the sprintf.
It should work perfectly without any other changes.
Quote:
Original post by Drew_Benton Quote:
Original post by Doc
Use std::string instead. Seriously. You're just asking for trouble, otherwise.
*** Source Snippet Removed ***
Seconded like there is no tomorrow!
Quote:
Original post by Zahlman
And thirded. Assuming you are able to use C++. In which case, use streams as well (instead of the C-style *print* family of functions).
Quote:
I see,
In that cases, how do I set the function type for a function that returns a char array:
char meh() {
char goo[12];
...
return goo;
}
What would I change "char meh() {" to, to allow it to return a char array?
// "C++" codechar* meh(void){ char* goo = (char*)malloc(12*sizeof(char)); // optional:a char is always 1 byte return(goo); // goo is now valid because it is resident on the heap}...char* pchar = (char*)malloc(12);...// when you no more need the array remember to free itfree(pchar);// now pchar is invalid!Quote:
Original post by Zahlman Quote:
Original post by Anonymous Poster
You need to add "meh = &blah[0];" before the sprintf.
It should work perfectly without any other changes.
This is so wrong I just have to laugh.
#include <iostream>char* ReturnString();int main(){ char* pString = ReturnString(); // Get the string std::cout << "The string out of the function: " << pString << std::endl; // Output it delete pString; // Delete the allocated memory return 0;}char* ReturnString(){ char szString[ 256 ]; // Since you wanted to work with char arrays, and not pointers, we initialize an array // If working with char* instead isn't a problem, you don't need this, as well as a few other lines in the function strcpy( &szString[ 0 ], "foobar" ); // ... work with the char array // ...continue working with the char array char* pfString = new char[ 256 ]; // Create a dynamic char* that you can return strcpy( pfString, (char*)szString ); // Copy char array into it std::cout << "The string within the function: " << pfString << std::endl; // Just to make sure return pfString; // Return the char*}char blah[255];/* This is fine. Declares a constant pointer to non-constant data, of 255 bytes length, on the stack. */blah = "foobar";/* This won't work, and should not compile. You cannot assign a new value to a constant pointer. */char* meh;/* This is fine. Declare a non-constant pointer to non-constant data. Yes, there is a real difference between "const char *" and "char const *"; the first is an alterable pointer which points at constant data, and the second is a constant pointer which points at alterable data. */sprintf(meh,"%s",blah);/* Syntactically correct, but assuming the mistakes with blah have been corrected, you're still writing data to the uninitialized and likely unallocated location in memory which meh supposedly looks at. */printf("%s",meh);/* Would work fine if meh pointed at anything. As it is, it's a pointer to unallocated junk data. */Quote:
Original post by Pipo DeClown Quote:
Original post by Zahlman Quote:
Original post by Anonymous Poster
You need to add "meh = &blah[0];" before the sprintf.
It should work perfectly without any other changes.
This is so wrong I just have to laugh.
Uhm.. No?
BTW, The way you replied is so wrong that I just have to laugh.
Quote:
Original post by Zahlman
The original code has bugs, which Xai and many others pointed out, and the suggested inclusion cannot prevent those problems.
The statement "meh = &blah[0];" (which is equivalent to "meh = blah;") does not do what the OP wants - it will make meh point at the array data, when the OP clearly wants meh to point at a separate copy of that data.
"adding it before the sprintf" is just silly, as a result - why make a pointer point at an array and then sprintf the array contents to the location indicated by the pointer? The sprintf becomes a no-op. Of course, it will still end up outputting the desired text and will therefore "work perfectly" - except that it won't, because the previous assignment of the string literal to the char array is still wrong!
I made an attempt to demonstrate that I know whereof I speak. You are hereby challenged to follow suit.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more