C++_functions - Background: What = and & Mean In C+ the...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ functions In C++ the = symbol means either initialization or assignment If it’s used with a type declaration , it means initialization If it’s used without a type declaration, it means assignment int j(7); // j is initialized with value 7 int k = 4; // k is initialized with value 4 j = 3; // j is assigned value 3 In C++ the & symbol also has a similar “dual nature” If it’s used inside a type declaration, it means a reference (an alias) Arguments to function are always declared along with their types If it’s used outside a type declaration, it means “address of” int swap (int & i, int & j); // references to int int & s = j; // reference s initialized to refer to j int * p = & j; // pointer p initialized w/ j’s address
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ functions Background: Parameter/Variable Declarations Hint: read parameter and variable declarations right to left int i; i is an integer” int & r = i; r is a reference to an integer (initialized with i)” int * p; p is a pointer to an integer” int * & q = p; q is a reference to a pointer to an integer (initialized with p)” Read function declarations inside out “function main takes an integer and an array of pointers to char, and returns an integer” int main (int argc, char * argv[]); “function usage takes a pointer to char, and returns void (nothing)” void usage (char * program_name); “function setstring takes a reference to a (C++) string, and returns void” void setstring (string & s);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

C++_functions - Background: What = and & Mean In C+ the...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online