Get answers, questions, notes, textbook solutions, extras, pdf, mcqs, for Computer Chapter 8 Pointers in C of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.
Summary
Pointers in C programming are like the backstage crew in a theatre production: unseen but indispensable. They give you the ability to access and manipulate variables at the memory level, providing an alternative route that can be more efficient and flexible. Instead of just dealing with data, pointers let you interact directly with a variable’s location in memory.
Declaring a pointer variable is quite straightforward. An asterisk (*) is used to denote that you’re not just dealing with a regular variable. So when you write int *ptr, you’re telling the compiler, “Hey, ptr isn’t just any integer; it’s going to point to the location of an integer.”
How do you assign a memory location to this pointer? That’s where the ampersand (&) comes into play, the address-of operator. When you say ptr = &x, you’re making ptr the designated map-holder for locating x in memory. It’s as if ptr is saying, “If you need to find x, come to me. I know where it lives.”
And once ptr has this responsibility, you can use it to get the value stored in that memory address with the dereference operator (*). The expression *ptr retrieves the content at the memory location it points to, giving you an alternate way to access the value of x. It’s a bit like having a spare key to your house.
Pointers really shine when you’re working with arrays. Imagine an array as a series of houses on a street. The array name is like the address of the first house. With pointer arithmetic, you can walk down the block, from one house to the next, without having to know each house’s address. This makes iterating through arrays much more convenient.
Beyond arrays, pointers are essential for dynamic memory allocation. Functions like malloc() allow you to request memory space while your program is running. This is analogous to building an extension on your house while still living in it—a feat that’s not possible if you’re limited to fixed-sized arrays. Once this dynamic memory is allocated, it’s your pointer variable that holds the directions for getting there.
Yet, this incredible power comes with its own set of risks. Pointers can lead to memory leaks, where you lose track of some allocated memory. They can leave you with dangling pointers, akin to an address that leads to a demolished building. And improper use can result in segmentation faults, abrupt crashes that happen when you try to access restricted memory.
Video tutorial
Exercise/textual questions and answers
1. How is a pointer variable different from a normal variable?
Answer: A pointer variable stores the address of another variable, while a normal variable directly stores a value.
2. Why is dynamic memory allocation an efficient memory management technique?
Answer: Dynamic memory allocation is efficient because it allows memory to be allocated at runtime based on the program’s needs, rather than pre-allocating a fixed amount of memory at compile time which may be more than needed.
3. How many bytes are needed to store an int pointer variable? Is it the same for a char pointer variable? Write a simple C program to explain your answer.
Answer: An int pointer variable requires 4 bytes on a 32-bit system and 8 bytes on a 64-bit system. A char pointer variable requires 1 byte on any system. Here is a simple C program to demonstrate:
#include <stdio.h>
int main() {
int *intPtr;
char *charPtr;
printf(“Size of int pointer: %lu bytes\n”, sizeof(intPtr));
printf(“Size of char pointer: %lu byte\n”, sizeof(charPtr));
return 0;
}
4. Write the output of the following code segment.
a.
int * ptr, x = 9;
ptr = &x;
printf(“\n %d”, (*ptr)++);
Answer: 9
b.
int *ptr, x = 9;
ptr = &x;
printf(“\n %d”, (*ptr)++);
printf(“\n %d”, *ptr);
Answer: 9
c.
int *ptr, x = 9;
ptr = &x;
int y = ++(*ptr);
printf(“\n %d”, y);
Answer: 10
d.
char * ptr, x = ‘A’;
ptr = &x;
char y = *ptr;
printf(“\n %c”, y);
Answer: A
e.
char *ptr, x = ‘A’;
ptr = &x;
char y = (*ptr)++;
printf(“\n %c”, y);
Answer: B
f.
char *ptr, x = ‘A’;
ptr = &x;
char y = ++(*ptr);
printf(“\n %c”, y);
Answer: C
5. Write a C program to dynamically allocate memory for an array to store 10 integers and display the first 5 out of them.
Answer: #include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, i;
arr = (int*)malloc(10 * sizeof(int));
for(i=0; i<10; i++) {
arr[i] = i+1;
}
printf(“First 5 elements:\n”);
for(i=0; i<5; i++) {
printf(“%d\n”, arr[i]);
}
free(arr);
return 0;
}
6. Write a C program to dynamically allocate memory for an array to store runs scored by Virat Kohli in the last ten ODI cricket matches. Write a function to find the maximum one.
Answer: #include <stdio.h>
#include <stdlib.h>
int findMax(int arr[], int n){
int max = arr[0];
for(int i=1; i<n; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
int main() {
int n = 10;
int *runs = (int*) malloc(n * sizeof(int));
// initialize array
printf(“Maximum runs: %d”, findMax(runs, n));
free(runs);
return 0;
}
7. Write a C program and define a function that takes the length of your name as an input parameter and then allocates memory dynamically to store your name. Write another function to display the name.
Answer: #include <stdio.h>
#include <stdlib.h>
void getName(char **name, int len){
*name = (char *)malloc(len * sizeof(char));
printf(“Enter name: “);
scanf(“%s”, *name);
}
void printName(char *name){
printf(“Name: %s”, name);
}
int main() {
char *name;
int len;
printf(“Enter name length: “);
scanf(“%d”, &len);
getName(&name, len);
printName(name);
free(name);
return 0;
}
8. Write a C program to store some integer variables in an array. Then write functions to the following.
1. To calculate the number of even numbers in the array.
2. To dynamically allocate memory to a new array to store only the even numbers.
3. To copy the even numbers from the first array to the second one.
Sample run:
First array
2 | 4 | 0 | 9 | 1 | 9 | 8 | 6 |
Second array
2 | 4 | 0 | 8 | 6 |
Answer: #include <stdio.h>
#include <stdlib.h>
// Function to count even numbers
int countEvens(int arr[], int n){
int count = 0;
for(int i=0; i<n; i++){
if(arr[i] % 2 == 0){
count++;
}
}
return count;
}
// Function to allocate new array
int* allocateEvensArray(int numEvens){
return (int*)malloc(numEvens * sizeof(int));
}
// Function to copy even numbers
void copyEvens(int arr1[], int n1, int arr2[], int n2){
int j = 0;
for(int i=0; i<n1; i++){
if(arr1[i] % 2 == 0){
arr2[j] = arr1[i];
J++;
}
}
}
int main(){
int arr[8] = {2, 4, 0, 9, 1, 9, 8, 6};
int n = 8;
int numEvens = countEvens(arr, n);
int *evensArr = allocateEvensArray(numEvens);
copyEvens(arr, n, evensArr, numEvens);
// Print arrays
for(int i=0; i<n; i++){
printf(“%d “, arr[i]);
}
printf(“\n”);
for(int i=0; i<numEvens; i++){
printf(“%d “, evensArr[i]);
}
free(evensArr);
return 0;
}
9. Write a C program to store some integer variables in an array. Then write functions to the following.
1. To calculate the number of non-zero elements that are divisible by 3.
2. To dynamically allocate memory to a new array to store only those elements.
3. To copy the selected elements from the first array to the second one.
4. To calculate the summation of these elements.
Sample run:
2 | 4 | 0 | 9 | 1 | 9 | 8 | 6 |
First array
9 | 9 | 6 |
Summation: 24
Answer: #include <stdio.h>
#include <stdlib.h>
// Function to count elements divisible by 3
int countDivBy3(int arr[], int n){
int count = 0;
for(int i=0; i<n; i++){
if(arr[i] != 0 && arr[i] % 3 == 0){
count++;
}
}
return count;
}
// Function to allocate new array
int* allocateNewArray(int numElems){
return (int*) malloc(numElems * sizeof(int));
}
// Function to copy divisible by 3 elements
void copyElements(int arr1[], int n1, int arr2[], int n2){
int j = 0;
for(int i=0; i<n1; i++){
if(arr1[i] != 0 && arr1[i] % 3 == 0){
arr2[j++] = arr1[i];
}
}
}
// Function to find summation
int findSum(int arr[], int n){
int sum = 0;
for(int i=0; i<n; i++){
sum += arr[i];
}
return sum;
}
int main() {
int arr[] = {2, 4, 0, 9, 1, 9, 8, 6};
int n = 8;
int numDivBy3 = countDivBy3(arr, n);
int *newArr = allocateNewArray(numDivBy3);
copyElements(arr, n, newArr, numDivBy3);
int sum = findSum(newArr, numDivBy3);
printf(“Summation: %d”, sum);
free(newArr);
return 0;
}
Get notes of other boards, classes, and subjects