1) What is the first step in problem-solving? A) Writing code B) Debugging C) Understanding the problem D) Optimizing the solution Answer: C 2) Which of these is not a step in the problem-solving process? A) Algorithm development B) Problem analysis C) Random guessing D) Testing and debugging Answer: C 3) What is an algorithm? A) A high-level programming language B) A step-by-step procedure to solve a problem C) A flowchart D) A data structure Answer: B 4) Which of these is the simplest data structure for representing a sequence of elements? A) Dictionary B) List C) Set D) Tuple Answer: B 5) What does a flowchart represent? A) Errors in a program B) A graphical representation of an algorithm C) The final solution to a problem D) A set of Python modules Answer: B 6) What is pseudocode? A) Code written in Python B) Fake code written for fun C) An informal high-level description of an algorithm D) A tool for testing code Answer: C 7) Which of the following tools is NOT commonly used in pr...
Pointer Expressions and Pointer Arithmetic
Like other variables, pointer variables can also be used in expressions. For example, if ptr1 and ptr2 are pointers, then the following statements are valid:
int num1 = 2, num2 = 3, sum = 0, mul = 0, div = 1;
int *ptr1, *ptr2;
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
mul = sum * (*ptr1);
*ptr2 += 1;
div = 9 + (*ptr1)/(*ptr2) – 30;
In C, the programmer may add integers to or subtract integers from pointers as well as subtract one pointer from the other. We can also use shorthand operators with the pointer variables as we use them with other variables.
C also allows comparing pointers by using relational operators in the expressions. For example, p1>p2, p1==p2 and p1!=p2 are all valid in C.
Postfix unary increment (++) and decrement (––) operators have greater precedence than the dereference operator (*). Therefore, the expression *ptr++ is equivalent to *(ptr++), as ++ has greater operator precedence than *. Thus, the expression will increase the value of ptr so that it now points to the next memory location. This means that the statement *ptr++ does not do the intended task. Therefore, to increment the value of the variable whose address is stored in ptr, you should write (*ptr)++.
Null Pointers
So far, we have studied that a pointer variable is a pointer to a variable of some data type. However, in some cases, we may prefer to have a null pointer which is a special pointer value and does not point to any value. This means that a null pointer does not point to any valid memory address.
To declare a null pointer, you may use the predefined constant NULL which is defined in several standard header files including <stdio.h>, <stdlib.h>, and <string.h>. After including any of these
files in your program, you can write
int *ptr = NULL;
You can always check whether a given pointer variable stores the address of some variable or contains NULL by writing,
if (ptr == NULL)
{
Statement block;
}
You may also initialize a pointer as a null pointer by using the constant 0
int *ptr,
ptr = 0;
This is a valid statement in C as NULL is a preprocessor macro, which typically has the value or replacement text 0. However, to avoid ambiguity, it is always better to use NULL to declare a null pointer. A function that returns pointer values can return a null pointer when it is unable to
perform its task.
Generic Pointers
A generic pointer is a pointer variable that has void as its data type. The void pointer, or the generic pointer, is a special type of pointer that can point to variables of any data type. It is declared like a normal pointer variable but using the void keyword as the pointer’s data type. For example,
void *ptr;
In C, since you cannot have a variable of type void, the void pointer will therefore not point to any data and, thus, cannot be dereferenced. You need to cast a void pointer to another kind of pointer before using it.
Generic pointers are often used when you want a pointer to point to data of different types at different times. For example, take a look at the following code.
#include <stdio.h>
int main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d", *(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c", *(char*)gp);
return 0;
}
Output
Generic pointer points to the integer value = 10
Generic pointer now points to the character = A
It is always recommended to avoid using void pointers unless absolutely necessary, as they effectively allow you to avoid type checking.
Programming Example
Write a program to add two integers using pointers and functions.
#include <stdio.h>
void sum (int*, int*, int*);
int main()
{
int num1, num2, total;
printf("\n Enter the first number : ");
scanf("%d", &num1);
printf("\n Enter the second number : ");
scanf("%d", &num2);
sum(&num1, &num2, &total);
printf("\n Total = %d", total);
return 0;
}
void sum (int *a, int *b, int *t)
{
*t = *a + *b;
}
Output
Enter the first number : 23
Enter the second number : 34
Total = 57