Docsity

Arrays and Strings in C, Lecture notes of C programming

Partial preview of the text

Download Arrays and Strings in C and more Lecture notes C programming in PDF only on Docsity!

Chapter 4

Arrays and Strings

FE – C Programming

Prof. Namrata Jiten Patel

Assistant Professor

Dept. of Computer Engineering,

SIES Graduate School of Technology

Objectives :To provide exposure to problem-solving by developing an

algorithm, flowchart and implement the logic using C programming

language

Outcomes: Learner will be able to …

  1. Demonstrate the use of arrays, strings and structures in C

language.

Namrata Jiten Patel

Learning Outcomes

  • (^) Define an Array
  • (^) Declaration of 1-dim array,ways of access individual array

element,representation of array element in memory and other

possible operations.

  • (^) Declaration of 2-dim array,ways of access individual array

element,representation of array element in memory and other

possible operations

  • (^) Explain array of Strings ,their declarations ,initialization and

other operations.

Namrata Jiten Patel

Introduction

  • The variables used so far have all had a common characteristic:

each variable can only be used to store a single value at a time. For

example, each of the variables ch, n, and price declared in the

statements.

These kind of variable holds different data types, and each variable

can only store one value of the declared data type.

These types of variables are called scalar variables.

A scalar variable is a single variable whose stored value is an atomic

type. This means that the value cannot be further subdivided or

separated into a legitimate data type.

Namrata Jiten Patel

Define Array

  • An array is a fundamental data structure that enables the storing and

manipulation of potentially huge quantities of data.

  • An array stores an ordered sequence of homogeneous values.

Homogeneous means that all the values are of the same data type.

  • (^) The order of the values are also preserved, i.e., the integer array {1,

2, 3, 4} is different from {1, 4, 3, 2}

Namrata Jiten Patel

  • (^) An array is a collection of individual data elements that is

 Ordered—one can count off the elements 0, 1, 2, 3, ...

 Fixed in size

 Homogeneous—all elements must be of the same type, e.g., int, float, char,

etc.

  • (^) In C, each array has two fundamental properties: the data type and

the size.

  • (^) Individual array elements are identified by an integer index. In C, the

index begins at zero and is always written inside square brackets.

Namrata Jiten Patel

One-Dimensional array

  • A one-dimensional array declaration is a data type followed by an

identifier with a bracketed constant integral expression

  • The value of the expression, which must be positive, is the size of the

array.

  • (^) It specifies the number of elements in the array.
  • The array subscripts can range from 0 to (size –1).
  • (^) The lower bound of the array subscripts is 0 and the upper bound is

(size –1). Thus, the following relationships hold.

Namrata Jiten Patel

Illustrating the organization of number array in memory

with the correct designation for each array element.

Namrata Jiten Patel 11

Array Declarations

Namrata Jiten Patel

Initializing the array

Namrata Jiten Patel

Automatic sizing While initializing, the size of a one-dimensional array can be

omitted as shown.

int arr[] = {3,1,5,7,9};

Here, the C compiler will deduce the size of the array from the initialization

statement. From the above initialization statement, the size of the array is

deduced to be 5.

Basic example

  • (^) #include
  • #defi ne ARRAY_SIZE 10
  • int main()
  • (^) { int index, a[ARRAY_SIZE];
  • (^) for(index = 0; index < ARRAY_SIZE;

index++)

  • {
  • a[index] = 0;
  • (^) printf(“a[%d] = %d\n”, index, a[index]);
  • (^) }
  • printf(“\n”);
  • return 0;
  • (^) }

Namrata Jiten Patel 16

Internal representation of Array

“ In C, an array is implemented as a single block of memory, with element 0 occupying the first

‘slot’ in that block.

That is, arrays are allocated contiguous space in memory and so they are closely related to the

notion of pointer which will be discussed later.”

  • (^) For a simple variable (e.g., int, double, etc.) of data type X, the

compiler allocates sizeof(X) bytes to hold it.

  • (^) For an array of length L and data type X, the compiler allocates
  • L* sizeof (X) bytes.
  • (^) For example
  • (^) Given an array like int scores[100],
  • (^) The compiler allocates 200 bytes starting at some location, say 64789.
  • (^) Given an expression like scores[5], the compiler accesses the value

stored at the memory location starting with byte

  • (^) In general, a[i] is located at byte: base address of a + i * sizeof (type

of array).

Namrata Jiten Patel

References to elements outside of

the array bounds

  • Therefore, the programmer’s responsibility to ensure that all

references to array elements lie within the declared bounds of the

associated arrays

Namrata Jiten Patel

Glimpse of memory allocation

  • It is important to see how memory is allocated to arrays.
  • (^) It is given a small area of the computer’s memory to use. This

memory, which is known as the stack, is used by variables in the

program

  • (^) Example:

Namrata Jiten Patel