Char array and char *

This is a discussion on Char array and char * within the C Programming forums, part of the General Programming Boards category; If I have an array of char and a char pointer, how can I make the compatible ? Code: void ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Char array and char *

    If I have an array of char and a char pointer, how can I make the compatible ?

    Code:
    void something(char *mstring)
    {
    	char list[i] = ... // "Hello World"
                    mstring = list;  ?????  // Now I want mstring to return "Hello world"
    }
    Best wishes, Desmond

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,644
    It sounds like you want to copy from one string to another:
    Code:
    void something(char *mstring)
    {
        char list[] = "Hello World";
        strcpy(mstring, list);
    }
    or perhaps a little safer:
    Code:
    void something(char *mstring, size_t len)
    {
        char list[] = "Hello World";
        strncpy(mstring, list, len);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    How are you calling your function? It is unclear form your example what you want the function to do.

    If you want to modify a char pointer in the calling routine, then you need to pass a char **. However, in that case you must either make list a const char pointer to a literal string, or static.

    If you want to copy the string created in list to a buffer passed to you function, then you can use the strcpy() function found in <string.h>. However, you must first make sure the buffer is big enough.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    or perhaps a little safer:
    Code:
    void something(char *mstring, size_t len)
    {
        char list[] = "Hello World";
        strncpy(mstring, list, len);
    }
    Remember that strncpy() doesn't add a terminating NULL if there are at least len characters in the string . . . in other words, this is closer to being an equivalent of the first something():
    Code:
    void something(char *mstring, size_t len)
    {
        char list[] = "Hello World";
        strncpy(mstring, list, len);
        mstring[len] = 0;
    }
    Also note that list could be of type const char * and it would still work. It would probably use less memory, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    mstring[len] = 0;
    isn't it out of bounds access?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by vart View Post
    isn't it out of bounds access?
    Depends whether len is the number of characters in the string before the null terminator(like what you would expect strlen to return) or the amount of memory allocated for mstring.
    Don't quote me on that... ...seriously

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Brad0407 View Post
    Depends whether len is the number of characters in the string before the null terminator(like what you would expect strlen to return) or the amount of memory allocated for mstring.
    I know it, but in most cases when passing the buffer with len, I do it as

    Code:
    somefunc(buffer, sizeof buffer);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Generally, I think of string lengths in terms of strlen(). You know . . . buffer[strlen(buffer)] . . . malloc(strlen(buffer) + 1) . . . .

    On the other hand there is fgets(), of course. But I have a convincing argument for my point of view!
    Code:
        strncpy(mstring, list, len);
        mstring[len] = 0;
    strncpy() takes the maximum number of characters to copy -- in other words, a strlen()-like value. Thus, strncpy() could fill in mstring[0] ... mstring[len-1]. In order to avoid overwriting any of those characters, mstring[len] is the next logical index to use.

    It's just an idiom -- you know:
    Code:
    strncpy(a, b, n);
    a[n] = 0;
    It's reasonably common . . . it's even in the example in the link I posted earlier in this post.

    But it doesn't matter, it's just an example. That's why it's called something().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21