Sign in to follow this  
Dead_Zone

sharing variables

Recommended Posts

Dead_Zone    100
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
RuneLancer    253
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   
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
Conner McCloud    1135
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
Way Walker    745
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this