0% found this document useful (0 votes)
196 views10 pages

Unit-Iii Short Answer Questions: Pointer Declaration

Pointer is a variable that stores the address of another variable. Pointers are declared using a datatype and * symbol. Pointers are initialized by assigning the address of a variable to it using & operator. Pointer operators are * and &. * is used for dereferencing and & is used for getting the address. When an array is declared, elements are stored in contiguous locations and array name acts as a pointer to the base address of first element. A 1D array can be accessed using pointers by making a pointer variable point to the base address of array. A 2D array can also be accessed similarly using pointers and subscript operators. An array of pointers can also be declared to store addresses of multiple variables of same type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views10 pages

Unit-Iii Short Answer Questions: Pointer Declaration

Pointer is a variable that stores the address of another variable. Pointers are declared using a datatype and * symbol. Pointers are initialized by assigning the address of a variable to it using & operator. Pointer operators are * and &. * is used for dereferencing and & is used for getting the address. When an array is declared, elements are stored in contiguous locations and array name acts as a pointer to the base address of first element. A 1D array can be accessed using pointers by making a pointer variable point to the base address of array. A 2D array can also be accessed similarly using pointers and subscript operators. An array of pointers can also be declared to store addresses of multiple variables of same type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT-III

Short Answer Questions

1. Define pointer, how declare and initialize pointer?


a) A pointer is a derived datatype in C, pointers are variables that hold the address of another
variable of same type or access the information of particular memory location.
*Pointer Declaration:
Syntax: datatype *pointer variable name;
Ex: int *p or int* p;
*Initialization of Pointers:
Pointer initialization is a process of assigning address of variable to pointer variable.
Pointer variable consists address of variable of same datatype.
Ex: int a,*p;
a=5;
p=&a;
2. What are pointer operators, how to use it?
a) Pointer Operators:
*To create a pointer variable use * operator.
*To find address of a variable use & operator.

Pointer Operator Operator name Purpose


* Value at address Gives value stored at
operator(Indirection/deferencing) particular address
& Address operator Gives address of
variable (memory
location).

3. What are the advantages of using pointers?

Advantages of using Pointers:


1. Pointers are more efficient in handling arrays and structures.
2. It reduces the length and program execution time.
3. It allows ‘c’ to support dynamic memory management (DMA).
4. It is used in accessing files.
5. It is used in call by reference.
4. Define pointer to an array, give an example?
a) When an array is declared, elements of array are stored in continuous location. The address of
the first element of an array is called its base address.

a[0] a[1] a[2] a[3] a[4]


10 20 30 40 50

1000 1002 1004 1006 1008 1010

* The name of the array is called its base address that is a & a[0] are equal. Now both a & a[0]
points to location 1000if we declare ‘p’ an integer pointer then we can make the pointer point to
the array by following assignment:
p=a; or
p=&a[0];
// WAP to access 1-D array using pointers
#include<stdio.h>
void main()
{
int a[5]={10, 20,30,40,50},*ptr,i;
ptr=&a[0];
for(i=0;i<5;i++)
{
printf(“%d\n”,*ptr++);
}
}

O/P:10 20 30 40 50

5.Define array of pointers, give an example?

a) Just like we can declare an array of int, float or char etc, we can also declare an array of
pointers
Syntax: datatype *array_name[size];
int *arrop[5];
Here arrop is an array of 5 integer pointers. It means that this array can hold the address
of 5 integer variables. In other words, you can assign 5 pointer variables of type pointer
to int to the elements of this array.
// WAP to implement array of pointers
#include<stdio.h>
void main()
{
int a[3];
int *p[3];
p[3]=&a[3];
for(i=0;i<3;i++)
printf(“enter the elements”);
for(i=0;i<3;i++)
{
scanf(“%d”,&p[i]);
}
for(i=0;i<3;i++)
{
printf(“%d”,*p[i]);
}
}

6. Define pointer to string, give an example?

a) The variable name of the string str holds the address of the first element of the array i.e., it points
at the starting memory address. So, we can create a character pointer ptr and store the address of
the string str variable in it. This way, ptr will point at the string str.
In the following code we are assigning the address of the string str to the pointer ptr.
char *ptr=str;
We can represent the character pointer variable ptr as follows.
The pointer variable ptr is allocated memory address 8000 and it holds the address of the string
variable str i.e., 1000.

• Accessing string via pointer


To access and print the elements of the string we can use a loop and check for the \o null
character.

In the following example we are using while loop to print the characters of the string
variable str.

7. Define pointer to structure, how to access structure members using structure pointer,
give an example?
a) C structure can be accessed in two ways in a C-program, they are:
1. Using Normal structure variable.
2. Using Pointer structure variable.

• Dot operator is used to access the data using normal structure variable and → is used to
access the data using pointer variable.
• A pointer is a variable which stores the address of another variable.

//WAP to implement structure using pointer

#include<stdio.h>
void main ()
{
struct student
{
int rollno;
char name[20];
int age;
};
struct student s1={101,”abc”, 20};
struct student *ptr;
ptr=&s1;
printf(“enter student details”);
printf(“rollno=%d\n name=%s\n age=%d\n”,ptr→rollno,ptr→name,ptr→age);
}

8. Write a program to illustrate pointer concept?


