Chapter 2
Chapter 2
Programming Concepts
Chapter contents
• Arrays
• Pointers
• Structures
2
Arrays
• An array is a data structure which stores a fixed-size sequential
collection of elements of the same type.
3
Cont...
• You can initialize C++ array elements either one by one or using a
single statement as follows:
type name[size1][size2]...[sizeN];
type arrayName [ x ][ y ];
5
Cont...
int a[3][4] = {
{0, 1, 2, 3} , /* initializes row indexed by 0 */
{4, 5, 6, 7} , /* initializes row indexed by 1 */
{8, 9, 10, 11} /* initializes row indexed by 2 */
};
7
Cont...
• Here, type is the pointer's base type; it must be a valid C++ type
and var-name is the name of the pointer variable.
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
• The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address.
• The only difference between pointers of different data types is the
data type of the variable or constant that the pointer points to.
• Practice problem 2.4
8
Pointers in C++
• Pointers have many but easy concepts and they are very
important to C++ programming.
9
Cont...
10
Null Pointers
11
Cont...
• However, the memory address 0 has special significance; it signals
that the pointer is not intended to point to an accessible memory
location.
• Thus, if all unused pointers are given the null value and you avoid
the use of a null pointer, you can avoid the accidental misuse of an
uninitialized pointer.
12
Pointer Arithmetic
• As you understood pointer is an address which is a numeric value;
therefore, you can perform arithmetic operations on a pointer just
as you can a numeric value.
• There are four arithmetic operators that can be used on pointers:
++, --, +, and -.
• To understand pointer arithmetic, let us consider that ptr is an
integer pointer which points to the address 1000.
• Assuming 32-bit integers, let us perform the following arithmetic
operation on the pointer: ptr++ the ptr will point to the location
1004 because each time ptr is incremented, it will point to the
next integer.
13
Cont...
• This operation will move the pointer to next memory location
without impacting actual value at the memory location.
14
Cont...
• Pointers may be compared by using relational operators, such as
==, <, and >.
• If p1 and p2 point to variables that are related to each other, such
as elements of the same array, then p1 and p2 can be
meaningfully compared.
• Practice problem 2.8.
15
Cont...
• Practice problem 2.9.
16
Array of Pointers
int *ptr[MAX];
• This declares ptr as an array of MAX integer pointers. Thus, each
element in ptr, now holds a pointer to an int value.
17
Cont...
• You can also use an array of pointers to character to store a list of
strings.
• Practice problem 2.12.
18
Structures
• Structure is a user defined data type which allows to combine
data items of different kinds.
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
20
Cont...
• You can define pointers to structures in very similar way as you
define pointer to any other variable as follows:
struct_pointer = &Book1;
• To access the members of a structure using a pointer to that
structure, you must use the -> operator as follows:
struct_pointer->title;
21
Cont...
• Practice problem 2.19.
22
Cont...
• Now, you can use Books directly to define variables of Books type
without using struct keyword. Following is the example:
23
Thank You!!!
24