QB Unit Iv
QB Unit Iv
Pointers and addresses, Pointers and Function Arguments, Pointers and Arrays, Address Arithmetic,
character Pointers and Functions, Pointer Arrays, Pointer to Pointer, Multi-dimensional arrays, Strings,
Initialisation of Pointer Arrays, Command line arguments, Pointers to functions, complicated
declarations..
1. Define a pointer.
A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it
to store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer.
Example:
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
int main () {
return 0;
}
3. What is a NULL Pointers
#include <stdio.h>
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
The value of ptr is 0
The function, which can accept a pointer, can also accept an array as shown in the following
example.
#include <stdio.h>
/* function declaration */
double getAverage(int *arr, int size);
int main () {
int i, sum = 0;
double avg;
In the below example, the sum() function is assigned to the pointer s having two
parameters of type integer. The summation value returned by the pointer s pointing to
the function sum() will be printed as output of this coding.
int sum(int,int);
void main()
{
int (*s)(int,int);
int x=10,y=10;
s=∑
printf("The sum = %d",(*s)(x,y));
}
void main()
{
int *a,**b, x=10;
a=&x;
b=&a;
printf("Address of x = %x Value of x = %d\n",&x,x);
printf("Address in a = %x Address of a = %x Value of a = %d\n",a,&a,*a);
printf("Address in b = %x Address of b = %x Value of b = %d\n",b,&b,**b);
}
In the above program, a is a pointer (*a) and b is a pointer to a pointer (**b) and x is a
variable assigned with its initial value as 10. Here, address of x is assigned to the pointer a
and the address of the pointer a is assigned to the pointer b. Now, b is pointing a pointer
variable which is referred as pointer to pointer. Through the pointer b, the value of x can be
accessed
Address of x1000
x
a 10
Address of a 2000 1000
b
Address of b2500 2000
Part B
1. Write short notes on pointers in C.
2. What are the various arithmetic operations performed on a pointer variable. Explain
with examples.
3. Write short notes on
i. Pointer to a function
ii. Pointer to a pointer
4. Explain in detail about command line arguments.
5. Compare pointers with arrays. Justify how pointers are better than array.