Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with pointers

gemacjrgemacjr Posts: 4Member
I can't get this to work

void main()
{
char string[20];
char *aString = string;
function (aString);
}
void function(char *name)
{
cout << "enter name";
cin >> *name;
cout << name;
}

Comments

  • gautamgautam Posts: 642Member
    Hi,

    Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;


    : I can't get this to work
    :
    : void main()
    : {
    : char string[20];
    : char *aString = string;
    : function (aString);
    : }
    : void function(char *name)
    : {
    : cout << "enter name";
    : cin >> *name;
    : cout << name;
    : }
    :

  • gemacjrgemacjr Posts: 4Member
    : Hi,
    :
    : Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;
    :
    :
    : : I can't get this to work
    : :
    : : void main()
    : : {
    : : char string[20];
    : : char *aString = string;
    : : function (aString);
    : : }
    : : void function(char *name)
    : : {
    : : cout << "enter name";
    : : cin >> *name;
    : : cout << name;
    : : }
    : :
    :
    : I thought so, I thought they were trying to trick me

    Thanks so much

  • stoberstober Posts: 9,765Member ✭✭✭
    : : Hi,
    : :
    : : Your cin >> *name <---- this is incorrect - by doing this you are actually modifying only the first character of the string. What you want is cin >> name;
    : :
    : :
    : : : I can't get this to work
    : : :
    : : : void main()
    : : : {
    : : : char string[20];
    : : : char *aString = string;
    : : : function (aString);
    : : : }
    : : : void function(char *name)
    : : : {
    : : : cout << "enter name";
    : : : cin >> *name;
    : : : cout << name;
    : : : }
    : : :
    : :
    : : I thought so, I thought they were trying to trick me
    :
    : Thanks so much
    :
    :


    you don't need pointer aString. Just pass variable [b]string[/b] like this:
    [code]
    function (string);
    [/code]

  • bilderbikkelbilderbikkel Posts: 754Member
    main() is of return type int. See www.codepedia.com/1/CppMain for references.
    bilderbikkel

  • bluj91bluj91 Posts: 133Member
    : main() is of return type int. See www.codepedia.com/1/CppMain for references.
    : bilderbikkel
    :
    :

    In C++, try using the class std::string and the function getline().
Sign In or Register to comment.