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

  1. #1
    Registered User
    Join Date
    May 2009
    Kiruna, Sweden.

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


    I have recently started to learn C++ trough the tutorials on 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:

    char *arry;
    arry = new char[256];
    cout<<"Please enter a string.\n";
    cout<<"\nYour string has been saved, press enter to read it.\n";
    delete [] arry;
    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!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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
    Toronto, ON
    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
    Registered User
    Join Date
    May 2009
    Kiruna, Sweden.
    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! :)


  5. #5
    Join Date
    Oct 2007
    Inside my computer
    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, 02: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, 06: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, 12:30 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM