help with pointers - Programmers Heaven

Howdy, Stranger!

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

Categories

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.