C unit-IV
C unit-IV
UNIT-IV
1) What is Pointers? Explain about Pointers for Inter function commuication and L-
value and R-value?
Pointers: The pointer in C language is a variable which stores the address of another
variable. This variable can be of type int, char, array, function, or any other pointer. The size
of the pointer depends on the architecture.
Accessing: In C programming language, we use the reference operator “&” to access the
address of variable.
Eg: printf(“Address: %u”,&marks);
Declaring pointers: The pointer in c language can be declared using * (asterisk symbol).
It is also known as indirection pointer used to dereference a pointer.
Syntax: datatype *pointerName;
Eg: int *a;//pointer to int
Accessing variable value using pointer: Pointer variable are used to store the address of
other variables. We can use this address to access the value of the variable through its
pointer. We use the symbol “*” in front of pointer variable name to access the value of
variable to which the pointer is pointing.
Syntax: *pointerVariableName;
//Eg:
#include<stdio.h>
int main( ){
int number=50;
int *p;
p=&number;//stores the address of number variable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
Pointers for Inter function communication:
✓ We know that functions can be called by value and called by reference.
✓ If the actual parameter should not change in called function, pass the parameter-by value.
✓ If the value of actual parameter should get changed in called function, then use pass-by
reference.
✓ If the function has to return more than one value, return these values indirectly by using
call-by-reference.
//Eg:
#include<stdio.h>
void main() {
void area(int,int*,int*);
int r;
float a,p;
printf("enter radius of circle:");
scanf("%d",&r);
area(r,&a,&p);
printf("area=%f",a);
printf("perimeter=%f",p);
}
void area(int x,int *p,int *q) {
*p=3.14*x*x;
*q=2 * 3.14*x;
}
❖ malloc( ): The malloc( ) function allocates single block of requested memory. It doesn’t
initialize memory at execution time, so it has garbage value initially. It returns NULL if
memory is not sufficient.
Syntax: ptr=(cast-type*)malloc(byte-size)
//Eg:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int *p = (int *)malloc(5 * sizeof(int));
if (p == NULL)
printf("Memory is not allocated\n");
else {
printf("Enter elements:\n");
for (i = 0; i < 5; i++)
scanf("%d", p + i);
printf("Elements are:\n");
for (i = 0; i < 5; i++)
printf("%d\t", *(p + i));
printf("\n");
}
❖ calloc( ): The calloc( ) function allocates multiple block of requested memory. It initially
initialize all bytes to zero. It returns NULL if memory is not sufficient.
Syntax: ptr=(cast-type*)calloc(number, byte-size)
Eg:
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
int *p = (int *) calloc(5, sizeof(int));
if (p == NULL) {
printf("Memory is not sufficient\n");
} else {
printf("Enter elements:\n");
for (i = 0; i < 5; i++) {
scanf("%d", p + i);
}
printf("Elements are:\n");
for(i=0;i<5;i++)
{
❖ realloc( ): If memory is not sufficient for malloc( ) or calloc( ) you can reallocate the
memory by realloc( ) function.
Syntax: ptr=realloc(ptr, new-size)
//Eg:
#include<stdio.h>
#include<stdlib.h>
main( ){
int *p=(int *)malloc(6);
int i;
*p=10;
*(p+1)=20;
*(p+2)=30;
realloc(p,10);
*(p+3)=40;
*(p+4)=40;
for(i=0;i<5;i++)
printf(“%d\t”,*(p+i));
free(p);
}
Reading string value: We can read a string value from the user during the program
execution.
⚫ Using scanf( ) method--reads single word
⚫ Using gets( ) method -- reads a lien of text.
//Eg:
way 1:
void main()
{
char name[30];
printf("Enter your name");
scanf("%s", name); //format specifier
printf("%s", name); }
way 2:
void main()
{
char name[30];
printf("Enter your name");
gets(name); //in-built function
puts(name); }
➢ int strlen(char array):This function accepts string as parameter and return integer i.e
the length of String passed to it.
Example:
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[]="spark";
int len;
len=strlen(string);
printf("length of %s is %d\t", string, len);
}
➢ strupr (string):This function accepts single string that can be in any case(lower or
upper).It converts the string in upper case.
Example :
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="SPArk";
strupr(string1);
printf("%s is in upper case",string1);
}
➢ strlwr (string):This function accepts single string that can be in any case(lower or
upper).It converts the string in lower case.
Example :
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="SPArk";
strlwr(string1);
printf("%s is in lower case",string1);
}
➢ strcat (Destination string,source string): This function accepts two strings source
string is appended to the destination string.
Example:
#include <stdio.h>
#include <string.h>
void main(void)
{
char src[]="spark",dest[]="programming";
strcat(dest,src);
printf("concatenated string is %s",dest);
}
➢ strrev (string):This function accepts single string as parameter and reverse that string.
Example :
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[]="spark";
strrev(string);
printf("reverse string is %s",string);
}
Arrays:
➢ An array is a data structure that represents a collection of elements of the same type
stored in contiguous memory locations. It provides a way to store and access multiple
values of the same data type using a single variable name.
➢ It stores a homogeneous collection of data.
➢ Its size can be decided by the programmer.
➢ It provides random access.
➢ The array is one of the most used data types in many programming languages due to its
simplicity and capability.
Pointers:
➢ A pointer is a variable that holds the memory address of another variable. Pointers can be
used for storing addresses of dynamically allocated arrays and for arrays that are passed
as arguments to functions.
➢ This size of the pointer is fixed and only depends upon the architecture of the system.
➢ Pointers are one of the core concepts that provide a lot of unique and useful features in
our program.
Array VS Pointers
Pointers to void:
❖ In C programming, pointer to void is the concept of defining a pointer variable that
is independent of datatype.
❖ A void pointer is a pointer variable used to store the address of a variable of any
datatype.
❖ It means single void pointer ca be used to store the address of integer variable , float
variable ,character variable or any structure variable.
❖ “void” keyword is used to create void pointer.
Syntax: void *pointerName;
Eg: void *ptr;
Here, ptr is a void pointer which is used to store the address of any datatype
variable.
//Eg:
#include<stdio.h>
#include<conio.h>
int main( ){
clrscr( );
int a;
float b;
char c;
void *ptr;
ptr=&a;
printf(“Address if integer variable ‘a’=%u\n”,ptr);
ptr=&b;
printf(“Adress of float variable = ‘b’=%u\n”,ptr);
ptr = &c;
printf(“Address of character variable ‘c’=%u\n”,ptr);
return 0;}
Pointers to functions:
➢ In C programming there are two ways to pass parameters to functions. They are Call by
Value & Call by Reference.
➢ In case of Call by reference parameter passing method, the address of actual parameters
is passed as arguments from the calling function to the called function. To receive
address we use pointer variable is as formal parameters.
//Eg:
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main( ){
int a=10,b=20;
clrscr( );
printf(“Before swap:a= %d and b= %d\n”,a,b);
swap(&a,&b);
printf(“After swap:a=%d and b = %d\n”a,b);
getch( );}
void swap(int *x,int *y){
int temp;
temp=*x;
*x=*y;
*y=temp;
}