Page 71 - DCAP407_DATA_STRUCTURE
P. 71
Data Structure
char *pointerfunction (void)
{
char c[ ] = “pointers and arrays”;
return c;
}
In this example, if it is necessary to “return” the address of c then you can
declare it with the “static” storage specifier.
Dangling pointers are usually created by the combination of malloc() and free() library calls. A pointer
becomes dangling when the block of memory referred to by the pointer is set free.
#include<stdlib.h>
{
char *c = malloc (A_CONST);
free( c) ;
c = NULL;
}
Did you know? In Java, object reference is a pointer and an address.
4.4 Pointers to Functions
Pointers can be used in function declaration. By using pointers, a complex function can be easily
represented. The variables used to invoke the called function are known as arguments or actual
parameters, and the variables used in the actual definition of the called function are called dummy
parameters or formal parameters. Pointers used in the function definition are classified into two groups.
They are:
1. Pass by value or call by value
2. Pass by reference or call by reference
Pass by Value or Call by Value
You have seen that when a function is invoked, a link has to be established between the formal and the
actual parameters. A temporary storage has to be created so that the value of the actual parameters can
be stored. The formal parameters will pick up the value from the storage area. This mechanism of data
transfer between actual and formal parameters allows the actual parameter values to be copied into the
formal parameters. This method is known as “call by value” or “pass by value”. The corresponding
formal parameter will represent a local variable in the called function. The current value of the
corresponding actual parameter will become the initial value of the formal parameter. The value of the
formal parameter might be changed in the body of the actual parameter or subprogram by using the
assignment or input statements. This will not change the value of actual parameters.
# include<stdio.h>
#include<conio.h>
void num(int x, int y)
{
x = 100;
y = 200;
}
void main()
{
int m, n;
m = 10;
n = 20;
num (m, n);
printf(“m = %d and n = %d”, m, n);
64 LOVELY PROFESSIONAL UNIVERSITY