C++ ARRAYS AND POINTERS

C++ ARRAYS AND POINTERS

Problem Solving with Computers-I

C++ Arrays

? List of elements ? All elements have the same data type ? The elements are located adjacent to each other in memory ? Like all variables in C++, you must declare an array before using it

0 8000

Scores

tI

is the address of element O

cont Cc scores

0 8000 is also

11 0

called

8000

the

base

address

Accessing elements of an array

int scores[]={20,10,50}; // declare and initialize

? scores is the starting memory location of the array ? also called the base address ? Base address (scores)cannot be modified

? Access array elements using their index ? Indices start at 0

? scores[0]: 20 ? scores[1]: 10 ? scores[2]: 50 ? scores[3]: out of bound array access, undefined behavior

Iterating through an array

20 10 50

I

scores 0

1

2

other data in memory

int scores[]={20,10,50}; // declare an initialize

To iterate use:

* regular for loops

* Or range based for loop (C++ 11 feature)

Modifying the array

What is the output of this code?

int scores[]={20,10,50}; scores = scores + 10; for(int i=0; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download