Sign in to follow this  

char * to char[]

This topic is 4868 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

Anyone know how to store a char * "string" into a char[] without using any additional headers (as in no string.h or CString.h)? Here's the code I have so far: char * myvar; char String[1024]; cin >> myvar; for (int a = 1;a < sizeof(myvar); a++) { String[a] = myvar[a]; }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well char * can point to anything. Why not just make it point to the array?

Share this post


Link to post
Share on other sites
Why are you trying to avoid extra headers? You're just going to end up reinventing the wheel.

sizeof(myvar) is going to be 4, cuz it's the size of the pointer.

char* ptr = myvar;
int a = 0;
while (*ptr != 0) {
String[a++] = *ptr++;
}
String[a] = 0;

But better to just strcpy it.

Share this post


Link to post
Share on other sites
strcpy(String, myvar);

or strncpy

make sure you check the docs about the limitations of these 2 functions, specifically their behavior as far as their treatment of null terminators.

edit: oops didn't see the part about wanting to avoid using extra headers.

Share this post


Link to post
Share on other sites
Quote:
Original post by aWG
Why are you trying to avoid extra headers? You're just going to end up reinventing the wheel.

sizeof(myvar) is going to be 4, cuz it's the size of the pointer.

char* ptr = myvar;
int a = 0;
while (*ptr != 0) {
String[a++] = *ptr++;
}
String[a] = 0;

But better to just strcpy it.


Thanks! It works now. I didn't want to strcpy it because I was receiving linker errors when declaring a string. It said string was an undeclared identifier (using VS 6).

Share this post


Link to post
Share on other sites
Quote:
Original post by DrEvil
Perhaps you should try to fix the problem instead of going around it with more or less a hack?


Yeah, I agree. I think you're probably defining strings incorrectly. CString.h is used for MFC only. string.h should work correctly but remember that string.h is a C library, so there's no string objects, just char* and char[] like you're trying to use. Maybe you were trying to use the std::string object without including the proper header? Try the same code again by:

#include <string.h>

strcpy(String, myvar);

It will make sure your code is a lot cleaner and you won't have to reinvent every C library you want to use in your code.

Share this post


Link to post
Share on other sites
No, the string.h functions work fine. It's just that I can't declare an actual string.

#include <string.h>

std::string abc;

It gives me the following errors:

error C2653: 'std' : is not a class or namespace name
error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'abc'
error C2065: 'abc' : undeclared identifier

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by EIShadowDragon
No, the string.h functions work fine. It's just that I can't declare an actual string.

#include

std::string abc;

It gives me the following errors:

error C2653: 'std' : is not a class or namespace name
error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'abc'
error C2065: 'abc' : undeclared identifier



try this

#include

std:string abc;

string.h is the c header file, string is the c++ header

Share this post


Link to post
Share on other sites
As aWG said, <string.h> is only a C library thing. To get the C++ std::string stuff, you need to include <string> (no ".h").

[edit: wow, slow indeed.]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I screwed up forgot about html formatting :)

should have been
#include <string>

Share this post


Link to post
Share on other sites

This topic is 4868 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this