...

View Full Version : C++ Type Conversion



chiefbutz
11-19-2008, 03:38 AM
I need to convert from type "char (*)[256]" to "char*" because I have a function that requires it. I have tried changing the function, I have tried all kinds of things, but none have worked. I have tried casting too. Anyone have any suggestions. What I am trying to do is copy a string onto a fixed size character array, and I have having more trouble that I should. Any help would be appreciated.

ralph l mayo
11-19-2008, 05:02 AM
void some_func(char* ch);
// ...
char* cha[256];
// ...
some_func(*cha); // equivalent to some_func(cha[0]);


edit, NB: string in C++ means std::string, you're talking about C strings here. Unless this is an assignment or you have some very specific need (ie, to actually treat some data as an array of characters rather than as a contiguous string) you probably don't want to use C strings.

Moreover, you should realize that you can't convert char* [256] to char* at all because they're not at all the same: the former is an array of the latter. You can either select one of the C strings with bracket notation or dereference it to get the first one, per above.

chiefbutz
11-19-2008, 07:30 AM
I am doing an assignment for my unix class and we have having to do stuff with semaphores and shared memory and all of those things run on C-Strings. I guess I need to see if there is another way to do this stuff then.

oesxyl
11-19-2008, 11:09 AM
I am doing an assignment for my unix class and we have having to do stuff with semaphores and shared memory and all of those things run on C-Strings. I guess I need to see if there is another way to do this stuff then.
strings are \0 terminated so this is usual:


void myfunc(char * mystring){
char * localstring = mystring;
while(*localstring){ // at end of the string is \0, => false
// do something with *localstring which is a char
// next position
localstring++;
}
}


regards

chiefbutz
11-19-2008, 09:38 PM
Thanks, I figured out something that I should have thought of earlier. I am using a for loop to copy each character over one by one. It isn't efficient, but it works.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum