Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; Just wanted to know why we use asterisk with return in the following code,,whats the logic of it. Code: void ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Help

    Just wanted to know why we use asterisk with return in the following code,,whats the logic of it.
    Code:
    void setname()
    {
    cin>>name;
    }
    
    char getname()
    {
    return *name;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Presumably name is a char array? In that context, name by itself would then be taken to represent the address of the first character of array (a char pointer). In order to get a specific character (the first character) at an address you must dereference that address/pointer by using the * operator. It seems likely that getname would actually only return the first character of the name entered by the setname function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    yeah name is a char array,, it dint only return the firsl character,,it returns the whole string,,

Popular pages Recent additions subscribe to a feed

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