Thread: A simple Array issue

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    UK
    Posts
    5

    A simple Array issue

    Hey there, I'm fairly new to C++ so please bear with me as I almost insult your intelligence with this simple question

    Code:
     char *nu_array[] = {"HELLO", "WORLD"};
    
       char **p = nu_array;
    
       for(int i = 0; i != 6; i++ ){
    
           cout << *(*p + i) << endl;
    I've first created an array of char pointers, and fed into it 2 string literals. I'm then assuming that the original array becomes 2 dimensional; with each element containing each literal, and down a level so to speak each character of said literal.

    Assuming that this above assumption is right, how do I go about manipulating said array. I've toyed about with the code and just managed to extract the "Hello'.
    Also, all the examples I've seen seem to require a size be stated before hand to manipulate it. Is this the only way I can do it?
    Last edited by TastySauceCode; 09-19-2011 at 05:59 PM.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    This is not C++ code, this is C. There is not a single good reason to use a char[] instead of a std::string.
    This leads me to your next question: Yes, you can find the length of a std::string by calling the member function length() or size(), as for C style arrays you have to iterate through the string until you reach NULL (as C uses NULL-terminated arrays), alternatively, there is a function in the C standard library that you can use, but really, you should just steer clear of all that and use std::string.
    For a 2 dimensional string you could use a std::vector containing two strings, thus you could avoid using raw pointers altogether.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Neo1 View Post
    This is not C++ code, this is C.
    OMG! Does C really support OOP?! ( cout for example )

    Quote Originally Posted by Neo1 View Post
    There is not a single good reason to use a char[] instead of a std::string.
    There IS one ... don't know how to use the STL like std::string or std::vector

    @TastySauceCode, the C++ way is to use a vector and a string class like this:
    Code:
    // Additional includes
    #include <vector>
    #include <string>
    
    // Vector of Strings declaration
    std::vector<std::string> strVec;
    
    // Filling the vector
    strVec.push_back(std::string("Word1"));
    strVec.push_back(std::string("Word2"));
    ... etc ... etc ...
    
    // Either ::iterator or ::const_iterator
    std::vector<std::string>::iterator i; 
    
    // Iterating and printing all vector strings
    for (i = strVec.begin(); i < strVec.end(); i++){
        std::cout << *i << endl;
    }
    
    // Virtually all STL classes free themselves upon hitting the end of their scope
    STL Tutorial : Cprogramming.com

    EDIT: Thank God ( admin ), we finally have syntax highlighting! That's it, I can die now!
    Last edited by GReaper; 09-19-2011 at 09:03 PM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    UK
    Posts
    5
    Thanks for taking the time to show me the proper C++ way of doing this :-) The real purpose originally though was just to try and get to grips with the logic behind it; I like reading other people's code examples and then attempting to break down the bits I don't quite understand. And I guess in this case the person in question has reverted to some C based stuff.

    Am I right in assuming that the array has gone 2 dimensional? and probably because string literals are themselves null terminated char arrays I'm guessing.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by TastySauceCode View Post
    Am I right in assuming that the array has gone 2 dimensional? and probably because string literals are themselves null terminated char arrays I'm guessing.
    Think in terms of objects instead of(or in addition to) arrays.

    This is a vector(which you can consider a 1d dynamic array of a specified type) of string objects, not a 2d array.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    C++ now supports uniform initialization, including initialization lists, so to extend your example:

    Code:
    // Additional includes
    #include <vector>
    #include <string>
     
    // Vector of Strings declaration
    std::vector<std::string> strVec;
    
    // declaration and initialization in one shot
    std::vector<std::string> newVec = { "Hello", "world" };
     
    // Filling the vector
    strVec.push_back(std::string("Word1"));
    strVec.push_back(std::string("Word2"));
    ... etc ... etc ...
     
    // Either ::iterator or ::const_iterator
    std::vector<std::string>::iterator i; 
     
    // Iterating and printing all vector strings
    for (i = strVec.begin(); i < strVec.end(); i++){
        std::cout << *i << endl;
    
    // iterate over the 'newVec' the new way
    for (std::string& s : newVec)
        std::cout << s << std::endl;
    
    }

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    UK
    Posts
    5
    Quote Originally Posted by manasij7479 View Post
    Think in terms of objects instead of(or in addition to) arrays.

    This is a vector(which you can consider a 1d dynamic array of a specified type) of string objects, not a 2d array.
    Ah sorry about that, I meant in the original code example. Not the uber efficient C++ variant kindly posted :-)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am just going to try and provide an example for compilers that does not support initializer lists (eg, Visual C++):

    Code:
    // Additional includes
    #include <vector>
    #include <string>
     
    // declaration and initialization in one shot
    const char* tmp[] = { "Hello", "world" };
    
    // Vector of Strings declaration
    std::vector<std::string> strVec(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));
     
    // Either ::iterator or ::const_iterator
    std::vector<std::string>::iterator i; 
     
    // Iterating and printing all vector strings
    for (i = strVec.begin(); i < strVec.end(); i++)
        std::cout << *i << endl;
    }
    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.

  9. #9
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by TastySauceCode View Post
    Am I right in assuming that the array has gone 2 dimensional? and probably because string literals are themselves null terminated char arrays I'm guessing.
    nu_array is essentially a pointer to a pointer to a char. Instead of writing char *nu_array[] you could do char **nu_array and get essentially the same effect (or char nu_array[][], although i believe you have to specify the size of the second dimension, i'm not sure, has been a while since i used raw pointers like this). In other words nu_array is a pointer to an array of chars.
    I suggest you read up on pointers, grasping what they are and how they work is a good exercise, although there are few valid usages for them in modern C++.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Elkvis View Post
    Code:
    // iterate over the 'newVec' the new way
    for (std::string& s : newVec)
        std::cout << s << std::endl;
    }
    I hesitate to use the "new way". We should wait for C++11 to become standard first...
    Devoted my life to programming...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is standard. But I take it that you mean widely supported by compilers. Currently there is only one popular compiler that support this feature (GCC).
    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.

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Elysia View Post
    But I take it that you mean widely supported by compilers.
    Yes, that's what I meant.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2nd array simple issue (IMPORTANT!)
    By Kadmany in forum C Programming
    Replies: 1
    Last Post: 04-23-2011, 06:46 AM
  2. simple programming issue!
    By ke121885 in forum C Programming
    Replies: 5
    Last Post: 09-27-2009, 06:57 PM
  3. Simple issue please help
    By te5la in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2008, 12:11 PM
  4. Simple issue with bytes
    By Nositi in forum C Programming
    Replies: 6
    Last Post: 03-25-2008, 11:06 AM
  5. Simple issue but I am STUCK
    By jedispy in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2006, 02:02 AM