sharing variables

Started by
4 comments, last by Way Walker 18 years, 1 month ago
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?
Advertisement
p = (char *)(&password[0]);
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
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.
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
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.

This topic is closed to new replies.

Advertisement