1 of 15

CSO101: Computer Programming

Lecture – 16

O.S.L. Bhavana

2 of 15

Multi-dimensional Arrays

  • Declaration of a multidimensional array should specify all the dimensions.
  • If the initialization is list initialization, only the left most dimension may be skipped. All the other dimensions are compulsory.
  • EX: Correct declarations:
    • int a[2][3] = {{1,2,4},{9,8,0}};
    • int a[][3][2] = {{{1,2},{2,3}}, {{3,6},{11,9}}}; // equivalent to declaring a[2][3][2]
    • int m=9, n=10; int a[n][m][n]; // declares a n*m*n matrix
  • Ex: Incorrect declarations
    • int a[2][] = {{1,2,4},{9,8,0}};// second dimension is missing
    • int a[][][2] = {{{1,2},{2,3}}, {{3,6},{11,9}}};// second dim missing

3 of 15

Multi-dimensional arrays as function arguments

  • All except the first dimension are compulsory to mention like in the declaration
  • If the array dimensions are constants, then they can be mentioned in the function declaration as follows

Ex: Function declaration: int max(int a[2][3]);

Function call: max(a);

Ex: incorrect function declaration: int max(int a[][]); //second dim missing

  • If the array dimensions are variables, then these variables can be declared in the function declaration too
  • Ex: Function declaration: int max(int x, int y, int a[x][y]); // here the dimensions of the 2D matrix are allowed to be variables. Note that the dimension variables x,y are declared before a.
  • Function Call: int x,y; scanf(“%d %d”,&x,&y); int a[x][y] max(x,y,a);

4 of 15

Multi-dimensional arrays as function arguments

  • Finding the max of elements of a 2Dmatrix

5 of 15

Storage Classes

  • Variables/identifiers consume memory
  • Some variables are only available for limited time
    • Variables declared within the loop statement – available only during loop execution
    • Variables declared within a function – available only during the function execution
    • Variables declared in main

6 of 15

Storage Classes

  • Various categories storage classes are available to specify availability of variables in the memory
  • C provides 4 storage classes
    • Auto
    • Register
    • Extern
    • Static

  • A storage class determines its storage duration, scope and linkage
  • Scope of a variable is where the variable can be referenced in a program
  • Storage duration is for how long the variable exists in the memory

7 of 15

Auto

  • A variable declared with auto keyword is destroyed when its enclosing block is exited
  • EX:
  • main()
  • {{auto int a=10;} printf(“%d”, a); } // results in error. a has to be used within the block its declared
  • Auto is the default storage class and can be omitted.
  • A variable may be re-declared in different scopes. Re-declaration within same scope results in compilation errors.
  • EX: {int a=10; {int a=900; printf(“%d”,a);}printf(“%d”,a);} outputs 900, 10
  • Auto variables may be accessed outside their block using pointers! (later)

8 of 15

Register

  • register int a =1;
  • Similar to auto
  • The variable is stored in the CPU register (not in the memory) for fast access
  • If CPU registers are not available for storage, then the variable may also be stored in the memory.
  • Some compilers are clever enough to identify frequently used variables and store them automatically in the registers

9 of 15

Static Storage Duration

  • To declare variables that exist throughout the program the keywords extern or static need to be used
  • Variables with static storage duration exist till the end of the program.
  • Even functions have static storage declaration

10 of 15

Static Storage Duration: Global Variables

  • Global variables are declared outside the function descriptions
  • extern is the default keyword for such variables
  • Global variables and functions can be referenced by any function that follows their declaration or definition in the file.
  • Need to be cautiously used; as many functions can access the same variable
  • #include <stdio.h>
  • int a=9;

void f(){......}

void g(){......}

main(){....} // variable a can be used by f, g and main

11 of 15

Static Storage Duration: Global Variables

  • Global variables are declared outside the function descriptions
  • extern is the default keyword for such variables
  • extern keyword can be used to access variables/functions across different files
  • Ex: - int foo(int arg1, char arg2);�is treated as - extern int foo(int arg1, char arg2);

which tells the compiler that there exists a function with name foo and its definition is somewhere else�

12 of 15

Static Storage Duration: Static Keyword

  •  static int count = 1;
  • Static variables are local to the function in which they are declared but retains its value even after  the function is exited.
  •  All numeric variables of static storage duration are initialized to zero if they are not explicitly initialized.
  • But initialization is done only for the first time (not on subsequent function calls)
  • means that internal static variables provide private, permanent storage within a single

13 of 15

Static Keyword

 void f_1(void);

 int main( )

 {func(); func();func(); } // prints 1,2,3

 void func(void)

{

   static int count = 1;

   printf(“%d\n”, count);

   count ++;

 } 

14 of 15

Static Keyword

 void f_1(void);

 int main( )

 {f_1(); f_1();f_1(); } // prints 1,1,1

 void f_1(void)

{

   static int count = 1; count=1;

   printf(“%d\n”, count);

   count ++;

 } 

Useful in counting the number of recursive calls a function has

15 of 15

Static Keyword

  • Useful in counting the number of recursive calls

int fib(int n) //function to print the n^th fibonacci number

 {

static int counter = 0; 

counter ++;

printf(“In fib with counter = %d \n”, counter);

if (n==0 || n==1) return n;

else return fib(n-2)+ fib(n-1);

 }

Number of recursive calls in fib(4)?