Docsity

Arrays and Strings in C Programming, Assignments of Procedural Law

Partial preview of the text

Download Arrays and Strings in C Programming and more Assignments Procedural Law in PDF only on Docsity!

Lecture 10: Array and String

  • (^) Understand the concept of array and its memory organization
  • (^) Know how arrays are declared and initialized
  • (^) Know how arrays are accessed
  • (^) Use multi dimensional arrays
  • (^) Know about string representations
  • (^) Know about various character and string functions

Objectives

  • (^) Many applications require the processing of multiple data

items with

  • (^) Common characteristics (e.g., same data type)
  • (^) E.g., set of numbers, set of characters, set of names

Need for an array

‘O’ ‘N’ ‘E’ ‘\0’

  • (^) It is used to store
    • (^) similar data items
    • (^) in contiguous memory locations
    • (^) under a single name
  • (^) It holds a fixed number of equally sized data elements (of

the same data type)

  • (^) The individual elements are accessed by
    • (^) specifying the subscript/index using [] operator
    • (^) Index start from 0, increased by 1 for every element

Array

  • (^) Arrays are declared with appropriate data type and size
  • (^) Arrays can be of single dimension or multi dimensions
  • (^) Array declaration reserves space in memory
  • (^) General form:
    • (^) datatype arrayname[size];

Array Declaration

  • (^) Defines an integer array x of 5 integers starting at x[0], and

ending at x[4]

  • (^) int x[5];
  • (^) Defines a character array, which represent a string of

maximum 16 characters

  • (^) char str[16]=“qwerty”;
  • (^) Defines a floating point array sa of 10 floating points starting

at sa[0], and ending at sa[9]

  • (^) float sa[10];

Examples

  • (^) Array elements can be initialized during declaration or in the program
  • (^) When arrays are initialized during declaration, partial initialization is allowed
  • (^) In partial initialization, the uninitialized elements are initialized to
    • (^) Zero for numeric array
    • (^) Null for character array
  • (^) If initialized, array can be declared without specifying exact size
    • (^) The size equals to the number of elements initialized
  • (^) General forms
    • (^) datatype arrayname[size] = {values(s)}
    • (^) datatype arrayname[] = {values(s)}

Array Initialization

Array declaration/initialization int a[5] = {1, 2, 3, 4, 5}; /a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5; / int a[5] = {0}; /all the array elements are initialized to zero int a[5] = {1, 2, 3, 4}; /a[4] = 0/ int a[] = {1, 2, 3, 4}; /a[0]=1; a[1]=2; a[2]=3; a[3]=4; (if not specified, the size depends on the number of values initialized)/ float b[2} = {10.2, 45.34}; /b[0]=10.2; b[1]=45.34;*/

Getting the value for Arrays int a[3]; (1) scanf(“%d”, &a[0]); /gets value for the 1 st location/ scanf(“%d”, &a[1]); /gets value for the 2nd^ location/ scanf(“%d”, &a[2]); /gets value for the 3rd^ location/ (2) scanf(“%d%d%d”, a, a+1, a+2); /get values for first 3 locations/ (3) for(i=0;i<3;i++){ scanf(“%d”, &a[i]); } /loop statement is used to set/get the array elements/

Printing out the array elements int a[3]; (1) printf(“%d”, a[0]); /prints value of 1 st location/ printf(“%d”, a[1]); /prints value of 2nd^ location/ printf(“%d”, a[2]); /prints value of 3rd^ location/ (2) print(“%d%d%d”, a[0], a[1], a[2]); /prints values of first 3 locations/ (3) for(i=0;i<3;i++){ printf(“%d”, a[i]); } /loop statements is used to print the array elements/

  • (^) The elements of an array can themselves be arrays
  • (^) Multidimensional arrays will also occupy the contiguous

memory locations

  • (^) Two dimensional arrays can be viewed as set of one

dimensional array (rows & columns)

  • (^) Three dimensional arrays can be viewed as set of two

dimensional arrays

Multi-dimensional

Array

  • (^) Two-dimensional arrays are defined in the same way as

one dimensional array

  • (^) Except that a separate pair of square of brackets is required for second dimension
  • (^) General form
  • (^) datatype arrayname[row][column]
Two-dimensional array -
declaration
Two-dimensional array
initialization

int num[2][3] = {1, 2, 3, 4, 5, 6}; /* a[0][0] = 1; a[0][1]=2; a[0][2]=3; a[1][0] = 1; a[1][1]=2; a[1][2]=3;/ /1, 2, 3 are in the 1 st^ row, and 4, 5, 6 are in the 2nd^ / int num[2][3] = {1, 2, 3, 4, 5}; /num[1][2]=0/ int num[2][3] = {{1, 2, 3}, {1, 2, 3}}; /row elements are initialized separately/ int num[2][3] = {{1, 2}, {4}}; /num[0][2] = 0; num[1][1]=0; num[1][2]=0;*/

  • (^) Advantages
    • (^) Simple and easy to use
    • (^) Stored in contiguous locations
    • (^) Fast retrieval because of its indexed nature
    • (^) No need to worry about the allocation and de-allocation of arrays
  • (^) Limitations
    • (^) Arrays are static in nature (fixed size). Memory is allocated at the beginning of the execution. If n is defined and used m -> wasted n-m
    • (^) No automatic array bounds checking during compilation
Advantages/Limitations of arrays