Unable to store my entire string in an array trough a pointer. (C++)

This is a discussion on Unable to store my entire string in an array trough a pointer. (C++) within the C++ Programming forums, part of the General Programming Boards category; Hi! I have recently started to learn C++ trough the tutorials on cprogramming.com and so far i find it really ...

  1. #1
    mr2
    mr2 is offline
    Registered User
    Join Date
    May 2009
    Location
    Kiruna, Sweden.
    Posts
    2

    Unable to store my entire string in an array trough a pointer. (C++)

    Hi!

    I have recently started to learn C++ trough the tutorials on cprogramming.com and so far i find it really fun!
    However, I am having some problems creating strings(stored in arrays) trough pointers.
    Here is my code:

    Code:
    char *arry;
    arry = new char[256];
    
    cout<<"Please enter a string.\n";
    cin.getline(arry,256,'\n');
    cout<<"\nYour string has been saved, press enter to read it.\n";
    cin.get();
    cout<<*arry<<endl;
    delete [] arry;
    cin.get();
    Now when i write a sentence, for example "Hello World" it will only print out the first character ("H").
    If i just declare an array and put the string in that array it will work just fine, so I am guessing that I am doing something wrong with the pointer.

    Any help would be greatly appreciated!
    //Mattias

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since you asked to print a character, that's what you got. (arry is a pointer-to-char, so *arry is consequently a character.) If you want to print a string, you should probably print a string.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    i.e. *arry is a char
    arry is a char* string.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    mr2
    mr2 is offline
    Registered User
    Join Date
    May 2009
    Location
    Kiruna, Sweden.
    Posts
    2
    Wow, I didn't expect to get an answer this fast!
    It's really hard to know when to use "*" but I think that i get it now.
    If i ask for *arry i will just get one single character since it is "char" datatype. But if i ask for arry i will get the whole array stored on that location.

    Thank you very much for helping me out! :)

    //Mattias

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    It's rather simple, actually. Apply "*" left to a variable and you dereference it, thereby removing an "*" from the type. Ie char* -> char.
    In C, a string is considered const char*/char*, and a character a char. To store many characters, an array is used, which decays into a pointer when used, so that's how it is.
    But I urge you to use std::string instead, unless it's for practice.
    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. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. store function in string array? possible?
    By sappy in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2005, 01:30 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

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