Unit-Iii Short Answer Questions: Pointer Declaration
Unit-Iii Short Answer Questions: Pointer Declaration
* 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
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]);
}
}
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.
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.
#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);
}
O/P:10 20 30 40 50
#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
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.
z = *ptr1 * *ptr2 ;
Ex: ptr1=ptr1+2;
ptr2=ptr2-2;
We cannot perform addition, multiplication and division operations on two pointer variables.
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
* 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];
O/P:10 20 30 40 50
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.
#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);
}