Sei sulla pagina 1di 10

Pointers

Pointer: A pointer is a variable that contains the address of a variable.


Q

Give a C statement to allocate memory for p where p has the declaration


float *p;
p=(float *) malloc(sizeof(foat)); or
p=(float *) calloc(1,sizeof(foat));

p=(float *) malloc(4);
OR
or
p=(float *) calloc(1,4);

This address is collected in a float pointer p. The expression (float *) is used to typecast the address being
returned as the address of a float rather than a character. This type casting is necessary since malloc() by
default returns a pointer to a void.
The calloc() functions works exactly similar to malloc() except for the fact that it needs two arguments as
against the one argument required by malloc(). For example,
int *p;
p=(int *) calloc(10,2);
Here 2 indicates that we wish to allocate memory for storing integers, since an integer is a 2 byte entity.
And 10 indicates that we want to reserve space for storing 10 integers.
Example:
main()
{
int

i, n, *x;

printf(\n How many numbers : );


scanf(%d,&n);
x=(int *)malloc(n*sizeof(int));
printf(\nEnter %d numbers :, n);
for(i=0;i<n;i++)
scanf(%d, x+i);
printf(\nThe given numbers are :);
for(i=0;i<n;i++)
printf(%d, *(x+i));
}
Example: array elements and their addresses
main()
{
int x[10]={10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int i;
for(i=0;i<10;i++)
{
printf(\n i=%d

x[i]=%d

*(x+i)=%d, i, x[i], *(x+i));// elements

printf(&x[i]=%u

x+i=%u, &x[i], (x+i));

// address

}
}
OUTPUT:
i=0
x[i]=10
i=1
x[i]=11
i=2
x[i]=12
..

*(x+i)=10
*(x+i)=11
*(x+i)=12

&x[i]=72
&x[i]=74
&x[i]=76

x+i=72
x+i=74
x+i=76

Arrays and pointers


main()
{
int
int

num[]={ 24, 34, 12, 44};


i;

for(i=0;i<4;i++)
printf(\n address=%u

element=%d,&num[i],num[i]);

}
main()
{
int
int

num[]={ 24, 34, 12, 44};


i, *j;

j=num;
for(i=0;i<4;i++, j++)
printf(\n address=%u

// j=&num[0]
element=%d, j,*j);

}
OUTPUT:

address=400
address=402
address=404
address=406

element=24
element=34
element=12
element=44

When to use which one


Accessing array elements by pointers is always faster than accessing by subscripts. Array elements should
be accessed using pointers if the elements are to be accessed in a fixed order, say from beginning to end,
or from end to beginning, or every alternative element or any such definite logic.
Instead, it would be easier to access elements using a pointer if there is not fixed logic in accessing the
elements.
Difference
int

a[6], *pa;

pa=a;
a
A pointer is a

2
a[0
]

a[1]

a[2]

pa

pa+
1

pa+
2

1
variable so

pa=a; and
pa++;
are legal. But an array name is not a variable; constructions like
a=pa; and
a++;
are illegal.
Different ways to access elements of an array
main()
{
int num[]={24, 34, 12, 44, 56, 17};
int i;
for(i=0;i<6;i++)
printf(\nadd=%u element=%d %d %d %d,&num[i], num[i], i[num], *(num+i),*(i+num));
}
Pointer to pointer
main()
{
int i=3,*j, **k;
j=&i;
k=&j;
printf(\n
printf(\n
printf(\n
printf(\n

address ofi=%u%u%u,&i, j, *k);


address ofj=%u%u, &j, k);
value ofj=%uk=%u, j, k);
values ofi=%d %d %d %d , i, *(&i), *j, **k);

}
Q1. What is location name, value at location and locations address? What is lvalue and rvalue of a
variable?
Q2. State whether true or false:
a. '*' means value at address
b. '&' means address of
c. '*(&i)' is same as i
Q3. What is the difference between calloc() and malloc() and what are the default value of memory
allocated by the two.
Q4. Give equivalent pointer expression for accessing same element as
a[i][j][k][l]
where a[10][11][12][13]
Q5. What is the difference between null pointer, a NULL macro, ASCII NULL character and a null
string?

Q6. How many bytes are occupied by near, far and huge pointer?
Q7. What is null pointer assignment error and what causes it?
Q8. What is the difference between:
const char * const s;
char const * const s;
Q9. What do memcpy() and memset() do and use them in a program.

Q12. Point the error in the program, if any,


main()
{
int (*p)() = fun;
(*p)();
}
int fun(void)
{
printf("Hello World");
}
Q13. What do following declaration signify:
1. void *cmp();
2. Void (*cmp)();
3. char (*(*f())[])();
4. char (*(*x[3])())[5];
5. int **(*f)(int **, int **(*)(int **, int **));
Q14. How would we declare:
1. A pointer to a function which receives an int pointer and returns a
float pointer
2. A pointer to a function which receives nothing and returns nothing
Find the output of the following :
Q 1.

main()
{
int

2
404

4
406

n[0
]

3
408

6
410

8
412

5
414

n[1
]

%u %d,n,n[2],n[2][2]);
// assume that array begins at address 404
}
Output: 404 416 1

