char * to char[]

Started by
14 comments, last by bytecoder 19 years, 8 months ago
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]; }
Advertisement
well char * can point to anything. Why not just make it point to the array?
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.
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.
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.
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).
Perhaps you should try to fix the problem instead of going around it with more or less a hack?
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.
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
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

This topic is closed to new replies.

Advertisement