a) #include<stdio.h>
void main()
{
int a, *p;
p=&a;
printf(“enter a value”);
scanf(“%d”,&a);
printf(“%d\n”,a);
printf(“%u\n”,&a);
printf(“%d\n”,*p);
printf(“%u\n”,p);
printf(“%u\n”,&p);
printf(“%u\n”,&(*p));
}

9. Write a program to read display 1-D array using array pointer?


a) #include<stdio.h>
void main()
{
int a[5]={10, 20,30,40,50},*ptr,i;
ptr=&a[0];
for(i=0;i<5;i++)
{
printf(“%d\n”,*ptr++);
}
}

O/P:10 20 30 40 50

10.Write a program to read and display 2-D array using pointer?


a) #include<stdio.h>
void main()
{
int a[2][2]={10,20,35,45},i,j;
int *p;
p=&a;
for(i=0;i<2;i++)
{
printf("row[%d]",i);
for(j=0;j<3;j++)
{
printf("a[i][j]=%d\t",*(*a+i)+j);
}
printf("\n");
}
}
O/P:
row[0]a[i][j]=10 a[i][j]=11 a[i][j]=12
row[1]a[i][j]=20 a[i][j]=21 a[i][j]=22

11.Write a program to read display string using pointer.

#include<stdio.h>
void main()
{
char str[100];
char *ptr;
printf(“enter a string”);
gets(str);
ptr=str;
printf(“entered string is”);
while(*ptr!=’\0’)
printf(“%c”,*ptr++);
}
O/P: good morning

Long Answer Questions

1. Define pointer, how to declare and initialize pointers, What are pointer operators,
illlustrate with example?
a) A pointer is a derived datatype in C, pointers are variables that hold the address of another
variable of same type or access the information of particular memory location.
*Pointer Declaration:
Syntax: datatype *pointer variable name;
Ex: int *p or int* p;
*Initialization of Pointers:
Pointer initialization is a process of assigning address of variable to pointer variable.
Pointer variable consists address of variable of same datatype.
Ex: int a,*p;
a=5;
p=&a;

*Pointer Operators:
*To create a pointer variable use * operator.
*To find address of a variable use & operator.

Pointer Operator Operator name Purpose


* Value at address Gives value stored at
operator(Indirection/deferencing) particular address
& Address operator Gives address of
variable (memory
location).

2. Discuss in detail about pointer expressions?


a)A pointer is a variable that stores the address of another variable. Unlike other variables that
hold values of a certain type, pointer holds the address of a variable. For example, an integer
variable holds (or you can say stores) an integer value, however an integer pointer holds the
address of a integer variable. We can use pointer variables in expression.
Ex:

int x = 10, y = 20, z;

int *ptr1 = &x;

int *ptr2 = &y;

z = *ptr1 * *ptr2 ;

Will assign 200 to variable z.


We can perform addition and subtraction of integer constant from pointer variable.

Ex: ptr1=ptr1+2;

ptr2=ptr2-2;

We cannot perform addition, multiplication and division operations on two pointer variables.

For Ex: ptr1 +ptr2 is not valid

However we can subtract one pointer variable from another pointer variable.
We can use increment and decrement operator along with pointer variable to increment or
decrement the address contained in pointer variable.

Ex:ptr1++;

ptr2--;

We can use relational operators to compare pointer variables if both pointer variable points to the
variables of same data type.

3. Explain pointer to 1-D array and 2-D array, how to access array elements using pointers,
explain with neat diagram and suitable examples?
a) Pointers & Arrays:- When an array is declared elements of array are stored in continuous
locations. The address of the first element of an array is called its base address.
a[0] a[1] a[2] a[3] a[4]
10 20 30 40 50

1000 1002 1004 1006 1008 1010

* The name of the array is called its base address that is a & a[0] are equal. Now both a & a[0]
points to location 1000if we declare ‘p’ an integer pointer then we can make the pointer point to
the array by following assignment
p=a; or
p=&a[0];

// WAP to access 1-D array using pointers


#include<stdio.h>
void main()
{
int a[5]={10, 20,30,40,50},*ptr,i;
ptr=&a[0];
for(i=0;i<5;i++)
{
printf(“%d\n”,*ptr++);
}
}

O/P:10 20 30 40 50

4.Define array of pointers and implement relevant program?


a) Just like we can declare an array of int, float or char etc, we can also declare an array of
pointers
Syntax: datatype *array_name[size];
int *arrop[5];

Here arrop is an array of 5 integer pointers. It means that this array can hold the address
of 5 integer variables. In other words, you can assign 5 pointer variables of type pointer
to int to the elements of this array.

// WAP to implement array of pointers


#include<stdio.h>
void main()
{
int a[3];
int *p[3];
p[3]=&a[3];
for(i=0;i<3;i++)
printf(“enter the elements”);
for(i=0;i<3;i++)
{
scanf(“%d”,&p[i]);
}
for(i=0;i<3;i++)
{
printf(“%d”,*p[i]);
}
}

4.Define pointer to a structure, explain with any suitable program?


a)//WAP to access structure by using pointers
Like primitive types, we can have pointer to a structure. If we have a pointer to structure,
members are accessed using arrow ( -> ) operator.

#include<stdio.h>
void main()
{
struct student
{
int rollno;
char name[20];
int age;
};
struct student s={12,”abc”,18};
struct student *ptr;
ptr=&s;
printf(“enter student details”);
printf(“rollno=%d\n name=%s\n age=%d\n”,ptr->rollno,ptr=name,ptr=age);
}

You might also like