Lecture%2005%20-%20%20Arrays%20and%20Pointers

Lecture%2005%20-%20%20Arrays%20and%20Pointers - Lecture 05...

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

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 05 C Arrays & pointers In this lecture Introduction to 1D arrays Array representation, access and updates Passing arrays to functions Array as a const pointer Dynamic arrays and resizing Introduction to 2D arrays 2D Array Representation Arrays and pointers Starting to think like a C programmer Further readings Exercises Introduction to 1D Array Unlike Java, C arrays are NOT objects. They do not have any inherited properties like length or do not contain methods like contains. C arrays are made up of primitives. A C array can be viewed as a contiguous memory block. The size of the block depends on the type of the array. For example, if we declare char A[10]; an array of 10 characters would require 10 bytes of storage for data. On the other hand, int A[10]; would require 10*sizeof(int) amount of storage. In general, the sizeof(A) returns the number of bytes required to store the array. Array representation, access and updates Arrays can be accessed and updated using its index. An array of n elements, has indices ranging from 0 to n-1. An element can be updated simply by assigning A[i] = x; A great care must be taken in dealing with arrays. Unlike in Java, where array index out of bounds exception is
Background image of page 1

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

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena thrown when indices go out of the 0. .n-1 range, C arrays may not display any warnings if out of bounds indices are accessed. Instead, compiler may access the elements out of bounds, thus leading to critical run time errors. Arrays can also be initialized as they are declared. For example, int A[] = {1,2,3,4} defines an initializes an array of size 4. Passing arrays to functions Arrays can be passed to functions using the array name. Array name is a const pointer to the array. For example, if we declare int A[10]; Then A is the address of the first element of the array. A can be thought of as const int* and can be passed to a function as follows. int foo(int array[], int size){ ….. } Call foo as : foo(A,10); Note that array size was passed to the function to make sure function foo accesses only the valid elements. Unlike Java arrays where size of the array is an attribute and hence can be accessed, C arrays do not carry size into function. Therefore the size is also typically provided as an argument to an external function. Array as a const pointer As stated above the name of the array is a const pointer to the first element of the array. Hence think of A as the address of A[0], A+1 as the address of A[1] etc. Assigning a value to a const pointer is illegal. For example, int A[n]; int* ptr = A ; /* is valid */
Background image of page 2
Copyright @ 2009 Ananda Gunawardena however A = ptr; /* is invalid as A cannot be changed */ Note that in the above example, the difference between A and ptr is that ptr is a pointer to an integer or int* and A is a const pointer to an integer or const int* Moreover, A is a static array managed by the compiler in the run time stack and ptr is just a pointer variables. Hence sizeof(A) returns total bytes required for A, while
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 14

Lecture%2005%20-%20%20Arrays%20and%20Pointers - Lecture 05...

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

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