Ad
Friday, March 17, 2023
Pointers : FYBCS : Semester II : PYQS
1 mark
--Q. What is pointer variable? Give example.
Answer: A pointer variable is a variable which stores memory address of another variable of same type.
For example:
int *ptr; // Declare a pointer variable named ptr
int num = 10;
ptr = # // Store the address of num in ptr
Here the integer type pointer variable 'ptr' stores the address of another integer type variable 'num'.
Q. What is the use of * operator?
Answer: * operator is also known as indirection operator. When it is used with pointer variable, it refers to variable being pointed to and provides contents in memory location specified by the pointer. * is used for dereferencing of pointers.
Q. What is formal parameter?
Answer: A formal parameter refers to the variables declared in the function definition that are used to receive the values of arguments passed to the function during function call.
for example: int sum(int a, int b)
{
int c = a + b;
return c;
}
--Q.What is dynamic memory allocation?
Answer: Dynamic memory allocation is the process of allocating memory for a variable or data structure at runtime instead of at compile time.
Q. While passing array to a function, address of first element of array is passed.State true or false.
Answer: True. When an array is passed to a function in C, the address of the first element of the array is passed as a pointer to the function. This is because the name of an array is a pointer to the first element of the array.
Q. What is the meaning of & and * operators in pointer ?
Answer: In C programming, the & operator is used to get the address of a variable, while the * operator is used to declare and dereference pointers. The & operator can be used to obtain the memory address of a variable, which can then be assigned to a pointer variable. The * operator is used to declare a pointer variable, and to access the value that is stored in the memory address pointed to by the pointer variable.
2 mark
Q. Differentiate between static & dynamic memory allocation.
Sr.No. | Static Memory Allocation | Dynamic Memory Allocation |
---|---|---|
1 | Memory is allocated compile time. | Memory is allocated run time. |
2 | Memory size cannot be modified while execution. | Memory size can be modified while execution. |
3 | More memory space and memory wastage. | Less memory space and no memory wastage. |
4 | Fater execution. | Slow execution. |
5 | Example: Array. | Example: Linked List. |
4 mark
Q. Explain various operations possible on pointers.
Answer: Operations that can be performed on pointers are:
1. Incrementing or Decrementing a pointer (++ or --): After Incrementing or Decrementing a pointer variable it points to next or previous memory location of its type.
Example: int i=20, *ptr;
ptr= &i;
ptr++;
Here ptr++ will increment the integer pointer by two bytes.
2. Addition or Subtraction of a constant number to a pointer (+ or -): We can add or subtract any constant integer number to pointer variable.
example:
int *ptr,n ;
ptr= &n;
ptr= ptr + 3;
Suppose ptr contains address 1000, then after addition, it's address will be 1006.
3*2=6 times addition will happen since size of integer variable is 2 bytes.
3. Differencing pointers or Subtraction of one pointer from another: It gives total number of elements between them.
example: Suppose value of ptr1=1000 and ptr2=1004.
ptr2-ptr1
=1004-1000
=4
Final result = 4/size of integer = 4/2= 2.
4. Comparison of two pointers: It is valid only if two pointers are pointing to same array. The result is true if both pointers point to the same location in the memory.
example: if a and b are pointing to same location 1000.
Then , a==b will be True.
-Q. Explain the concept of pointer to pointer with example.
Answer: A pointer to pointer (or double pointer) is a pointer variable that stores the address of another pointer variable.
The first pointer is used to store the address of the variable and the second pointer is used to store the address of the first pointer.
It's syntax is : int **ptr;
for example:
int var=789;
int *ptr2;
int *ptr1;
ptr2=var;
ptr1=&ptr2;
Here ptr2 stores the value of var variable and ptr1 stores the value of ptr2 pointer.
Q. Explain malloc and calloc functions with example.
Answer:
Malloc() Function: It is used to allocate memory dynamically. It carries a garbage value as the memory is allocated.
It is generally used as:
ptr= (data_type *)malloc*(specified_size);
for example:
int *x;
x=(int *)malloc(50*sizeof(int));
Here memory space is allocated to variable x. We are allocating a block of memory that can hold 50 integers. 'x' can be used to access allocated block of memory.
Calloc() Function: It is used to dynamically allocate the specified number of blocks of memory of the specified type. It takes two arguments, the first one specifies the number of blocks and the second one specifies the size of each block. This function initialized allocated memory to zero.
It is generally used as:
ptr=(data_type *)calloc(number of blocks,specified_size)
example:
int*ptr;
ptr2 = (int *)calloc(5, sizeof(int));
Here we are allocating memory for an array of 5 integers.
-Q. Write short notes on the following with example :
(a) Call by value (b) Call by reference.
Answer: (a) Call by value : In this method the original value is copied into the function as arguments. Changes made to the formal parameter inside the function have no effect on actualy argument and both will be created at different memory locations. It is therefore restricted to a one-way transfer of information.
(b) Call by reference : In this method, the address of arguments is copied into the function as an argument. Changes made to the formal parameter affect the actual argument because address is used to access the actual argument. Both actual and formal arguments will be created in same memory location for which it requires pointer concept. It is therefore a two-way transfer of information.
Programs [4 mark each]
Q. Write a ‘C’ program to calculate area, & perimeter and diameter of circle using one function for all & use pointers.
Answer:
#include <stdio.h>
void main() {
float radius, area, perimeter, diameter;
float *ptr_area, *ptr_perimeter, *ptr_diameter;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
ptr_area = &area;
ptr_perimeter = &perimeter;
ptr_diameter = &diameter;
*ptr_area = 3.14159 * radius * radius;
*ptr_perimeter = 2 * 3.14159 * radius;
*ptr_diameter = 2 * radius;
printf("Area of the circle: %.2f\n", *ptr_area);
printf("Perimeter of the circle: %.2f\n", *ptr_perimeter);
printf("Diameter of the circle: %.2f\n", *ptr_diameter);
}
Q. Write a ‘C’ program to accept a time from user as hh:mm:ss & check the validity of it. If it is invalid, validate it. Use pointer to structure.
Answer:
#include <stdio.h>
struct time {
int hours;
int minutes;
int seconds;
};
void main() {
struct time t;
struct time *ptr;
ptr = &t;
printf("Enter a time in hh:mm:ss format: ");
scanf("%d:%d:%d", &(ptr->hours), &(ptr->minutes), &(ptr->seconds));
if (ptr->hours < 0 || ptr->hours > 23) {
printf("Invalid time - fixing hours...\n");
ptr->hours = ptr->hours < 0 ? 0 : 23;
}
if (ptr->minutes < 0 || ptr->minutes > 59) {
printf("Invalid time - fixing minutes...\n");
ptr->minutes = ptr->minutes < 0 ? 0 : 59;
}
if (ptr->seconds < 0 || ptr->seconds > 59) {
printf("Invalid time - fixing seconds...\n");
ptr->seconds = ptr->seconds < 0 ? 0 : 59;
}
printf("Validated time: %02d:%02d:%02d\n", ptr->hours, ptr->minutes, ptr->seconds);
}
Q. Write the output of following code . Justify
#include<stdio.h>
void main()
{
char *S="hello";
char *P=S;
printf("%c\t%c",*(p+3),s[1]);
}
Answer:
Output = l e
Justification :
The pointer P is assigned to the address of the string literal "hello".
The pointer S is also assigned to the same address as P.
In the printf statement, the character at index 3 of the string "hello" is accessed using pointer arithmetic on P. So *(P+3) refers to the character 'l' (since 'h' is at index 0, 'e' at index 1, 'l' at index 2, and so on).
The character at index 1 of the string "hello" is accessed using the array syntax on S. So S[1] also refers to the character 'e'.
Therefore, the output of the program is l e, separated by a tab character.
Q. Find and justify output of the following:
main()
{
int p[6]- {1,2,3,4,5,6};
int *ptr= p;
printf("%d", *(p+4));
printf("%d",p[2]);
printf("%d", *ptr);
}
Answer:
Output =
5
3
1
Justification:
*(p+4) is equivalent to p[4], which is the fifth element of the array p. Therefore, the first printf statement prints the value 5.
p[2] is the third element of the array p. Therefore, the second printf statement prints the value 3.
*ptr is equivalent to *(p+0), which is the first element of the array p. Therefore, the third printf statement prints the value 1.
Q. What is the output of following C code? Justify.
int main ( )
{
int a =5, b = 10, c = 7;
predict ( a, & b, c);
printf (“% d - % d- % d”, a, b, c);
}
void predict (int p, int *q, int r)
{
p = 50;
*q = *q * 10;
r = 77;
}
Answer:
Output = 5 - 100 - 7
Justification : The predict function takes three arguments p, q, and r.
The first argument p is passed by value, so any changes made to it inside the function are local to the function and do not affect the value of a in the main function.The second argument q is passed by reference (i.e., the address of b is passed to the function), so any changes made to the value pointed to by q inside the function affect the value of b in the main function.
The third argument r is also passed by value.
Inside the predict function, p is assigned the value 50, which does not affect the value of a.
The integer pointer variable q is dereferenced using the * operator and multiplied by 10. Therefore, the value of b in the main function becomes 100.
r is assigned the value 77, which does not affect the value of c in the main function.
Hence only the value of b was changed to 100.
Q. Find and justify the output of the following program :
main( )
{
int x [25];
x[0] = 100;
x[24] = 400;
printf (“\n %d %d”, *x, *(x + 24) + * (x + 0));
}
Answer:
Ouput= 100 500
Justification:
The program initializes an integer array x of size 25 with the first element as 100 and the last element as 400.
In the printf statement, the first argument *x is equivalent to x[0] which is 100.
The second argument *(x + 24) + * (x + 0) is equivalent to x[24] + x[0], which is 400 + 100 = 500.
Q. Find the output and justify.
main( )
{
struct student *ptr=stu;
for (i=0, i<4; i++)
{
printf("At address % u : %.s % d", ptr, ptr name, ptr rollno);
ptr ++;
} }
Q. What is the output of the following program?
void main ()
{
char str[]="program";
char *ptr= str;
printf("%c%c",*ptr,*ptr+4);
ptr= ptr+3;
printf("%c%c", *ptr, *(ptr+2));
}
Answer:
Output= pgsm.
Justification:
The pointer ptr is initialized to the first character of the string "program", i.e. 'p'.
The expression *ptr+4 is evaluated first. it is equivalent to *(ptr+4). This means the value at the memory location ptr+4 is accessed, which is the character 'r'. Therefore, the output of the first printf statement is "pr".
The pointer ptr is then incremented by 3, so it now points to the fourth character of the string "program", i.e. 'g'.
The second printf statement prints the value at the current pointer location, which is 'g', and the value at the location ptr+2, which is 's'. Therefore, the output of the second printf statement is "gs".
Q. What is output of following code, justify:
int change(int x, int *y)
{
x=x+5;
*y=*y+5;
}
main()
{
int a=10, b=20;
change(a,&b);
printf("%d%d",a,b);
}
Answer:
Output = 10 25
Justification:
In the main() function, variables a and b are declared and initialized to 10 and 20 respectively. Then the change() function is called with arguments a and &b.
In the change() function, the value of x is updated to x+5, which does not affect the value of a in the main() function, because x is a copy of a. However, the value of *y (i.e., the value pointed to by y) is updated to *y+5, which means the value of b in the main() function will also be updated to 20+5=25.
Q. Find and correct error in the following code :
#include(stdio.h)
int main()
{
char ch='c';
const char *ptr=&ch;
*ptr='a';
return 0;
}
Answer:
The #include statement should be #include <stdio.h> with angle brackets instead of parentheses.
The pointer ptr is declared as a pointer to a constant character (const char *), which means the value of the character it points to cannot be modified. However, the code tries to modify the value of the character through the pointer (*ptr='a';).
Corrected code:
#include <stdio.h> int main() { char ch = 'c'; char *ptr = &ch; *ptr = 'a'; printf("%c\n", ch); return 0; }
About Abhishek Dhamdhere
Qna Library Is a Free Online Library of questions and answers where we want to provide all the solutions to problems that students are facing in their studies. Right now we are serving students from maharashtra state board by providing notes or exercise solutions for various academic subjects
No comments:
Post a Comment