0% found this document useful (0 votes)
13 views13 pages

C unit-IV

The document provides a comprehensive overview of pointers in C, including their definition, usage for inter-function communication, and the distinction between L-values and R-values. It also covers memory allocation functions, command-line arguments, and string manipulation functions, along with examples. Additionally, it explains the relationship between arrays and pointers, pointers to void, and pointers to functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

C unit-IV

The document provides a comprehensive overview of pointers in C, including their definition, usage for inter-function communication, and the distinction between L-values and R-values. It also covers memory allocation functions, command-line arguments, and string manipulation functions, along with examples. Additionally, it explains the relationship between arrays and pointers, pointers to void, and pointers to functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

St.

PAUL’S DEGREE & PG COLLEGE


(Affiliated to Osmania University)
Street No. 8, Himayathnagar, Hyderabad. Ph.No: 27602533

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

 Assigning address to pointer: To assign address to a pointer variable we use assignment


operator.
Syntax: pointerVariableName = &variableName;
Eg: ptr = &a;

 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;
}

L-value & R-value:


◆ An L-value (locator value) represents an object that occupies some identifiable location
in memory. L-value refers to memory location which identifies an object. l-value may
appear as either left hand or right hand side of an assignment operator (=). L-value often
represents as identifier. Expressions referring to modifiable locations are called
“modifiable L-values “.
◆ R-value refers to data value that is stored at some address in memory. A r-value is an
expression, that can’t have a value assigned to it, which means r-value can appear on
right but not on left hand side of an assignment operator (=).

2) Discuss about Memory Allocation functions and Command-Line Arguments?

Memory Allocation functions: The concept of dynamic memory allocation in c language


enables to allocate memory at runtime. Dynamic memory allocation in C language is
possible by 4 functions of <stdlib.h> header file.
➢ malloc( )
➢ calloc( )
➢ realloc( )
➢ free( )

❖ 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");
}

free(p); // Free allocated memory


return 0; // Return 0 for successful program execution
}

❖ 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);
}

❖ free( ): The memory occupied by malloc( ) or calloc( ) functions must be released by


calling free( ) function.
Syntax: free(ptr)

Command Line Arguments:


In C programming language, command line arguments are the data values that are passed
from command line to our program. Using command line arguments, we can control the
program execution from the outside of the program. Generally all the command line
arguments are handled by the main( ) method.
It contains two arguments (i) int argc
(ii) char *argv[ ]
➢ int argc: It stands for argument count. It gives the number of arguments that are passed
to the main( ) from command prompt.
➢ char *argv[ ]: A character array of pointers. If we want to store the corresponding args
addresses, then we have to use the argument. Strings are passed to the main( ) from
command prompt. It stands for argument vector[array]
//Eg:
#include<stdio.h>
main(int argc,char *argv[])
{
int i;
printf(“\n No of arguments are:%d”,argc);
for(i=0;i<argc;i++)
{
printf(“\n Argument[%d] is %s”,i,argv[i]);
}}
3) Explain about Pointers for inter functional 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;
}

4) Explain about Pointers Arithmetic Operations?


Pointer variable are used to store the address of variables. Address of any variable is an
unsigned integer value i.e, it is a numerical value. We can perform arithmetic operations on
pointer values. The result depends on the amount of memory required by the variable to
which the pointer is pointing.
➢ Addition operation on pointer: In C programming the addition operation on pointer
variable is calculated using the formula.
new_address= current_address + (number * size_of(data type))
//Eg:
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+3; //adding 3 to pointer variable
printf("After adding 3: Address of p variable is %u \n",p);
return 0; }

➢ Subtraction Operation on Pointer: In C programming language, the


subtraction operation on pointer variables is calculated using the formula.
new_address= current_address - (number * size_of(data type))
//Eg:
#include<stdio.h>
int main( ){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-3; //subtracting 3 from pointer variable
printf("After subtracting 3: Address of p variable is %u \n",p);
return 0; }

➢ Increment & Decrement Operation on pointer: The increment operation on pointer


variable is calculated as
new_address= current_address + i * size_of(data type)
//Eg:
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p);
return 0; }

The decrement operation on pointer variable is calculated as


new_address= current_address - i * size_of(data type)
//Eg:
#include <stdio.h>
void main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p-1;
printf("After decrement: Address of p variable is %u \n",p); }

➢ Comparison of pointers: The comparison operation is performing between the


pointers of same data type only. In C programming language we can use all
comparison operators with pointers.

5) What is String? Explain the C-Strings, String Input/Output functions and


string manipulation functions?
String is a set of characters that are enclosed in double quotes. In C programming strings are
created using one dimensional array of character datatype. Each string is terminated by
'\0' as indication of string termination.
The string handling functions are defined in a header file called <string.h>
 Creating String:
In C strings are created as a one-dimensional array of character datatype. We can use both
static and dynamic memory allocation. When we create a string the size of the array must be
more than the actual number of characters to be stored. That extra memory block is used to
store string termination character NULL(\0).
Eg: char str[6];

 Assigning string value: String value is assigned using two methods


⚫ At the time of declaration(initialization).
⚫ After declaration.

 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); }

String Input/Output functions :


C language provides lot of string functions for manipulating the strings. All the string
functions are available in <string.h> header file. String functions are:

➢ 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);
}

➢ int strcmp (string 1, string2):This function compares two strings passed as


parameters and returns either +ve number,0,-ve number.
+ve value indicates string1 > string2.
0 indicates string1 and string2 are equal
-ve value indicates string1 < string2.
Example :
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="spark",string2[]="programming";
int cmp;
cmp=strcmp(string1,string2);
if(cmp>0)
printf("%s > %s",string1,string2);
else
{
if(cmp<0)
printf("%s < %s",string1,string2);
else
printf("%s = %s",string1,string2);
}}

➢ 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);
}

➢ strcpy (Destination string,source string):This function accepts 2 strings as


parameter,1st one is destination string and 2nd is source string. This function copies
source string to destination string.
Example :
#include <stdio.h>
#include <string.h>
void main(void)
{
char src[]="spark",dest[15];
strcpy(dest,src);
printf("%s is copied to dest string\t",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);
}

String Manipulation functions:


C supports a string handling library which provides useful functions that can be used for
string manipulation. All these strings handling functions are defined in the header file
<string.h>. Some of the major string handling functions are
6) Explain about Arrays and Pointers, Pointers to void, Pointers to functions?

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;
}

You might also like