L06 - intro to pointers and references

L06 intro to - Last time we met we were talking about Iterative structures with break and continue Rules for combining control structures to build

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

View Full Document Right Arrow Icon
Last time we met, we were talking about: • Iterative structures with break and continue • Rules for combining control structures to build programs that are well structured • Divide and conquer as a problem solving strategy • Modules in C++ and C++/CLI – functions –c lasses – methods • About functions – – function invocation (or call) – function prototype
Background image of page 1

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

View Full DocumentRight Arrow Icon
Argument Promotion • Whenever the arguments that are passed in the function call don’t match the prototype, the compiler tries to convert them – a copy of the argument is converted to a different type without a specific cast – some of the conversions (usually those that are “widening”) are generally safe – others are not and they may result in losing data – the .NET framework warns you if this might happen • You can always use a cast operator (static_cast) to do the conversion explicitly
Background image of page 2
Functions and Their Arguments - Preliminaries • There are two categories of types in C++ – value types • variables of value type contain data of that type • value types are accessed directly • they normally contain primitive type data – bool , Char (or wchar_t), Byte (or char), SByte (or signed char), Int16 (or short), UInt16 (or unsigned short), Int32 (or int or long), UInt32 (or unsigned int/long), Int64 or (int64), UInt64 (or unsigned int64), Decimal, Single (or float ), Double (or double ) – reference types • contain the address of the location in memory where the data of that type is stored • usually refer to objects • examples include String ^ and Object ^
Background image of page 3

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

View Full DocumentRight Arrow Icon
Pointers and References these are variables that contain memory addresses as their values – a pointer normally contains the address of another variable that contains a specific value a “normal” variable directly references a value and a pointer indirectly references a value Pointers in standard C++ have to be declared just like any other variable: int *nums; double *xval, *yval; – here nums is a pointer to something that has an int value and both xval and yval are pointers to things that have double value – each pointer declared must have the * prepended: double *xval, yval declares one pointer to things of type double and one ordinary double variable
Background image of page 4
More about Pointers Always, always, always initialize pointers! – can initialize to NULL, to 0, or to some address (0 and NULL are equivalent in standard C++) To assign the address of some variable to a pointer, you need some way of determining what that address is this is a unary operator that returns the memory address of its operand – if we had the following scenario: int num = 15; int *numptr then the statement numptr = # would assign the address of the variable num to the pointer variable numptr. – remember that everything lives in memory:
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course EE 361 taught by Professor Conry during the Spring '08 term at Clarkson University .

Page1 / 23

L06 intro to - Last time we met we were talking about Iterative structures with break and continue Rules for combining control structures to build

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

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