Jump to content
  • Advertisement
Sign in to follow this  
EIShadowDragon

char * to char[]

This topic is 5083 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
Advertisement
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
Quote:
Original post by Anonymous Poster
well char * can point to anything. Why not just make it point to the array?


Other way around. I'm trying to copy the char * INTO the char[]. Not sure how to do it.

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
Perhaps you should try to fix the problem instead of going around it with more or less a hack?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!