Notes_75_C-Intro_6
Notes_75_C-Intro_6
Lect. 6
EI-ES-106
Programming for Problem Solving
Module - I
Module - 2
• Functions: User defined functions-function definition, function
declaration, function call, Formal and actual parameters, Categories
of functions, Passing parameters to functions- Pass by value, Pass
by reference, Recursion- types of recursion, programming example
s and exercises.
• Arrays and Strings: Arrays: Classification of arrays, Storing value
in arrays, Using arrays with Functions- passing individual elements
of array, passing the whole array, Multidimensional arrays-addition
and multiplication of matrices,
• Searching and Sorting-Linear search, Binary search, Bubble sort,
String: Declaring, Initializing, Printing and reading strings, String
input and output functions, String handling functions, Arrays of
strings, programming examples and Exercises.
1
5/9/2023
Books
• Text Books:
– “The C Programming Language”, Brian W. Kernighan and Dennis
M. Ritchie, 2nd Edition, PHI, 2012.
– "Problem Solving with C ", Jacqueline Jones & Keith Harrow, 1st
Edition, Pearson2011.
– “Let Us C” , by Yashavant Kanetkar, 5th Edition, BPB
• Reference Books:
– “Computer Concepts and C Programming”, Vikas Gupta,
Dreamtech Press2013.
– "Programming with C ", R. S. Bichkar, University Press,2012.
– "Computer Programming in C ", V. Rajaraman, PHI, 2013.
Objectives
1. To explain the problem solving concepts using a
computer.
2. To develop problem solutions for the computer by using
problem solving tools.
3. To describe the Programming structure of C language.
4. To convert an Algorithm, Pseudo code and Flowchart into a C
program
5. To find errors and execute a C program
2
5/9/2023
Outcomes
CO1: Understand the fundamental concepts of computer
hardware and number systems.
CO2: Apply the basic programming skills of C Language in
problem solving.
CO3: Use different data types, decision structures, loops,
arrays, strings and functions of C- programming to
design a computer program.
CO4: Apply dynamic memory concepts with pointers.
CO5: Apply various algorithms in solving sorting problems.
CO6: Apply linear data structures like Stack, Queues and
Trees in organizing and traversing data.
POINTERS
3
5/9/2023
4
5/9/2023
Introduction
• Every variable in C has a name and a value associated with it. When a variable is declared,
a specific block of memory within the computer is allocated to hold the value of that variable.
The size of the allocated block depends on the type of the data.
int x = 10;
• When this statement executes, the compiler sets aside 2 bytes of memory to hold the value
10. It also sets up a symbol table in which it adds the symbol x and the relative address in
memory where those 2 bytes were set aside.
• Thus, every variable in C has a value and an also a memory location (commonly known as
address) associated with it. Some texts use the term rvalue and lvalue for the value and the
address of the variable respectively.
• The rvalue appears on the right side of the assignment statement and cannot be used on
the left side of the assignment statement. Therefore, writing 10 = k; is illegal.
• Here, data_type is the data type of the value that the pointer will point to. For example:
int *pnum; char *pch; float *pfnum;
int x= 10;
int *ptr = &x;
• The '*' informs the compiler that ptr is a pointer variable and the int specifies that it will store the
address of an integer variable.
• The & operator retrieves the lvalue (address) of x, and copies that to the contents of the pointer ptr.
5
5/9/2023
6
5/9/2023
NULL POINTERS
• A null pointer which is a special pointer value that is known not
to point anywhere. This means that a NULL pointer does not point to
any valid memory address.
• To declare a null pointer you may use the predefined constant NULL,
int *ptr = NULL;
• You can always check whether a given pointer variable stores
address of some variable or contains a null by writing,
if ( ptr == NULL)
{ Statement block; }
• Null pointers are used in situations if one of the pointers in the
program points somewhere some of the time but not all of the time.
In such situations it is always better to set it to a null pointer
when it doesn't point anywhere valid, and to test to see if it's a
null pointer before using it.
7
5/9/2023
GENERIC POINTERS
• A generic pointer is pointer variable that has void as
its data type.
• The generic pointer, can be pointed at variables of any
data type.
• It is declared by writing
void *ptr;
• You need to cast a void pointer to another kind of
pointer before using it.
• Generic pointers are used when a pointer has to point
to data of different types at different times. For ex,
GENERIC POINTERS
For ex,
#include<stdio.h> OUTPUT:
int main() Generic pointer points to the integer value = 10
Generic pointer now points to the character = A
{ int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d",
*(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character %c",
*(char*)gp);
return 0;
}
8
5/9/2023
9
5/9/2023
int arr[]={1,2,3,4,5};
int *parr;
parr=arr;
int arr[2][2]={{1,2},{3,4}};
int (*parr)[2];
parr=arr;
10
5/9/2023
#include<stdio.h>
main()
{ char str[] = “Oxford”;
char *pstr = str;
printf(“\n The string is : “);
while( *pstr != ‘\0’)
{ printf(“%c’, *pstr);
pstr++;
}
}
puts(pstr);
– Here the "const" modifier is used to assure the user that the
function will not modify the contents pointed to by the source
pointer. Note that the address of the string is passed to the
function as an argument.
11
5/9/2023
ARRAY OF POINTERS
An array of pointers can be declared as
int *ptr[10];
12
5/9/2023
POINTER TO FUNCTION
• This is a useful technique for passing a function as an argument to
another function.
(*func)(1,2); OR func(1,2);
POINTER TO FUNCTION
#include <stdio.h>
void print(int n);
main()
{ Comparing Function Pointers
void (*fp)(int);
fp = print; if(fp >0) // check if initialized
(*fp)(10); { if(fp == print)
fp(20); printf("\n Pointer points to Print");
else
return 0; printf("\n Pointer not initialized!");
} }
13
5/9/2023
POINTERS TO POINTERS
You can use pointers that point to pointers. The pointers in turn,
point to data (or even to other pointers). To declare pointers to
pointers just add an asterisk (*) for each level of reference.
For example, if we have:
int x=10;
int *px, **ppx;
px=&x; 10 1002 2004
ppx=&px; x px ppx
Now if we write,
printf(“\n %d”, **ppx);
Then it would print 10, the value of x.
14
5/9/2023
• Tired by now !
Any Question?
……..
Thanks!
9 May 2023 Pardeep Kumar, KUK 30
15