You are on page 1of 16

Q1. What is pointer?

 Pointersarevariables,whichcontaintheaddressof
someothervariables.
 Declaration:
odata type*pointer name;
oe.g.int*ptra;
 Thetypeofapointerdependsonthetypeofthevaria
bleitpointsto.Every pointer points to some data
type.
 All data is stored in memory. But different data
types occupydifferent amount of memory.

Q2. How to declare pointer variable?


 It is declared like regular variable except that an
asterisk (*) is placed in front of the variable
oexample int *x;
 Multiple pointers require multiple asterisks
 The * is not a part of the pointer variable name
oint *myPtr1, *myPtr2;
oThe * is not a part of the pointer variable
name.
 declaring a variable does not allocate space on
the memory for it
 it simply creates a local variable that is
a pointer
 Example
int *ptr1;
float *ptr2;
 ptr1 is a pointer to an int value i.e., it can
have the address of the memory location (or
the first of more than one memory
locations) allocated to an int value
 ptr2 is a pointer to a float value i.e., it
can have the address of the memory
location (or the first of more than one
memory locations) allocated to a float value
Q3. Assigning of Pointer Variables?
 A pointer variable has to be assigned a valid
memory address before it can be used in the
program
 Example:
float data = 50.8;
float *ptr;
ptr = &data;
 This will assign the address of
the memory location allocated for the
floating point variable data to the pointer
variable ptr.
 This is OK, since the variable data has
already been allocated some memory space
having a valid address
 Don’t try to assign a specific integer value to
a pointer variable since it can be disastrous
float *ptr; int data = 50;
ptr = 120; float *ptr;
ptr = &data;
Q4. Initializing pointers?
 A pointer can be initialized during
declaration by assigning it the address of an
existing variable
float data = 50.8;
float *ptr = &data;
 If a pointer is not initialized during
declaration, it is wise to give it a NULL (0)
value
int *ip = 0;
float *fp = NULL;
Q5. The NULLpointer?
 NULL is not memory address 0.
 It is a dereference a pointer whose value is
NULL
Q6. What is Dereferencing?
 Dereferencing – Using a pointer variable to
access the value stored at the location pointed
by the variable
 Using the dereferencing operator* in front
of a pointer variable
 Example: *p;
Q7.Example of pointer?
include <stdio.h>
void main()
{
float data = 50.8;
float *ptr;
ptr = &data;
printf(“data =%d”, *ptr );
*ptr = 27.4;
Printf(“%d”, *ptr );
Printf(“%d”,data);
}
Q8.Explain Pointers to arrays?
 A pointer variable can be used to access the
elements of an array of the same type.
int gradeList[8] = {92,85,75,88,79,54,34,96};
int *myGrades = gradeList;
printf(“%d”, gradeList[1]);
printf(“%d”,*myGrades);
printf(“%d”,*(myGrades + 2));
Note that the array name gradeList acts like the
pointer variable myGrades

Q9. ONE-DIMENSIONAL ARRAY AND POINTER


 A one-dimensional array and a pointer which
points to one-dimensional array are defined as
follows:
int a[10],*p;
&a[i] // a= =&a[0].
int *p;
p = &a[0]
Void main()
{ int a[10],i;
For(i=0;i<5;i++)
{
scanf(“%d”,(a+i));
}
For(i=0;i<5;i++)
{
printf(“%d”,*(a+i));
}
Q10. TWO-DIMENSIONAL ARRAY AND POINTER
• A two-dimensional array is defined as follows:
• int B[2][3];
Address of Two-dimensional Arr
Void main()
{ int a[3][3],i;
Int *p=&a[0][0] // p=*a;
// p=&a[0]
For(i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf(“%d”,(p+3*i+j);
or
scanf(“%d”,p);
p++;
} }
P=&a[0[0];
For(i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf(“%d”, *(p+3*i+j) );
or
printf(“%d”,*p);
p++;
}}
Q11.Pointers to functions?
 Pointers can be passed to functions just
like other types.
 One limitation of functions is that they
only return a single value.
 So how to change multiple values in a
single function.
 pass in pointers
 now any changes that are made are made
to the address being referred to.
 This changes the value for the calling
function as well as the called function
 Example of pointer arguments (CALL BY
REFERNCE)
void Swap(int *p1, int *p2);
void main ()
{
int x, y;
sacanf(%d %d”,x, y);
printf(“x=%d y=%d”,x,y);
Swap(&x,&y); // passes addresses of x and y
explicitly
Printf(“after swaping”);
printf(“x=%d y=%d”,x,y);
}
void Swap(int *p1, int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
Q12. Pointer Expressions and Pointer
Arithmetic?
• Pointer arithmetic:
– Increment/decrement pointer (++ or --)
– Add/subtract an integer to/from a pointer( +
or += , - or -=)
– Pointers may be subtracted from each other
– Pointer arithmetic meaningless unless
performed on pointer to array
• Example:
– 5 element int array on a machine using 4
byte ints
– vPtr points to first element v[ 0 ], which is at
location 3000
vPtr = 3000
– vPtr += 2; sets vPtr to 3008
vPtr points to v[ 2 ]
 Subtracting pointers:
 Returns number of elements between two
addresses
 vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
vPtr2 - vPtr == 2
Q13. Pointer to Characters and Strings?
• String assignment
– Character array
• char color[] = "blue";
– Creates 5 element char array color
• last element is '\0'
– Variable of type char *
• char *colorPtr = "blue";
– Creates pointer colorPtr to letter b in
string “blue”
• “blue” somewhere in memory
– Alternative for character array
• char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };
• The string are treated like character array.
– Char str[5]=“good”; //automatic insert ‘\0’
– Char str;
– Str=“good x
• Char * str;
– str=“good”
Void main()
{
Char name[20];
Char *p;
P=name;
Printf(“Enter name”);
Gets(name);
While(*p!=‘\0’)
{
Printf(“%c”,*p);
P++;
}
or
Printf(“the name is %s “,s);

Q14.Pointers to structure?
struct student {
char name[50];
char major [20];
double gpa;
} STUDENT;
STUDENT bob = {"Bob Smith", "Math", 3.77};
STUDENT sally = {"Sally", "CSEE", 4.0};
STUDENT *pStudent; /* pStudent is a
"pointer to struct student" */
/* make pStudent point to bob */
pStudent = &bob;
/* use -> to access the members */
printf ("Bob's name: %s\n", pStudent->name);
printf ("Bob's gpa : %f\n", pStudent->gpa);
/* make pStudent point to sally */
pStudent = &sally;
printf ("Sally's name: %s\n", pStudent->name);
printf ("Sally's gpa: %f\n", pStudent->gpa);
Pointer to struct for functions:
void PrintStudent(STUDENT *studentp)
{ printf(“Name : %s\n”, studentp->name);
printf(“Major: %s\n”, studentp->major);
printf(“GPA : %4.2f”, studentp->gpa); }
Q14. Advantage of using pointer?
1. Pointers are more efficient in handling
arrays and data tables.
2. Pointers can be used to return multiple
values from a function.
3. Pointers allow c to support dynamic
memory management.
4. Pointer reduces length and complexity of
program.
5. Pointer increase execution speed and
thus reduces the program execution time.
Q15. What is Chain of pointer?
Void main ()
{
int x, *p1, **p2;
X=100;
P1=&x;
P2=&p1;
Printf(“%d”, **p2);
}
The pointer variable p2 contains the address of
the pointer variable p1, which points to the
location that contains the desired value.
This is known as multiple indirections.

You might also like