Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jack of Null Pointer

N00b Question

This topic is 5893 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

Sometimes people use &''s in there code, which seem to do something, but I don''t know what. Is this some kind of varible? I always thought & was AND and it also is Address Of. What does it do within a string array? Am I just crazy! =========== Jack!!!!!!

Share this post


Link to post
Share on other sites
Advertisement
The & operator has different functions/meaning. It depends on the context that it''s used in.

int i;
&i = in this case it represents a pointer to i

if(i&1) = in this case it represents a bitwise-AND

if((i==1)&&(j==1)) = in this case it represents a logical-AND

In a string array it is the same as case 1, it represents a pointer to the array.


...and, no, you''re not crazy. The compiler is smart enough to know what you want it to do based on the context that it''s used in.

Share this post


Link to post
Share on other sites
If you mean something like

void foo(const string& str);

then & causes the str parameter be passed by reference, as opposed to by value.

Share this post


Link to post
Share on other sites
That''s C stuff. It''s used to display text in console applications.

For instance:

int var = 2351;

printf("%i people suffer from voice immodulation.", var);

... would display "2351 people suffer from voice immodulation."

The ''%i'' represents something that should be replaced as an integer value (conveniently provided by the variable ''var'').

You can add multiple arguments, like so:

printf("%i bottles of %s on the wall, %i bottles of %s (contd)", 100, "beer", 100, "beer");

The integer 100 and the string beer can be variables or actual numbers and strings, respectively. The %i is replaced by 100, and the %s is replaced by beer. If you were to switch the order of the 100 and the beer around at the end of the printf statement, you''d get problems because "beer" would be read as an integer and 100 as a character string.

%c is the character format specifier
%i is the integer format specifier
%f is for floats
%s is for strings

... and so on and so forth.

Hope that helps!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
isn''t it %d for integers?

Share this post


Link to post
Share on other sites
I believe that %i and %d are equivalent. Both mean signed integer. On the other hand, formatting codes like these are C-style (as well as printf). If you use C++ and iostreams (cout <<), then you don''t need them.
/Ksero

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!