• Advertisement
Sign in to follow this  

sharing variables

This topic is 4329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to share this with the rest of the program char password[128]; char *p; p = &password; how can i go about doing that..for one i know a class can do it but im just trying to pass the string in password to a function as an arguement help?

Share this post


Link to post
Share on other sites
Advertisement
Wouldn't p = password; work? Password is already a pointer. Using &password[0] and password should equate to the same thing, minus a bit of unnecessary indirection.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by RuneLancer
Wouldn't p = password; work? Password is already a pointer. Using &password[0] and password should equate to the same thing, minus a bit of unnecessary indirection.


password is *not* a pointer, it's an array. Arrays decay to pointers, though, so, yes, p = password; will work.

Share this post


Link to post
Share on other sites
Quote:
Original post by RuneLancer
Wouldn't p = password; work? Password is already a pointer. Using &password[0] and password should equate to the same thing, minus a bit of unnecessary indirection.

As the AP mentioned, password is not a pointer. More importantly, &password[0] is considerably more expressive. p=password looks, to me, like you're copying password. p=&password[0] looks, to anyone, like you're copying the address of the first element of password. So the extra typing is far from unnecessary. The cast is unneccessary, though.

That said, its just a matter of style. Go with whatever makes you feel most comfortable.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
More importantly, &password[0] is considerably more expressive. p=password looks, to me, like you're copying password. p=&password[0] looks, to anyone, like you're copying the address of the first element of password. So the extra typing is far from unnecessary. The cast is unneccessary, though.

That said, its just a matter of style. Go with whatever makes you feel most comfortable.


Just to add a dissenting viewpoint, if I saw p=&password[0] I'd assume that p was only concerned with the first element. That is, conceptually, p is "pointer to char" and not "pointer to first element of array of char". If p is meant to be thought of as an array, then p=&password[0] looks to me like p=&(*password) or if(b!=false).

But, it's just a matter of style, so people can rightfully disagree. The important thing is to be consistent.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement