array of character type

This is a discussion on array of character type within the C++ Programming forums, part of the General Programming Boards category; Hi Please don't mind my asking the following question(s). I was under the impression that the character arrays work as ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    array of character type

    Hi

    Please don't mind my asking the following question(s).

    I was under the impression that the character arrays work as regular arrays such as of type int, float etc. I was thinking that one can put more than one digit (e.g. 12, 13, etc.) or more than one letter (such as ab, bb, ccc, etc.).

    Please have a look on this ASCII table.

    The character values given for decimal values from "0" to "32" consist of more than one character. For example, for decimal value "0" the character value given is "NUL", and for decimal value "9" we have character value "TAB".

    I was wrong. An array element of character type can contain only one digit or character. The character values which consist of more than character such as "NUL" or "TAB" stand for particular function or signs. For example, "TAB" is a set of white spaces, and "Space" (decimal value is 32) is single white space.

    Please correct me if I'm wrong without confusing me more!

    Code:
    // learning_character_strings.cpp
    // learning how character string work
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
            int i;
            const int C = 5;
    
            char string[C];
    
            for (i=0; i<C; i++)
            {
                    cout << "enter string #" << (i+1) << ": ";
                    cin >> string[i];
            }
    
            cout << "\n\n";
    
            for (i=0; i<C; i++)
            {
    
                    cout << "string #" << (i+1) << " is: " << string[i] << endl;
            }
    
            system("pause");
            return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "TAB" is a string of three characters (plus implicit \0 at the end). '\t' is a tab character. It represents one single character, the character you get by hitting the big button that says Tab on the left side of your keyboard. It is not the three characters T, A, B. Same with all the control characters.

    ETA: And going back to the beginning: you can only put one "thing" in each slot of an array. An array of ints can hold several ints, but each slot just holds one int. The number of digits is irrelevant (other than the fact that int is only "so big").
    Last edited by tabstop; 06-10-2011 at 09:40 AM.

  3. #3
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    You didn't ask a question, but i think i understood what you need anyways. Basically you need an array that holds in each position a multi-character. If yes then include the string library and declare the array as it follows:
    Code:
    #include <string.h>
    string array[n];
    Hope this helps
    "A Computer in every desk and in every home, running Microsoft software."

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, tabstop, godly.

    On the ASCII table decimal 7 stands for ASCII "BEL" which I think produces the sound of a bell. In the following code I have tried to use "BEL" but it doesn't work. I need to hear the sound of a bell! Please help me. Thank you.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
            int i;
            const int C = 4;
    
            char string[C];
    
            for (i=0; i<C; i++)
            {
                    cout << "enter string #" << (i+1) << ": ";
                    cin >> string[i];
            }
    
            cout << "\n\n";
    
            for (i=0; i<C; i++)
            {
    
                    cout << "string #" << (i+1) << " is: " << string[i] << endl;
            }
    
            cout << static_cast<char>(int 7) << endl;
    
            system("pause");
            return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    It should be static_cast<char>(7), but you might as well write '\a'.
    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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Jackson, your code is dangerous since you are using a standard library class name (string) and using "using namespace std". If you include <string> now, you will get a nameclash.
    Therefore, avoid using "string" as a name or stop using "using namespace std" I would suggest.
    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.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, laserlight, Elysia.

    @ Elysia: Now I realize this; I should have used some other identifier name for that array.

    These are some of the escape sequences: \n, \t, \a, \f.

    I see they are called characters. Please have a look here:
    https://docs.google.com/viewer?a=v&p...thkey=CKTU4KwC

    But I don't get it. For example, "\n" is not a single character - these are two characters "\" and "n". And you have to use quotation marks around these so-called characters instead of single quotes which are used with normal characters such as 'a', 'c'.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    But I don't get it. For example, "\n" is not a single character - these are two characters "\" and "n". And you have to use quotation marks around these so-called characters instead of single quotes which are used with normal characters such as 'a', 'c'.
    You use the '\n' with single quotes. The backslash character '\' denotes an escape sequence and means that the next character is a special non-printing character or to denote that the character is a single or double quote or backslash.

    Jim
    Last edited by jimblumberg; 06-10-2011 at 05:05 PM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    '\n' is a single character. There is no need to use double quotes around them.
    '\n' is a special character that represents a newline. It couldn't be represented with a single character since there's no easy button on your keyboard to use to represent that. Obviously all keys on your keyboard are reserved for other characters.
    So a special exception was made: If you put \ and then another character, it would be a "single character" as seen by the language.
    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.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Yes, Using '\n' does work the same way as "\n". But I have almost always seen the "\n" being used with double quotes.

    Code:
    // character backslash n.cpp
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
    
            cout << "hello world" << '\n';
    
            cout << "hello world" << "\n";
    
            cout << "hello world\n";
    
    
            system("pause");
            return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    In your example it works the same, as all you are probably examining is the output.

    The sequence of actual operator functions being called to output the '\n' character and the "\n" string are different between your first and second lines (different overloads). You will not detect that without the aid of a debugger, but it is a real difference.

    The fact you don't detect a difference doesn't mean there isn't one. In your case, it reflects you using a technique that cannot directly detect the differences.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. array type has incomplete element type
    By philgrek in forum C Programming
    Replies: 29
    Last Post: 05-06-2011, 02:42 PM
  3. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM
  4. differentiate the character data type
    By cplusplus.Jr in forum Windows Programming
    Replies: 5
    Last Post: 04-05-2007, 01:41 PM
  5. Character type conversion error
    By xmltorrent in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 11:45 AM

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