Professional Documents
Culture Documents
#include<stdio.h>
Void areaperi( int, float*, float*);
Void main() {
Int radius;
Float area, perimeter;
Printf(“Enter radius of a circle”);
Scanf(“%d”,&radius);
Areaperi(radius, &area, &perimeter);
Printf(“Area= %f”, area);
Printf(“\nPerimeter = %f”, perimeter);}
Void areaperi(int r, float *a, float *p)
{ *a= 3.14*r*r;
*p = 2 * 3.14 *r}
Pointers to functions
• In C, like normal data pointers (int *, char *, etc), we can have pointers to functions.
#include <stdio.h>
void fun(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
// fun_ptr is a pointer to function fun()
void (*fun_ptr)(int) = &fun;
/* The above line is equivalent of following two
void (*fun_ptr)(int);
fun_ptr = &fun;
*/
// Invoking fun() using fun_ptr
(*fun_ptr)(10);
return 0;
}
• Output:
• Value of a is 10
How to declare a pointer to a function?
1) Unlike normal pointers, a function pointer points to code, not
data. Typically a function pointer stores the start of executable
code.
• 2) Unlike normal pointers, we do not allocate de-allocate
memory using function pointers.
•
3) A function’s name can also be used to get functions’ address.
{
void (*fun_ptr)(int) = fun; // & removed
fun_ptr(10); // * removed
return 0;
}
4) Like normal pointers, we can have an array of function pointers.
5) Function pointer can be used in place of switch case. For example, in below program, user is asked for
a choice between 0 and 2 to do different tasks.
#include <stdio.h>
void add(int a, int b)
{ printf("Addition is %d\n", a+b);
}
void subtract(int a, int b)
{ printf("Subtraction is %d\n", a-b);
}
void multiply(int a, int b)
{ printf("Multiplication is %d\n", a*b);
}
int main()
{ // fun_ptr_arr is an array of function pointers
void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};
unsigned int ch, a = 15, b = 10;
printf("Enter Choice: 0 for add, 1 for subtract and 2 ""for multiply\n");
scanf("%d", &ch);
if (ch > 2) return 0;
(*fun_ptr_arr[ch])(a, b);
return 0;
}
Output:- Enter Choice: 0 for add, 1 for subtract and 2 for multiply 2 Multiplication is 150
6) Like normal data pointers, a function pointer can be passed as an argument
and can also be returned from a function.
#include <stdio.h>
void fun1() { printf("Fun1\n"); }
void fun2() { printf("Fun2\n"); }
// A function that receives a simple function as parameter and calls the function
void wrapper(void (*fun)())
{
fun();
}
int main()
{
wrapper(fun1);
wrapper(fun2);
return 0;
}
Strings and Pointer
• a string is a sequence of characters which we save in an
array. And in C programming language the \0 null character
marks the end of a string.
• Creating a string
• In the following example we are creating a
string str using char character array of size 6.
• char str[6] = "Hello";
• The above string can be represented in memory as follows.
Creating a pointer for the string
• The variable name of the string str holds the address of the first element of the
array i.e., it points at the starting memory address.
• So, we can create a character pointer ptr and store the address of the
string str variable in it. This way, ptr will point at the string str.
• In the following code we are assigning the address of the string str to the
pointer ptr.
• char *ptr = str;
• We can represent the character pointer variable ptr as follows.
• The pointer variable ptr is allocated memory address 8000 and it holds the address
of the string variable str i.e., 1000.
Accessing string via pointer
• To access and print the elements of the string we can use a loop and check for the \
0 null character.
• In the following example we are using while loop to print the characters of the string
variable str.
#include <stdio.h>
int main(void) {
// string variable
char str[6] = "Hello";
// pointer variable
char *ptr = str;
// print the string
while(*ptr != '\0') {
printf("%c", *ptr);
// move the ptr pointer to the next memory location
ptr++;
}
return 0;
}
Using Pointer to Store String
• We can achieve the same result by creating a character
pointer that points at a string value stored at some memory
location.
Rollno : 1
Name : Aman
Additional Features of Structure
• The values of a structure variable can be assigned
to another structure variable of the same type
using the assignment operator.
• One structure can be nested within another
structure. Using this facility, complex data types
can be created.
• Like an ordinary variable, a structure variable can
also be passed to a function.
• The way we can have a pointer pointing to an int,
or a pointer pointing to a char, similarly we can
have a pointer pointing to a struct.
Uses of Structure
• Changing the size of the cursor
• Changing the content of the screen
• Placing the cursor at an appropriate position on
screen.
• Receiving a key from keyboard.
• Displaying the directory of a disk
• Sending the output to printer
• Interacting with the mouse.
• Hiding the file from the directory.
Union
• Union also stores the different types of elements i.e
heterogeneous elements. The union keyword is used
to define structure. Union takes the memory of largest
member only so occupies less memory than
structures.
Syntax
union union_name {
data_type member1;
data_type memeberN;
}Variable1, Variable2..;
Simple Program Using Union
#include <stdio.h>
#include <string.h>
union student {
int rollno;
char name[60];
}s1;
//declaring s1 variable for union
void main( ) {
//store first employee information
s1.rollno=1;
strcpy(s1.name, “Aman”);//copying string into char array
//printing first employee information
printf( "Rollno : %d\n", s1.rollno);
printf( "Name : %s\n", s1.name);
}
Output
Output
Rollno : 3546656
Name : Aman
Rollno takes garbage value because name has
large memory size. So only name will have
actual value.
Difference between Structure and Union
• Structure and union both are user defined data types which
contains variables of different data types. Both of them have
same syntax for definition, declaration of variables and for
accessing members. Still there are many difference between
structure and union. In this tutorial we will take a look on
those differences.
• In structure each member get separate space in memory.
Take below example.
• struct student { int rollno; char gender; float marks; }s1;
• The total memory required to store a structure variable is
equal to the sum of size of all the members. In above case 7
bytes (2+1+4) will be required to store structure variable s1.
• In union, the total memory space allocated is equal to the member with
largest size. All other members share the same memory space. This is the
biggest difference between structure and union.
• union student { int rollno; char gender; float marks; }s1;
• In above example variable marks is of float type and have largest size (4
bytes). So the total memory required to store union variable s1 is 4 bytes.
• We can access any member in any sequence.
• s1.rollno = 20; s1.marks = 90.0; printf("%d",s1.rollno);
• The above code will work fine but will show erroneous output in the case of
union. We can access only that variable whose value is recently stored.
• s1.rollno = 20; s1.marks = 90.0; printf("%d",s1.rollno);
• The above code will show erroneous output. The value of rollno is lost as
most recently we have stored value in marks. This is because all the members
share same memory space.
• All the members can be initialized while declaring the variable of structure.
Only first member can be initialized while declaring the variable of union. In
above example we can initialize only variable rollno at the time of declaration
of variable.
BASIS OF COMPARISON STRUCTURE UNION
Basic The separate memory location is All members of the 'union' share
allotted to each member of the the same memory location.
'structure'.
Store Value Stores distinct values for all the Stores same value for all the
members. members.
Way of Viewing Provide single way to view each Provide multiple way to to view
memory location. same memory location.