3
416
n[2
]

5
418

1
420

n[3][3] = {

2,4,3,
6,8,5,
3,5,1

};
printf(\n%u

Q 2.

main()
{
int n[3][3]={2,4,3,6,8,5,3,5,1};
int *ptr;
ptr=&n[0][0];
printf(\n%u,n[2]);
printf(\t%d,ptr[2]);
printf(\t%d,*(ptr+2));
}
OUTPUT : 414 3

Q 3.

main()
{
int arr[3][3][3];
printf(\n%u %u %u,arr, arr+1, arr+2);
printf(\n%u %u %u,arr[0], arr[0]+1, arr[1]);
printf(\n%u %u %u,arr[1][1], arr[1][0]+1, arr[0][1]);
}

Q 4. main()
{
int a[3][4][3]={
{1,2,3,
4,5,6,
7,8,9
},
{ 2,4,6,
8,10,12,
14,16,18
},
{ 3,6,9,
12,15,18,
21,24,27
}
};
int *ptr[]={a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2], a[2][0], a[2][1], a[2][2]};
int *ptr1[]={a[0],a[1],a[2]};
int **ptr2=ptr, i;
printf(\n);
for(i=0;i<=8;i++,ptr2++)
printf(%d,*ptr2);
printf(\n);

for(i=0;i<=2;i++)
printf(%d,*(ptr1[i]));
printf(\n);
for(i=0;i<=8;i++)
printf(%d,*ptr[i]);
/* assume the address of a[0][0][0] to be 400 */
}
Q15. Find the output of the following :
main()
{
int x,*p,**q,***r,a[11];
clrscr();
x=425;
p=&x;
q=&p;
r=&q;
a[0]=232;
a[10]=2568;
printf("\n&p=%u",&p);
printf("\n&q=%u",&q);
printf("\n&r=%u\n",&r);
printf("\nx, value of x=%d \t&x, address of x=%u",x,&x);
printf("\n*p, value of x=%d \tp, address of x=%u",*p,p);
printf("\n**q, value of x=%d \t*q, address of x=%u",**q,*q);
printf("\n***r,value of x=%d \t**r,address of x=%u\n",***r,**r);
printf("\np, value of p=%u \t&p,address of p=%u",p,&p);
printf("\n*q, value of p=%u \tq, address of p=%u",*q,q);
printf("\n**r, value of p=%u \t*r,address of p=%u",**r,*r);
printf("\n*(&q),address of p=%u\n",*(&q));
printf("\nq, value of q=%u \t&q,address of q=%u",q,&q);
printf("\n*r, value of q=%u \tr, address of q=%u\n",*r,r);
printf("\n&(*r),address q=%u",&(*r));
printf("\nr, value of r=%u \t&r,address of r %u\n",r,&r);
printf("\n&a[0],
address of first element=%u",&a[0]);
printf("\na
address of first element=%u",a);
printf("\n&a[5], address of sixth element=%u",&a[5]);
printf("\n*(&a[10]),value of eleventh element=%d",*(&a[10]));
getch();
return 0;

}
/*

OUTPUT

435
65524

65524
65522

65522
65520

65520
65518
&p=65522
&q=65520
&r=65518
x, value of x=425 &x,
*p, value of x=425 p,
**q, value of x=425 *q,
***r,value of x=425 **r,

address of x=65524
address of x=65524
address of x=65524
address of x=65524

p, value of p=65524 &p,


*q, value of p=65524 q,
**r, value of p=65524 *r,
*(&q),

address of p=65522
address of p=65522
address of p=65522
address of p=65522

q, value of q=65522 &q,


*r, value of q=65522 r,

address of q=65520
address of q=65520

&(*r),address q=65520
r, value of r=65520 &r,

address of r 65518

&a[0],
address of first element=65496
a
address of first element=65496
&a[5],
address of sixth element=65506
*(&a[10]),value of eleventh element=2568
*/
Q15 find the output of the following
main()
{
int x[3][5]={
{1,2,3,4,5,},
{6,7,8,9,10},
{11,12,13,14,15}
},*n=&x;
clrscr();

printf("\nx address x[0][0]=%u",x);


printf("\n*(*(x+2)+1)=%d",*(*(x+2)+1));
printf("\n*(*x+2)+5");
printf("\n\t*x
=%u",*x);
printf("\n\t*x+2
=%u",*x+2);
printf("\n\t*(*x+2) =%d",*(*x+2));
printf("\n\t*(*x+2)+5 =%d",*(*x+2)+5);
printf("\n*(*(x+1))");
printf("\n\tx+1
=%u",x+1);
printf("\n\t*(x+1) =%u",*(x+1));
printf("\n\t*(*(x+1)) =%d",*(*(x+1)));
printf("\n*(*(x)+2)+1");
printf("\n\t(x)
=%u",x);
printf("\n\t*(x)
=%u",*(x));
printf("\n\t*(x)+2 =%u",*(x)+2);
printf("\n\t*(*(x)+2) =%d",*(*(x)+2));
printf("\n\t*(*(x)+2)+1=%d",*(*(x)+2)+1);
printf("\n*(*(x+1)+3) =%d",*(*(x+1)+3));
printf("\nn
=%u",n);
printf("\n*n
=%d",*n);
printf("\n*(n+2)
=%d",*(n+2));
printf("\n(*(n+3)+1) =%d",(*(n+3)+1));
printf("\n*(n+5)+1 =%d",*(n+5)+1);
printf("\n++*n
=%d",++*n);
getch();
return 0;
}
/*

OUTPUT

x address of x[0][0]=65496
*(*(x+2)+1)=12
*(*x+2)+5
*x
=65496
*x+2
=65500
*(*x+2) =3
*(*x+2)+5 =8
*(*(x+1))
x+1
=65506
*(x+1) =65506
*(*(x+1)) =6
*(*(x)+2)+1
(x)
=65496
*(x)
=65496
*(x)+2 =65500
*(*(x)+2) =3
*(*(x)+2)+1=4
*(*(x+1)+3) =9

n
=65496
*n
=1
*(n+2)
=3
(*(n+3)+1) =5
*(n+5)+1 =7
++*n
=2

*/

Q 5. main()
{
int arr[]={97,98,99,100,101,102,103,104};
int *ptr=arr+1;
print(++ptr,ptr--,ptr,ptr++,++ptr);
}
void print(int *a, int *b, int *c, int *d, int *e)
{
printf(\n %d %d %d %d %d,*a,*b,*c,*d,*e);
}
Q 6. main()
{
int a[]={0,1,2,3,4};
int *p[]={a,a+2,a+1,a+4,a+3};
int **ptr;
ptr=p;
**++ptr;
printf(\n%d %d %d,**ptr,ptr-p,*ptr-a);
}
Pointers and Multi-dimensional Arrays
Given the definitions
int
int

a[10][20];
*b[10];

then a[3][4] and b[3][4] are both syntactically legal references to a single int. but a is a true twodimensional array : 200 int-sized locations have been set aside, and the conventional rectangular subscript
calculation 20*row+col is used to find the element a[row,col]. For b, however, the definition only
allocates 10 pointers and does not initialize them; initialization must be done explicitly, either statically or
with code. Assuming that each element of b does point to a twenty-element array, then there will be 200
ints set aside, plus ten cells for the pointers. The important advantage of pointer array is that the rows of
the array may be of different lengths. That is, each element of b need not point to a twenty-element
vector; some may point to two elements, some to fifty, and some to none at all.
Let us see another example in terms of array of characters:
Array of pointers
char
*pmonth[]={Illegal month\0,Jan\0,Feb\0,Mar\0,Apr\0,May\0,...Dec\0};

two-dimensional array
char
[15]={Illegal

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
0 I l l e g a l
m o n t h \
0
1 J a n \
0
2 F e b \
0

1 D e c \
2
0
month,Jan,Feb,Mar,Apr,May,Jun,..Dec};

pname
Illegal month\0
Jan\0
Feb\0

Dec\0

aname

amonth[]

Potrebbero piacerti anche