Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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.

  • #3
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    chiefbutz (11-19-2008)

  • #5
    New Coder
    Join Date
    Mar 2005
    Location
    Indiana (USA)
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •