help with pointers

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

  • 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;
    : }
    :

  • : 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

  • : : 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]

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

  • : 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.

Howdy, Stranger!

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

Categories