Explore the solutions chapter-wise:
SL. No. | Contents |
|---|---|
Chapter 1 | Introduction to Computer Network |
Chapter 2 | HTML5 and CSS3 |
Chapter 3 | Database Part - II MySQL |
Chapter 4 | Introduction to Loops |
Chapter 5 | Nested Loops in C |
Chapter 6 | Arrays in C |
Chapter 7 | |
Chapter 8 | |
Chapter 9 | Structure in C |
Chapter 10 | An Introduction to Object Oriented Programming |
Chapter 11 | Case Studies |
Chapter 8 – Pointers in C
Short Answer Type Questions
1. How is a pointer variable different from a normal variable?
Ans: A normal variable stores a value, such as an integer or a character.
A pointer variable stores the address of another variable. Just like normal variables, pointers must be declared before use.
Example:
a) int x = 10; → stores value 10
b) int *p = &x; → stores the address of x
2. Why is dynamic memory allocation an efficient memory management technique?
Ans: Dynamic memory allocation allows memory to be allocated during program execution and freed when not needed.
This helps in:
a) Better use of memory
b) Avoiding memory wastage
c) Reusing memory when required
Hence, dynamic memory allocation is an efficient way to manage memory.
3. How many bytes are needed to store an int pointer variable? Is it the same for a char pointer variable?
Ans: An int pointer variable requires 4 bytes of memory.
Yes, a char pointer variable also requires 4 bytes.
Pointer size depends on the system architecture, not on the data type.
4. Write the output of the following code segments.
(a)
int *ptr, x = 9;
ptr = &x;
printf("%d", (*ptr)++);
Output:
9
(b)
int *ptr, x = 9;
ptr = &x;
printf("%d", (*ptr)++);
printf("%d", *ptr);
Output:
9
10
(c)
int *ptr, x = 9;
ptr = &x;
int y = ++(*ptr);
printf("%d", y);
Output:
10
(d)
char *ptr, x = 'A';
ptr = &x;
char y = *ptr;
printf("%c", y);
Output:
A
(e)
char *ptr, x = 'A';
ptr = &x;
char y = (*ptr)++;
printf("%c", y);
Output:
A
(f)
char *ptr, x = 'A';
ptr = &x;
char y = ++(*ptr);
printf("%c", y);
Output:
B
(g)
char *ptr, x = 'A';
ptr = &x;
char *y;
y = ptr;
printf("%c", ++(*y));
Output:
B
Long Answer / Programming Questions
5. Program: Dynamic memory allocation for array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a, i;
a = (int *)malloc(10 * sizeof(int));
for (i = 0; i < 10; i++)
a[i] = i + 1;
for (i = 0; i < 5; i++)
printf("%d ", a[i]);
free(a);
return 0;
}
6. Program: Maximum runs using dynamic memory
#include <stdio.h>
#include <stdlib.h>
int max(int *a)
{
int m = a[0];
for (int i = 1; i < 10; i++)
if (a[i] > m)
m = a[i];
return m;
}
int main()
{
int *a;
a = (int *)malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++)
scanf("%d", &a[i]);
printf("Maximum run = %d", max(a));
free(a);
return 0;
}
7. Program: Dynamic memory for name
#include <stdio.h>
#include <stdlib.h>
char *create(int n)
{
return (char *)malloc((n + 1) * sizeof(char));
}
void show(char *s)
{
printf("%s", s);
}
int main()
{
int n;
char *name;
scanf("%d", &n);
name = create(n);
scanf("%s", name);
show(name);
free(name);
return 0;
}
8. Program: Even numbers using dynamic memory
#include <stdio.h>
#include <stdlib.h>
int countEven(int *a, int n)
{
int c = 0;
for (int i = 0; i < n; i++)
if (a[i] % 2 == 0)
c++;
return c;
}
int *copyEven(int *a, int n, int c)
{
int *b = (int *)malloc(c * sizeof(int));
int j = 0;
for (int i = 0; i < n; i++)
if (a[i] % 2 == 0)
b[j++] = a[i];
return b;
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10};
int c = countEven(a, 10);
int *b = copyEven(a, 10, c);
for (int i = 0; i < c; i++)
printf("%d ", b[i]);
free(b);
return 0;
}
9. Program: Numbers divisible by 3
#include <stdio.h>
#include <stdlib.h>
int count(int *a, int n)
{
int c = 0;
for (int i = 0; i < n; i++)
if (a[i] != 0 && a[i] % 3 == 0)
c++;
return c;
}
int *copy(int *a, int n, int c)
{
int *b = (int *)malloc(c * sizeof(int));
int j = 0;
for (int i = 0; i < n; i++)
if (a[i] != 0 && a[i] % 3 == 0)
b[j++] = a[i];
return b;
}
int sum(int *a, int n)
{
int s = 0;
for (int i = 0; i < n; i++)
s += a[i];
return s;
}
int main()
{
int a[] = {1,0,9,12,6,15,0,18,3,0};
int c = count(a, 10);
int *b = copy(a, 10, c);
printf("Sum = %d", sum(b, c));
free(b);
return 0;
}

FOLLOW US