COMP2006 Lecture 2 Data Types and Pointers
COMP2006 Lecture 2 Data Types and Pointers
C++ Programming
Lecture 2
Dr Chao Chen
1
Development Environment Comments
• You can use any development environment you want
• You can use Linux or Mac if you prefer
– As long as you are happy to install and support the environment
yourself – e.g. something you are familiar with?
– For the coursework I’ll give you some Linux and Mac information
which should get you started
– I use a library which has Linux and Mac support too so the code
should ‘just compile’ (unless something changed from last year)
• Windows virtual desktop should work for you and avoid
installing Visual Studio yourself
• You can install visual studio yourself
https://azureforeducation.microsoft.com/devtools
2
This lecture
• Data types
– C/C++ similarities and differences
• Pointers
– Address of
– Dereferencing
• Arrays
• C-type Strings
3
Data types
4
bool type (C++ only, not C)
• bool : true/false
• Similar to java’s boolean type
• Boolean expressions (e.g. many conditions)
have results of type ‘bool’ in C++
– But type int in C – a subtle difference
• IMPORTANT: bool and int can be converted
implicitly / automatically to each other
– i.e. C++ is backward compatible with C
– true defined to be 1 when converted to int
– false defined to be 0 when converted to int
– 0 is defined to be false
– non-zero is defined to be true 5
ints and bools
• In both C and C++ any integer types can be used in
conditions (i.e. char, short, long, int)
– In C++ the value is silently converted to a C++ bool type
• When using integer types:
– true is equivalent to non-zero (or 1), false is equivalent to zero
• Example:
int x = 6;
while ( x )
{
printf( "X is %d\n", x );
x -= 2;
}
• In Java this would be an error : “x not boolean”
• In C/C++ this is valid ( it means ‘while( x != 0 )’ ) 6
Sizes of types…
• The size of types (in bits/bytes) can vary in C/C++
– For different compilers/operating systems
– In Java, sizes are standardised, across O/Ss
• Some guarantees are given:
– A minimum size (bits): char 8, short 16, long 32
– Relative sizes: char ≤ short ≤ int ≤ long
• An int changes size more than other types!
– Used for speed (not portability), but VERY popular! (fast)
– Uses the most efficient size for the platform
– 16 bit operating systems usually use 16 bit int
– 32 bit operating systems usually use 32 bit int
– 64 bit operating systems usually use 64 bit int
• sizeof() operator exists to tell us the size (in chars)
7
Basic Data Types - Summary
Type Minimum Minimum range of values
size (bits) (Depends upon the size on your platform)
char 8 -128 to 127 (or 0-255 : implementation defined)
(WARNING: Java ‘byte’. Java char is 16 bit!)
short 16 -32768 to 32767
long 32 -2147483648 to 2147483647
8
signed/unsigned values
• Signed/unsigned variants of integer types
– Unlike in Java where they are fixed
– Examples:
signed char sc; unsigned short us;
signed long sl; unsigned int ui;
– Default is signed (except for char)
• If neither ‘signed’ nor ‘unsigned’ is stated
• Default for char is compiler-dependent
– Note: Using ‘signed char’ or ‘unsigned char’
removes the ambiguity of the range for char
9
wchar_t type
• wchar_t : wide character
– Like a Java ‘char’
• ASCII limited to values 0 to 127 (7 bits)
– Not enough characters for some languages
• wchar_t is designed to be big enough to
hold a character of the : “largest character
set supported by the implementation’s
locale”
(Bjarne Stroustrup, The C++ Programming Language)
• Use L'a' or L"hello" for wide character/string literals
10
The void type
15
Variables
#include <cstdio>
Conceptually:
Value of variable
‘pointer’ is the
pointer variable
address of variable
<&variable> 345639 ‘variable’
20
Pointer example
short s = 965;
short* ps1 = &s;
short* ps2 = ps1;
• Q: What goes into the red circled parts?
21
Pointer example
short s = 965;
short* ps1 = &s;
short* ps2 = ps1;
• Q: What goes into the red circled parts?
22
Pointer example
short s = 965;
short* ps1 = &s;
short* ps2 = ps1;
• So, assigning one pointer to another means:
– It points at the same object
– It has the same address stored in it (i.e. the same value)
30
Parameters can be pointers
First parameter Second parameter
is an int is a pointer
35