Structures & Pointers in C Programming
Structures & Pointers in C Programming
Structures & Pointers in C Programming
Library Functions
They are in-built functions of ‘C’ library. These are
already defined in header files.
Eg. printf(); Defined in stdio.h
strlen(); Defined in string.h
User Defined Functions
Programmer creates his own functions to perform
some task
Eg. add()
Library functions
5
Function call
Function definition
Function Declaration
7
Example:
int add(int a,int b);
void display();
The functions can be declared above the main()
or in the main() method
If function does not return any value then void
data type is used
Function returns only one value
Function Call
9
Example:
add(a,b);
fact(6);
display();
When compiler encounters function call, the control is
transferred to the function
The function is then executed line by line
A value is returned when a return statement is
encountered
Types of Function Call
11
Call By Reference
Call By Value
12
The function’s return type, followed by its name, parameter list, and body constitute
the ‘function definition’
Function name must be same in declaration, call and definition
Each function has a unique name
‘Function body’ refers to the statements that represent the actions that the function
performs
Function Definition
15
Syntax:
return_type function_name( parameter list ) { body of
the function }
Function Definition
16
Formal Arguments
Function Arguments
18
Syntax:
return(expression); OR
return(constant); OR
return;
Example:
return(c=a*b); OR return(c);
It always returns the value of the expression or variable which is
written inside the parenthesis following the return statement.
When a function does not return any value, then void is used.
return statement can return only one value.
The ‘return’ Statement
21
Output:
/* scope of block variables */ x=10
#include<stdio.h> x=10
int main(){
{ /* outer block */
int x=10;
{ /* inner block */
printf("x=%d",x);
}
printf("\nx=%d",x);
}
return 0;
}
Block Variable Example
29
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b; int c;
/* actual initialization */
a = 10; b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Global Variables
32
#include <stdio.h>
/* global variable declaration */
int g;
int main () {
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
Scope Rules
34
#include<stdio.h>
int x = 10; int main()
/* Called by g()*/ {
int f(){ printf("%d", g());
return x; printf("\n");
} return 0;
/* g() has its own variable*/ }
/* named as x and calls f()*/
int g(){ Output
int x = 20; 10
return f();
}
Dynamic Scoping
37
Example:
enum flag{constant1, constant2, constant3, ....... };
/* An example program to
/* In both of the below
demonstrate working of
cases, "day" is defined enum in C*/
as the variable of type #include<stdio.h>
week. */ enum week{Mon, Tue,
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
Wed}; int main(){
enum week day;
enum week day; day = Wed;
/* Or */ printf("%d",day);
enum week{Mon, Tue, return 0;
Wed} day; }
Output: 2
Pointers
50
data-type *pointer_name;
Data type of a pointer must be same as the data
type of a variable to which the pointer variable is
pointing.
Void type pointer works with all data types, but is not
used often used.
Initialization of Pointer Variable
53
int a = 10 ;
or
declaration together
Example(Continue)
55
int i = 3;
printf (“\n Address of I = %u”, &i );
printf (“\n Value of I = %d”, i);
}
‘&’ used in this statement is C’s ‘address of’
operator.
The expression &i returns the address of the
OUTPUT: variable i.
Address of i = 65524
Value of i = 3
Example (Continue)
56
main()
• The other pointer operator
{
int i = 3; available in C is ‘*’, called
printf (“\n Address of i = %u”, &i ); ‘value at address’ operator.
printf (“\n Value of i = %d”, i);
• It gives the value stored at a
printf (“\n Value of i = %d”, *(&i));
} particular address.
OUTPUT: • The ‘value at address’
Address of i = 65524
operator is also called
Value of i = 3
Value of i = 3 ‘indirection’ operator.
Example (Continue)
57
main()
{
INPUT
int i = 3;
int *j;
j = &i;
OUTPUT:
printf (“\n Address of i = %u”, &i );
Address of i = 65524
printf (“\n Address of i = %u”,j );
Address of i = 65524
printf (“\n Address of j = %u”, &j );
Address of j = 65522
printf (“\n Value of j= %u”, j);
Value of j = 65524
printf (“\n Value of i = %d”, i);
Value of i = 3
printf (“\n Value of i = %d”, *(&i));
Value of i = 3
printf (“\n Value of i = %d”, *j);
Value of i = 3
}
i is an ordinary int,
Example (Continue)
j is a pointer to an int (often called an integer
pointer), whereas k is a pointer to an integer
pointer
58
main()
printf (“\n 9.Value of i = %d”, i);
{
printf (“\n 10.Value of i = %d”, *(&i));
int i = 3, *j, **k;
printf (“\n 11.Value of i = %d”, *j);
j = &i;
printf (“\n 12.Value of i= %d”, **k);
k = &j;
}
printf (“\n 1.Address of i = %u”, &i );
OUTPUT:
printf (“\n 2.Address of i = %u”, j );
1.Address of i = 65524 7.Value of j = 65524
printf (“\n 3.Address of i = %u”, *k );
2.Address of i = 65524 8.Value of k = 65522
printf (“\n 4.Address of j = %u”, &j );
3.Address of i = 65524 9.Value of i = 3
printf (“\n 5.Address of j = %u”, k ); 4.Address of j = 65522 10.Value of i = 3
printf (“\n 6.Address of k = %u”, &k ); 5.Address of j = 65522 11.Value of i = 3
printf (“\n 7.Value of j= %u”, j); 6.Address of k = 65520 12.Value of i = 3
printf (“\n 8.Value of k= %u”, k);
Pointer Arithmetic
59
main() OUTPUT:
{ x = 20 y = 10
int a = 10, b=20; a = 10 b = 20
swapv(a,b);
printf(“\n a = %d b= %d”, a,b); • In this method the ‘value’ of each of
} the actual arguments in the calling
swapv (int x, int y) function is copied into corresponding
{ formal arguments of the called
int t; function.
t = x; • With this method the changes made to
x = y; the formal arguments in the called
y = t; function have no effect on the values
printf(“\n x = %d y = %d”, x,y); of actual arguments in the calling
} function. 64
Call by Reference Example
65
main() OUTPUT:
{ x = 20 y = 10
int a = 10, b=20; a = 20 b = 10
swapr(&a,&b);
printf(“\n a = %d b= %d”, a,b); • In this method (call by reference) the
} addresses of actual arguments in the calling
swapr (int *x, int *y)
function are copied into formal arguments
{
of the called function.
int t;
t = *x; • This means that using these addresses we
*x = *y; would have an access to the actual
*y = t;
arguments and hence we would be able to
printf(“\n x = %d y = %d”, *x,*y);
} manipulate them 65
Dynamic Memory Allocation
66
int *ip;
if ((ip = (int*)malloc(100 *
sizeof(int))) == NULL){
printf("out of memory\n");
exit();
}
free()
70
free()
e.g.
free(newPtr);
int main(){
float * buffer;
int i;
return 0;}
What is a File ?
74
file)
a+ : open for reading and writing (append if
file exists)
File Open
80
pointer variable:
fclose(spData);
fprintf()
84
Syntax:
fprintf (fp,"string",variables);
Example:
int i = 12;
float x = 2.356;
char ch = 's';
FILE *fp;
fp=fopen(“out.txt”,”w”);
fprintf (fp, "%d %f %c", i, x, ch);
fscanf()
85
Syntax:
fscanf (fp,"string",identifiers);
Example:
FILE *fp;
Fp=fopen(“input.txt”,”r”);
int i;
fscanf (fp,“%d",i);
getc()
86
Syntax:
identifier = getc (file pointer);
Example:
FILE *fp;
fp=fopen(“input.txt”,”r”);
char ch;
ch = getc (fp);
putc()
87
Example:
FILE *fp;
char ch;
putc (ch,fp);
fread ()
88
Declaration:
size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
Remarks:
fread reads a specified number of equal-sized data items
from an input stream into a block.
Example:
#include <stdio.h>
int main()
{
FILE *f;
char buffer[11];
if (f = fopen("fred.txt", “r”))
{
fread(buffer, 1, 10, f);
buffer[10] = 0;
fclose(f);
printf("first 10 characters of the file:\n%s\n", buffer);
}
return 0; }
fwrite()
90
Declaration:
size_t fwrite(const void *ptr, size_t size, size_t n,
FILE*stream);
Remarks:
fwrite appends a specified number of equal-sized data items
to an output file.
#include <stdio.h>
int main()
{
char a[10]={'1','2','3','4','5','6','7','8','9','a'};
FILE *fs;
fs=fopen("Project.txt","w");
fwrite(a,1,10,fs);
fclose(fs);
return 0;
}
fseek()
92
This function sets the file position indicator for the stream
pointed to by stream or you can say it seeks a specified place
within a file and modify it.
SEEK_SET Seeks from beginning of file
SEEK_CUR Seeks from current position
SEEK_END Seeks from end of file
Example:
#include <stdio.h>
int main()
{
FILE * f;
f = fopen("myfile.txt", "w");
fputs("Hello World", f);
fseek(f, 6, SEEK_SET); SEEK_CUR, SEEK_END
fputs(" India", f);
fclose(f);
return 0; }
End of File
93
FILE *fptr1;
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == feof(fptr1) )
{
printf ("End-of-file encountered.\n”) ;
}