Understanding strings and arrays. Specific questions included.

This is a discussion on Understanding strings and arrays. Specific questions included. within the C++ Programming forums, part of the General Programming Boards category; Hi, this is a beginners question, I apologize. I would greatly appreciate your help in my studies. Thank you! To ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Understanding strings and arrays. Specific questions included.

    Hi,

    this is a beginners question, I apologize. I would greatly appreciate your help in my studies. Thank you!

    To help illustrate my misunderstanding, I created an example. After it, I would like to ask simple questions.

    Code:
    #include <string>
    #include <iostream>
    
    using std::endl;
    using std::cin;
    using std::cout;
    
    int main() {
    
      cout << "Enter a string with at least two charactes: " << endl;
    
      std::string s;
      cin >> s;
    
      cout << "String pointer: " << &s << endl;
    
      // does not compile
      // const char* ps = s;
      // cout << "String pointer: " << ps << endl;
    
      const char* p = &s[0];
      cout << "First char pointer: " << p << endl;
      cout << "First char: " << *p << endl;
    
      p++;
      cout << "Second char pointer: " << p << endl;
      cout << "Second char: " << *p << endl;
    
      return 0;
    }
    Questions:

    1-
    Question about the following statements:
    Code:
      //  const char* ps = s;
      //  cout << "String pointer: " << ps << endl;
    If strings are char arrays, and using the name of an array as a value will return a pointer to the first element of an array, why is the first statement above wrong?


    2-
    Question about the following statements:
    Imagine that when I run the code, I input string "asdf"

    Code:
      cout << "First char pointer: " << p << endl;
    I was expecting p to have an address, not only that, but I expected that the address would be the same as &s, instead this statement outputs:
    $ First char pointer: asdf

    But then to my suprise, the next statement actually works:
    Code:
      cout << "First char: " << *p << endl;
    Another statement that I don't understand is:
    Code:
    cout << "Second char pointer: "<< p << endl;
    Again, I expected an address, but I got this output:
    $ Second char pointer: sdf

    Nevertheless, after p being incremented, the following statement produces expected results:
    Code:
    cout << "Second char: " << *p << endl;

    I deeply appreciate the help. Sorry for being so verbose, I wanted to make sure I asked specific questions.

    Many Thanks!!!

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    1. "If strings are char arrays, and using the name of an array as a value will return a pointer to the first element of an array, why is the first statement above wrong?"

    This is a matter of semantics. There are two uses of the word 'string' in common use. The first use is the C-style string (also the only type of 'string' in the pre-1998 versions of C++), which indeed is just a character array. That would be something like:

    Code:
    const char * str = "This is a C-style string";
    The second meaning of the word 'string' is an object of the std::string class. This is not at all the same as the first type of string; it's definitely the preferred way to handle strings in C++, but to get a C-style string out of it, you need to use its .c_str() method:

    Code:
    std::string str2 = "This is a C++-style std::string, which can be converted to a C-style string";
    const char * str = str.c_str();
    Unfortunately for historical reasons, both exist and both are called "strings". You need to pay attention to context to know which is being discussed.

    2. The reason for this is that cout is smart enough to know how a C-style string works, and it has special treatment for a char * or const char * (it will dereference the pointer and print all characters until it reaches a \0.

    You could see the address if you did this:

    Code:
    cout << "First char pointer: " << (const void *) p << endl;
    (If you're not familiar with void *, which are much less common in C++ than C, it's basically an address to "something" - the compiler knows it's a memory address but doesn't assume anything about what data is stored at that address.)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Thank you so much, Cat. Thanks for taking the time to be so clear! I appreciate it.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    62
    minor adjustment just to prevent confusions:

    std::string str2 = "This is a C++-style std::string, which can be converted to a C-style string";
    const char * str = str2.c_str();

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Quote Originally Posted by Cat View Post
    2. The reason for this is that cout is smart enough to know how a C-style string works, and it has special treatment for a char * or const char * (it will dereference the pointer and print all characters until it reaches a \0.
    I wouldn't say that it's "smart" in this case.
    There is unfortunately a legacy historical reason behind this.
    In C, strings are char arrays. Because of this, pointers to such arrays were very common (and the fact that arrays decayed to pointers when passed to functions), so pointers to char were typically interpreted as a C-style string and not some pointer to a single char, which it might very well be.
    Unfortunately C++'s streams emulated this dangerous behaviour which is why you see it output a string instead of an address for pointers to char.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbi Questions, A Deeper understanding of C
    By robrob in forum C Programming
    Replies: 1
    Last Post: 04-15-2012, 06:52 PM
  2. New to c++ some specific questions, gui, db, xml
    By bpr in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2010, 12:48 PM
  3. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM
  4. How to make some specific strings
    By hash-mobtadi in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2008, 12:33 PM
  5. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM

Tags for this Thread


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