Question Bank
UNIT 8
- Explain pointer with example.
Question Bank
UNIT 8
- Explain pointer with example.
Answer:
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. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
- Int n = 10;
- Int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
- Int *a;//pointer to int
- Char *c;//pointer to char
Pointer Example
An example of using pointers to print the address and value is given below.
As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
Let's see the pointer example as explained for the above figure.
- #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); // p contains the address of the number therefore printing p gives the address of number.
- Printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
- Return 0;
- }
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
2. Write Pointer Program to swap two numbers without using the 3rd variable.
Answer:
- #include<stdio.h>
- Int main(){
- Int a=10,b=20,*p1=&a,*p2=&b;
- Printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
- *p1=*p1+*p2;
- *p2=*p1-*p2;
- *p1=*p1-*p2;
- Printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
- Return 0;
- }
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
3. How to read the pointer: int (*p)[10].
Answer:
To read the pointer, we must see that () and [] have the equal precedence. Therefore, their associativity must be considered here. The associativity is left to right, so the priority goes to ().
Inside the bracket (), pointer operator * and pointer name (identifier) p have the same precedence. Therefore, their associativity must be considered here which is right to left, so the priority goes to p, and the second priority goes to *.
Assign the 3rd priority to [] since the data type has the last precedence. Therefore the pointer will look like following.
- Char -> 4
- * -> 2
- p -> 1
- [10] -> 3
The pointer will be read as p is a pointer to an array of integers of size 10.
Example
How to read the following pointer?
- Int (*p)(int (*)[2], int (*)void))
Explanation
This pointer will be read as p is a pointer to such function which accepts the first parameter as the pointer to a one-dimensional array of integers of size two and the second parameter as the pointer to a function which parameter is void and return type is the integer.
4. Explain declaration and initialization of pointer.
Answer:
Declaring a pointer
Like variables, pointers have to be declared before they can be used in your program. Pointers can be named anything you want as long as they obey C's naming rules. A pointer declaration has the following form.
Data_type * pointer_variable_name;
Here,
- Data_type is the pointer's base type of C's variable types and indicates the type of the variable that the pointer points to.
- The asterisk (*: the same asterisk used for multiplication) which is indirection operator, declares a pointer.
Let's see some valid pointer declarations
Int *ptr_thing; /* pointer to an integer */
Int *ptr1,thing;/* ptr1 is a pointer to type integer and thing is an integer variable */
Double *ptr2; /* pointer to a double */
Float *ptr3; /* pointer to a float */
Char *ch1 ; /* pointer to a character */
Float *ptr, variable;/*ptr is a pointer to type float and variable is an ordinary float variable */
Initialize a pointer
After declaring a pointer, we initialize it like standard variables with a variable address. If pointers are not uninitialized and used in the program, the results are unpredictable and potentially disastrous.
To get the address of a variable, we use the ampersand (&)operator, placed before the name of a variable whose address we need. Pointer initialization is done with the following syntax.
Pointer = &variable;
A simple program for pointer illustration is given below:
#include <stdio.h>
Int main()
{
Int a=10; //variable declaration
Int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
Printf("Address stored in a variable p is:%x\n",p); //accessing the address
Printf("Value stored in a variable p is:%d\n",*p); //accessing the value
Return 0;
}
Output:
Address stored in a variable p is:60ff08
Value stored in a variable p is:10
5. What are different types of a pointer?
Answer:
Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
Int main()
{
Int *p = NULL; //null pointer
Printf(“The value inside variable p is:\n%x”,p);
Return 0;
}
Output:
The value inside variable p is:
0
Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.
Following program illustrates the use of a void pointer:
#include <stdio.h>
Int main()
{
Void *p = NULL; //void pointer
Printf("The size of pointer is:%d\n",sizeof(p));
Return 0;
}
Output:
The size of pointer is:4
Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
Int main()
{
Int *p; //wild pointer
Printf("\n%d",*p);
Return 0;
}
Output
Timeout: the monitored command dumped core
Sh: line 1: 95298 Segmentation fault timeout 10s main
Other types of pointers in 'c' are as follows:
- Dangling pointer
- Complex pointer
- Near pointer
- Far pointer
- Huge pointer
6. What are Direct and Indirect Access Pointers?
Answer:
In C, there are two equivalent ways to access and manipulate a variable content
- Direct access: we use directly the variable name
- Indirect access: we use a pointer to the variable
Let's understand this with the help of program below
#include <stdio.h>
/* Declare and initialize an int variable */
Intvar = 1;
/* Declare a pointer to int */
Int *ptr;
Int main( void )
{
/* Initialize ptr to point to var */
Ptr = &var;
/* Access var directly and indirectly */
Printf("\nDirect access, var = %d", var);
Printf("\nIndirect access, var = %d", *ptr);
/* Display the address of var two ways */
Printf("\n\nThe address of var = %d", &var);
Printf("\nThe address of var = %d\n", ptr);
/*change the content of var through the pointer*/
*ptr=48;
Printf("\nIndirect access, var = %d", *ptr);
Return 0;}
After compiling the program without any errors, the result is:
Direct access, var = 1
Indirect access, var = 1
The address of var = 4202496
The address of var = 4202496
Indirect access, var = 48
7. What is Priority operation (precedence)?
Answer:
When working with pointers, we must observe the following priority rules:
- The operators * and & have the same priority as the unary operators (the negation!, the incrementation++, decrement--).
- In the same expression, the unary operators *, &,!, ++, - are evaluated from right to left.
If a P pointer points to an X variable, then * P can be used wherever X can be written.
The following expressions are equivalent:
Int X =10 | |
Expression | Equivalent Expression |
Y=*P+1 | Y=X+1 |
In the latter case, parentheses are needed: as the unary operators * and ++ are evaluated from right to left, without the parentheses the pointer P would be incremented, not the object on which P points.
Below table shows the arithmetic and basic operation that can be used when dealing with pointers.
Operation | Explanation |
Assignment | Int *P1,*P2 P1=P2; P1 and P2 point to the same integer variable |
Incrementation and decrementation | Int *P1; P1++;P1-- ; |
Adding an offset (Constant) | This allows the pointer to move N elements in a table. The pointer will be increased or decreased by N times the number of byte (s) of the type of the variable. P1+5; |
8. What are the advantages and disadvantages of pointers?
Answer:
Advantages of Pointers
- Pointers are useful for accessing memory locations.
- Pointers provide an efficient way for accessing the elements of an array structure.
- Pointers are used for dynamic memory allocation as well as deallocation.
- Pointers are used to form complex data structures such as linked list, graph, tree, etc.
Disadvantages of Pointers
- Pointers are a little complex to understand.
- Pointers can lead to various errors such as segmentation faults or can access a memory location which is not required at all.
- If an incorrect value is provided to a pointer, it may cause memory corruption.
- Pointers are also responsible for memory leakage.
- Pointers are comparatively slower than that of the variables.
- Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully.
Summary
- A pointer is nothing but a memory location where data is stored.
- A pointer is used to access the memory location.
- There are various types of pointers such as a null pointer, wild pointer, void pointer and other types of pointers.
- Pointers can be used with array and string to access elements more efficiently.
- We can create function pointers to invoke a function dynamically.
- Arithmetic operations can be done on a pointer which is known as pointer arithmetic.
- Pointers can also point to function which make it easy to call different functions in the case of defining an array of pointers.
- When you want to deal different variable data type, you can use a typecast void pointer.
9. Explain Pointers and Strings.
Answer:
A string is an array of char objects, ending with a null character '\ 0'. We can manipulate strings using pointers. Here is an example that explains this section
#include <stdio.h>
#include <string.h>
Int main()
{
Charstr[]="Hello Guru99!";
Char *p;
p=str;
Printf("First character is:%c\n",*p);
p =p+1;
Printf("Next character is:%c\n",*p);
Printf("Printing all the characters in a string\n");
p=str; //reset the pointer
For(inti=0;i<strlen(str);i++)
{
Printf("%c\n",*p);
p++;
}
Return 0;
}
Output
First character is:H
Next character is:e
Printing all the characters in a string
H
e
l
l
o
G
u
r
u
9
9
!
Another way to deal strings is with an array of pointers like in the following program:
#include <stdio.h>
Int main(){
Char *materials[ ] = { "iron", "copper", "gold"};
Printf("Please remember these materials :\n");
Inti ;
For (i = 0; i< 3; i++) {
Printf("%s\n", materials[ i ]);}
Return 0;}
Output:
Please remember these materials:
Iron
Copper
Gold
10. Explain Notion of linked list with example.
Answer:
A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.
Implementation in C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
Struct node {
Int data;
Int key;
Struct node *next;
};
Struct node *head = NULL;
Struct node *current = NULL;
//display the list
VoidprintList(){
Struct node *ptr= head;
Printf("\n[ ");
//start from the beginning
While(ptr!= NULL){
Printf("(%d,%d) ",ptr->key,ptr->data);
Ptr=ptr->next;
}
Printf(" ]");
}
//insert link at the first location
VoidinsertFirst(int key,int data){
//create a link
Struct node *link =(struct node*)malloc(sizeof(struct node));
Link->key = key;
Link->data = data;
//point it to old first node
Link->next= head;
//point first to new first node
Head= link;
}
//delete first item
Struct node*deleteFirst(){
//save reference to first link
Struct node *tempLink= head;
//mark next to first link as first
Head= head->next;
//return the deleted link
ReturntempLink;
}
//is list empty
BoolisEmpty(){
Return head == NULL;
}
Int length(){
Int length =0;
Struct node *current;
For(current = head; current != NULL; current = current->next){
Length++;
}
Return length;
}
//find a link with given key
Struct node* find(int key){
//start from the first link
Struct node* current = head;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//go to next link
Current= current->next;
}
}
//if data found, return the current Link
Return current;
}
//delete a link with given key
Struct node*delete(int key){
//start from the first link
Struct node* current = head;
Struct node* previous = NULL;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//store reference to current link
Previous= current;
//move to next link
Current= current->next;
}
}
//found a match, update the link
If(current == head){
//change first to point to next link
Head= head->next;
}else{
//bypass the current link
Previous->next= current->next;
}
Return current;
}
Void sort(){
Inti, j, k,tempKey,tempData;
Struct node *current;
Struct node *next;
Int size = length();
k =size ;
For(i=0;i< size -1;i++, k--){
Current= head;
Next= head->next;
For( j =1; j < k ;j++){
If( current->data >next->data ){
TempData= current->data;
Current->data =next->data;
Next->data =tempData;
TempKey= current->key;
Current->key =next->key;
Next->key =tempKey;
}
Current= current->next;
Next=next->next;
}
}
}
Void reverse(struct node**head_ref){
Struct node*prev= NULL;
Struct node* current =*head_ref;
Struct node*next;
While(current != NULL){
Next= current->next;
Current->next=prev;
Prev= current;
Current=next;
}
*head_ref=prev;
}
Void main(){
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("Original List: ");
//print list
PrintList();
While(!isEmpty()){
Struct node *temp =deleteFirst();
Printf("\nDeleted value:");
Printf("(%d,%d) ",temp->key,temp->data);
}
Printf("\nList after deleting all items: ");
PrintList();
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("\nRestored List: ");
PrintList();
Printf("\n");
Struct node *foundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Delete(4);
Printf("List after deleting an item: ");
PrintList();
Printf("\n");
FoundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Printf("\n");
Sort();
Printf("List after sorting the data: ");
PrintList();
Reverse(&head);
Printf("\nList after reversing the data: ");
PrintList();
}
If we compile and run the above program, it will produce the following result −
Output
Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[ ]
Restored List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Element found: (4,1)
List after deleting an item:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Element not found.
List after sorting the data:
[ (1,10) (2,20) (3,30) (5,40) (6,56) ]
List after reversing the data:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Question Bank
UNIT 8
- Explain pointer with example.
Answer:
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. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
- Int n = 10;
- Int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to dereference a pointer.
- Int *a;//pointer to int
- Char *c;//pointer to char
Pointer Example
An example of using pointers to print the address and value is given below.
As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
Let's see the pointer example as explained for the above figure.
- #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); // p contains the address of the number therefore printing p gives the address of number.
- Printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
- Return 0;
- }
Output
Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50
2. Write Pointer Program to swap two numbers without using the 3rd variable.
Answer:
- #include<stdio.h>
- Int main(){
- Int a=10,b=20,*p1=&a,*p2=&b;
- Printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
- *p1=*p1+*p2;
- *p2=*p1-*p2;
- *p1=*p1-*p2;
- Printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
- Return 0;
- }
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
3. How to read the pointer: int (*p)[10].
Answer:
To read the pointer, we must see that () and [] have the equal precedence. Therefore, their associativity must be considered here. The associativity is left to right, so the priority goes to ().
Inside the bracket (), pointer operator * and pointer name (identifier) p have the same precedence. Therefore, their associativity must be considered here which is right to left, so the priority goes to p, and the second priority goes to *.
Assign the 3rd priority to [] since the data type has the last precedence. Therefore the pointer will look like following.
- Char -> 4
- * -> 2
- p -> 1
- [10] -> 3
The pointer will be read as p is a pointer to an array of integers of size 10.
Example
How to read the following pointer?
- Int (*p)(int (*)[2], int (*)void))
Explanation
This pointer will be read as p is a pointer to such function which accepts the first parameter as the pointer to a one-dimensional array of integers of size two and the second parameter as the pointer to a function which parameter is void and return type is the integer.
4. Explain declaration and initialization of pointer.
Answer:
Declaring a pointer
Like variables, pointers have to be declared before they can be used in your program. Pointers can be named anything you want as long as they obey C's naming rules. A pointer declaration has the following form.
Data_type * pointer_variable_name;
Here,
- Data_type is the pointer's base type of C's variable types and indicates the type of the variable that the pointer points to.
- The asterisk (*: the same asterisk used for multiplication) which is indirection operator, declares a pointer.
Let's see some valid pointer declarations
Int *ptr_thing; /* pointer to an integer */
Int *ptr1,thing;/* ptr1 is a pointer to type integer and thing is an integer variable */
Double *ptr2; /* pointer to a double */
Float *ptr3; /* pointer to a float */
Char *ch1 ; /* pointer to a character */
Float *ptr, variable;/*ptr is a pointer to type float and variable is an ordinary float variable */
Initialize a pointer
After declaring a pointer, we initialize it like standard variables with a variable address. If pointers are not uninitialized and used in the program, the results are unpredictable and potentially disastrous.
To get the address of a variable, we use the ampersand (&)operator, placed before the name of a variable whose address we need. Pointer initialization is done with the following syntax.
Pointer = &variable;
A simple program for pointer illustration is given below:
#include <stdio.h>
Int main()
{
Int a=10; //variable declaration
Int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
Printf("Address stored in a variable p is:%x\n",p); //accessing the address
Printf("Value stored in a variable p is:%d\n",*p); //accessing the value
Return 0;
}
Output:
Address stored in a variable p is:60ff08
Value stored in a variable p is:10
5. What are different types of a pointer?
Answer:
Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This method is useful when you do not have any address assigned to the pointer. A null pointer always contains value 0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
Int main()
{
Int *p = NULL; //null pointer
Printf(“The value inside variable p is:\n%x”,p);
Return 0;
}
Output:
The value inside variable p is:
0
Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any standard data type. A void pointer is created by using the keyword void. It can be used to store an address of any variable.
Following program illustrates the use of a void pointer:
#include <stdio.h>
Int main()
{
Void *p = NULL; //void pointer
Printf("The size of pointer is:%d\n",sizeof(p));
Return 0;
}
Output:
The size of pointer is:4
Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of pointers are not efficient because they may point to some unknown memory location which may cause problems in our program and it may lead to crashing of the program. One should always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
Int main()
{
Int *p; //wild pointer
Printf("\n%d",*p);
Return 0;
}
Output
Timeout: the monitored command dumped core
Sh: line 1: 95298 Segmentation fault timeout 10s main
Other types of pointers in 'c' are as follows:
- Dangling pointer
- Complex pointer
- Near pointer
- Far pointer
- Huge pointer
6. What are Direct and Indirect Access Pointers?
Answer:
In C, there are two equivalent ways to access and manipulate a variable content
- Direct access: we use directly the variable name
- Indirect access: we use a pointer to the variable
Let's understand this with the help of program below
#include <stdio.h>
/* Declare and initialize an int variable */
Intvar = 1;
/* Declare a pointer to int */
Int *ptr;
Int main( void )
{
/* Initialize ptr to point to var */
Ptr = &var;
/* Access var directly and indirectly */
Printf("\nDirect access, var = %d", var);
Printf("\nIndirect access, var = %d", *ptr);
/* Display the address of var two ways */
Printf("\n\nThe address of var = %d", &var);
Printf("\nThe address of var = %d\n", ptr);
/*change the content of var through the pointer*/
*ptr=48;
Printf("\nIndirect access, var = %d", *ptr);
Return 0;}
After compiling the program without any errors, the result is:
Direct access, var = 1
Indirect access, var = 1
The address of var = 4202496
The address of var = 4202496
Indirect access, var = 48
7. What is Priority operation (precedence)?
Answer:
When working with pointers, we must observe the following priority rules:
- The operators * and & have the same priority as the unary operators (the negation!, the incrementation++, decrement--).
- In the same expression, the unary operators *, &,!, ++, - are evaluated from right to left.
If a P pointer points to an X variable, then * P can be used wherever X can be written.
The following expressions are equivalent:
Int X =10 | |
Expression | Equivalent Expression |
Y=*P+1 | Y=X+1 |
In the latter case, parentheses are needed: as the unary operators * and ++ are evaluated from right to left, without the parentheses the pointer P would be incremented, not the object on which P points.
Below table shows the arithmetic and basic operation that can be used when dealing with pointers.
Operation | Explanation |
Assignment | Int *P1,*P2 P1=P2; P1 and P2 point to the same integer variable |
Incrementation and decrementation | Int *P1; P1++;P1-- ; |
Adding an offset (Constant) | This allows the pointer to move N elements in a table. The pointer will be increased or decreased by N times the number of byte (s) of the type of the variable. P1+5; |
8. What are the advantages and disadvantages of pointers?
Answer:
Advantages of Pointers
- Pointers are useful for accessing memory locations.
- Pointers provide an efficient way for accessing the elements of an array structure.
- Pointers are used for dynamic memory allocation as well as deallocation.
- Pointers are used to form complex data structures such as linked list, graph, tree, etc.
Disadvantages of Pointers
- Pointers are a little complex to understand.
- Pointers can lead to various errors such as segmentation faults or can access a memory location which is not required at all.
- If an incorrect value is provided to a pointer, it may cause memory corruption.
- Pointers are also responsible for memory leakage.
- Pointers are comparatively slower than that of the variables.
- Programmers find it very difficult to work with the pointers; therefore it is programmer's responsibility to manipulate a pointer carefully.
Summary
- A pointer is nothing but a memory location where data is stored.
- A pointer is used to access the memory location.
- There are various types of pointers such as a null pointer, wild pointer, void pointer and other types of pointers.
- Pointers can be used with array and string to access elements more efficiently.
- We can create function pointers to invoke a function dynamically.
- Arithmetic operations can be done on a pointer which is known as pointer arithmetic.
- Pointers can also point to function which make it easy to call different functions in the case of defining an array of pointers.
- When you want to deal different variable data type, you can use a typecast void pointer.
9. Explain Pointers and Strings.
Answer:
A string is an array of char objects, ending with a null character '\ 0'. We can manipulate strings using pointers. Here is an example that explains this section
#include <stdio.h>
#include <string.h>
Int main()
{
Charstr[]="Hello Guru99!";
Char *p;
p=str;
Printf("First character is:%c\n",*p);
p =p+1;
Printf("Next character is:%c\n",*p);
Printf("Printing all the characters in a string\n");
p=str; //reset the pointer
For(inti=0;i<strlen(str);i++)
{
Printf("%c\n",*p);
p++;
}
Return 0;
}
Output
First character is:H
Next character is:e
Printing all the characters in a string
H
e
l
l
o
G
u
r
u
9
9
!
Another way to deal strings is with an array of pointers like in the following program:
#include <stdio.h>
Int main(){
Char *materials[ ] = { "iron", "copper", "gold"};
Printf("Please remember these materials :\n");
Inti ;
For (i = 0; i< 3; i++) {
Printf("%s\n", materials[ i ]);}
Return 0;}
Output:
Please remember these materials:
Iron
Copper
Gold
10. Explain Notion of linked list with example.
Answer:
A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.
Implementation in C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
Struct node {
Int data;
Int key;
Struct node *next;
};
Struct node *head = NULL;
Struct node *current = NULL;
//display the list
VoidprintList(){
Struct node *ptr= head;
Printf("\n[ ");
//start from the beginning
While(ptr!= NULL){
Printf("(%d,%d) ",ptr->key,ptr->data);
Ptr=ptr->next;
}
Printf(" ]");
}
//insert link at the first location
VoidinsertFirst(int key,int data){
//create a link
Struct node *link =(struct node*)malloc(sizeof(struct node));
Link->key = key;
Link->data = data;
//point it to old first node
Link->next= head;
//point first to new first node
Head= link;
}
//delete first item
Struct node*deleteFirst(){
//save reference to first link
Struct node *tempLink= head;
//mark next to first link as first
Head= head->next;
//return the deleted link
ReturntempLink;
}
//is list empty
BoolisEmpty(){
Return head == NULL;
}
Int length(){
Int length =0;
Struct node *current;
For(current = head; current != NULL; current = current->next){
Length++;
}
Return length;
}
//find a link with given key
Struct node* find(int key){
//start from the first link
Struct node* current = head;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//go to next link
Current= current->next;
}
}
//if data found, return the current Link
Return current;
}
//delete a link with given key
Struct node*delete(int key){
//start from the first link
Struct node* current = head;
Struct node* previous = NULL;
//if list is empty
If(head == NULL){
Return NULL;
}
//navigate through list
While(current->key != key){
//if it is last node
If(current->next== NULL){
Return NULL;
}else{
//store reference to current link
Previous= current;
//move to next link
Current= current->next;
}
}
//found a match, update the link
If(current == head){
//change first to point to next link
Head= head->next;
}else{
//bypass the current link
Previous->next= current->next;
}
Return current;
}
Void sort(){
Inti, j, k,tempKey,tempData;
Struct node *current;
Struct node *next;
Int size = length();
k =size ;
For(i=0;i< size -1;i++, k--){
Current= head;
Next= head->next;
For( j =1; j < k ;j++){
If( current->data >next->data ){
TempData= current->data;
Current->data =next->data;
Next->data =tempData;
TempKey= current->key;
Current->key =next->key;
Next->key =tempKey;
}
Current= current->next;
Next=next->next;
}
}
}
Void reverse(struct node**head_ref){
Struct node*prev= NULL;
Struct node* current =*head_ref;
Struct node*next;
While(current != NULL){
Next= current->next;
Current->next=prev;
Prev= current;
Current=next;
}
*head_ref=prev;
}
Void main(){
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("Original List: ");
//print list
PrintList();
While(!isEmpty()){
Struct node *temp =deleteFirst();
Printf("\nDeleted value:");
Printf("(%d,%d) ",temp->key,temp->data);
}
Printf("\nList after deleting all items: ");
PrintList();
InsertFirst(1,10);
InsertFirst(2,20);
InsertFirst(3,30);
InsertFirst(4,1);
InsertFirst(5,40);
InsertFirst(6,56);
Printf("\nRestored List: ");
PrintList();
Printf("\n");
Struct node *foundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Delete(4);
Printf("List after deleting an item: ");
PrintList();
Printf("\n");
FoundLink= find(4);
If(foundLink!= NULL){
Printf("Element found: ");
Printf("(%d,%d) ",foundLink->key,foundLink->data);
Printf("\n");
}else{
Printf("Element not found.");
}
Printf("\n");
Sort();
Printf("List after sorting the data: ");
PrintList();
Reverse(&head);
Printf("\nList after reversing the data: ");
PrintList();
}
If we compile and run the above program, it will produce the following result −
Output
Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[ ]
Restored List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) (1,10) ]
Element found: (4,1)
List after deleting an item:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]
Element not found.
List after sorting the data:
[ (1,10) (2,20) (3,30) (5,40) (6,56) ]
List after reversing the data:
[ (6,56) (5,40) (3,30) (2,20) (1,10) ]