Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-19-2008, 02:38 AM   PM User | #1
chiefbutz
New Coder

 
Join Date: Mar 2005
Location: Indiana (USA)
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
chiefbutz is an unknown quantity at this point
C++ Type Conversion

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.
chiefbutz is offline   Reply With Quote
Old 11-19-2008, 04:02 AM   PM User | #2
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
Code:
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.

Last edited by ralph l mayo; 11-19-2008 at 04:11 AM..
ralph l mayo is offline   Reply With Quote
Old 11-19-2008, 06:30 AM   PM User | #3
chiefbutz
New Coder

 
Join Date: Mar 2005
Location: Indiana (USA)
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
chiefbutz is an unknown quantity at this point
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.
chiefbutz is offline   Reply With Quote
Old 11-19-2008, 10:09 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by chiefbutz View Post
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:
Code:
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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
chiefbutz (11-19-2008)
Old 11-19-2008, 08:38 PM   PM User | #5
chiefbutz
New Coder

 
Join Date: Mar 2005
Location: Indiana (USA)
Posts: 50
Thanks: 4
Thanked 0 Times in 0 Posts
chiefbutz is an unknown quantity at this point
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.
chiefbutz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:04 AM.


Advertisement
Log in to turn off these ads.