How do array of char*'s?

This is a discussion on How do array of char*'s? within the C++ Programming forums, part of the General Programming Boards category; How would I do an array of char*'s? i know that char* is an array of char's to make a ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How do array of char*'s?

    How would I do an array of char*'s? i know that char* is an array of char's to make a string but what if I had an array of those? how would I write that?

    Would it be:

    char* [][]

    of

    char* []

    or something else?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Read it backwards.

    Code:
    char *[]
    It's an array of pointers to char. So yes, char *[] will be an array of char *'s.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Easiest would be a vector of strings:
    Code:
    #include <string>
    #include <vector>
    .
    .
    std::vector<std::string> myStrings;
    Or if you must use arrays:
    Code:
    char myStrings[num_strings][length];

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    C-style strings are not recommended for C++, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Some APIs require C-style strings, so therefore it's good to know how to use them in general. For regular usage, std::string should be used instead.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Hence "not recommended" but not "not necessary"
    However, to some point I may agree, it usually only requires extracting buffers from std::string or CString and then also putting them back. It's not necessary to manipulate C-style strings outside string classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If an API needs a modifiable buffer, you need to create a dynamic C-String, copy the text over, send the buffer to the function, copy it back to a C++ string, and then free the memory. Practice doesn't hurt.

    Nevertheless, as I said, std::string should be used in most circumstances.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by MacGyver View Post
    If an API needs a modifiable buffer, you need to create a dynamic C-String, copy the text over, send the buffer to the function, copy it back to a C++ string, and then free the memory. Practice doesn't hurt.

    Nevertheless, as I said, std::string should be used in most circumstances.
    of course you can use vector of char's here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    A note
    Code:
     char **
    is also an array of strings equivalent to your
    Code:
    char* []
    except we're talking C style notations here.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by WDT View Post
    A note
    Code:
     char **
    is also an array of strings equivalent to your
    Code:
    char* []
    except we're talking C style notations here.
    Actually, it's char ** and char *[] is not quite equivalent. They may be used in a similar way SOMETIMES, e.g. if you want to allocate an array of pointers, you can return that as either type. But you can't make a char ** into a char *[] directly - you can the other way around.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 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. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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