Docsity

Unit 4 18CSS101J Programming for Problem Solving, Slides of Programming for Engineers

Partial preview of the text

Download Unit 4 18CSS101J Programming for Problem Solving and more Slides Programming for Engineers in PDF only on Docsity!

18CSS101J – Programming for Problem Solving

Unit IV

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI.

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI UNIT IV INTRODUCTION Passing Array Element to Function-Formal and Actual Parameters- Advantages of using Functions-Processor Directives and #define Directives-Nested Preprocessor Macro-Advantages of using Functions- Functions-Pointers and address operator-Size of Pointer Variable and Pointer-Operator-Pointer Declaration and dereferencing-pointers-Void Pointers and size of Void Pointers-Arithmetic Operations-Incrementing Pointers-Pointers-Constant Pointers-Pointers to array elements and strings-Function Pointers-Array of Function Pointers-Accessing Array of Function Pointers-Null Pointers-Pointers

Introduction

 Multidimensional arrays are also allowed to be passed as arguments to functions.  The first dimension is omitted when a multidimensional array is used as a formal parameter in a function.

Formal and Actual Parameters One-Dimensional Array  Actual Parameters: int a[10]; Function call: add(a); here, a is the base address of the array add is the function

Formal and Actual Parameters Two-Dimensional Array  Actual Parameters: int a[10][10]; Function call: add(a); here, a is the base address of the array ‘a’ b is the base address of the array ‘b’ add is the function

Formal and Actual Parameters One-Dimensional Array  Formal Parameters: within the function body void add(int a[][10]) { .... } int a[][10] - is a formal parameter, the first dimension is omitted only column value is taken into the account.

Adding Constant five to the given array using functions(One-Dimensional)  Main Function int main() { int a[50],i,n; scanf(“%d”,&n); for(i=1;i<=n;i++) scanf(“%d”,&a[i]); add(a,n); for(i=1;i<=n;i++) printf(“%d\n”,a[i]); return 0; }

Adding Constant five to the given array using functions(One-Dimensional)  Add Function: void add(int a[],int n) { int r; for(r=1;r<=n;r++) { a[r]=a[r]+5; } }

Main Function

int main() { int num[2][2], i, j; printf("Enter 4 numbers:\n"); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { scanf("%d", &num[i][j]); } } // passing multi-dimensional array to displayNumbers function displayNumbers(num); return 0; }

Display Numbers Function

void displayNumbers(int num[2][2]) { // Instead of the above line, // void displayNumbers(int num[][2]) is also valid int i, j; printf("Displaying:\n"); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { printf("%d\n", num[i][j]); } } }

Preprocessor Directives MACROS

 The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation.  A macro is a segment of code which is replaced by the value of macro.  Macro is defined by #define directive.  Preprocessing directives are lines in your program that start with #.  The # is followed by an identifier that is the directive name.  For example, #define is the directive that defines a macro.  Whitespace is also allowed before and after the #.

List of preprocessor directives

#include

#define

#undef

#ifdef

#ifndef

#if

#else

#elif

#endif

#error

#pragma

#include "file"  This variant is used for header files of your own program.  It searches for a file named file first in the current directory, then in the same directories used for system header files.  The current directory is the directory of the current input file. #include anything else  This variant is called a computed #include.  Any #include directive whose argument does not fit the above two forms is a computed include.

Macro's (#define) #define token value There are two types of macros: 1.Object-like Macros

  1. Function-like Macros Object-like Macros  The object-like macro is an identifier that is replaced by value.  It is widely used to represent numeric constants. For example: #include <stdio.h> #define PI 3. main() { printf("%f",PI); } Output: