0% found this document useful (0 votes)
16 views6 pages

QB Unit Iv

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

QB Unit Iv

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT – IV: POINTERS AND ARRAYS

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 */

2. How to Use Pointers?

(a) We define a pointer variable,


(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the
address specified by its operand.
Example:
#include <stdio.h>

int main () {

int var = 20; /* actual variable declaration */


int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip );

return 0;
}
3. What is a NULL Pointers

A pointer that is assigned with NULL is called a null pointer.


The NULL pointer is a constant with a value of zero defined in several standard libraries.
Example:

#include <stdio.h>

int main () {

int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;
}
When the above code is compiled and executed, it produces the following result −
The value of ptr is 0

4. How to pass a pointer to a function as an argument.


Following is a simple example where we pass an unsigned long pointer to a function and
change the value inside the function which reflects back in the calling function.
#include <stdio.h>
#include <time.h>
void getSeconds(unsigned long *val);
int main () {
unsigned long sec;
getSeconds( &sec );
/* print the actual value */
printf("Number of seconds: %ld\n", sec );
return 0;
}

void getSeconds(unsigned long *val) {


/* get the current number of seconds */
*val = time( NULL );
return;
}

5. How to pass an Array as a function Argument:

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 () {

/* an int array with 5 elements */


int balance[5] = {1000, 2, 3, 17, 50};
double avg;

/* pass pointer to the array as an argument */


avg = getAverage( balance, 5 ) ;

/* output the returned value */


printf("Average value is: %f\n", avg );
return 0;
}

double getAverage(int *arr, int size) {

int i, sum = 0;
double avg;

for (i = 0; i < size; ++i) {


sum += arr[i];
}

avg = (double)sum / size;


return avg;
}

6. List various pointer arithmetic operations in C.

7. How to define a two-dimensional array using pointers in C


void main()
{
int *p,a[5][5],i,j;
p=&a;
for (i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",((p+i)+j));
printf("%d ",*((p+i)+j));
}
printf("\n");
}
}
In the above coding, p is a pointer and a is a two dimensional array. The array element
a’s address is assigned to the pointer p and p will point to the starting position of the
array a. Row of the array a is accessed by the index variable i and the column of the
array is accessed by the index variable j. (p+i) is specifying the position of ith row
from starting point of the pointer p and ((P+i)+j) is specifying the jth column in the ith
row from the starting point of the pointer p.
a[5][5]
p

8. How to define a pointer to a function?

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=&sum;
printf("The sum = %d",(*s)(x,y));
}

int sum(int x,int y)


{
printf("%d ",x+y);
return(x+y);
}
9. What is command line arguments in C?
The command line arguments are handled using main() function arguments
where argc refers to the number of arguments passed, and argv[] is a pointer array
which points to each argument passed to the program.
void main(int argc, char *argv[])
{
printf("\nFile Name %s",argv[0]);
printf("\nFirst Argument %s",argv[1]);
printf("\nLast Argument %s",argv[argc-1]);
}

If the inputs are given as


File1 10 20 30 40
Output will be
File Name File1
First Argument 10
Last Argument 40

10. How to define a pointer to a pointer?

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 x1000
x
a 10
Address of a 2000 1000

b
Address of b2500 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.

You might also like