Você está na página 1de 12

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.

(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

CHAPTERCHAPTER-1 POINTERS
1) Define pointer. Explain the concepts of pointer. Ans: pointer is the variable that stores the address of another variable. The pointer has three main concepts: 1) Pointer constant: In computer memory, memory addresses are known as pointer constant. One can not change its value, it can only be used. Example: house number. 2) Pointer value:The pointer value means the address of another variable. We can not access the value of memory address directly. If we want to access then we have to use & operator and * operator. The pointer value may change from one run of program to another run. Example: int a; &a is known as pointer value. 3) Pointer Variable:The variable which stores the pointer value is known as pointer variable. Example: int a; int *P; P=&a; P is known as pointer variable. Example: #include<stdio.h> #include<conio.h> void main() { int a; int *p; clrscr(); p=&a; printf(\n %u,p); printf(\n %d,*p); getch(); }

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 1

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

2) Give the advantages of pointer. Ans: 1) Pointers allow you to implement pass by reference (pass by address). 2) Pointer allows the modifications by a function that is not the creator of memory. that is function A() can allocate memory and function c() can modify it.

void A(int x) void B(int *p) { { B(&x); *p=*p+1; } } 3) Pointer allows you to use dynamic memory allocation. 4) Pointer gives the ability to implement complex data structures like linked list, trees, etc. 5) Pointer allows ease of programming especially when you are dealing with strings. pointer is incremented as per the size of the variable, that is if the variable is integer then pointer is incremented or decremented by 2 bytes. Example: #include<stdio.h> #include<conio.h> void main() { int *p; int a; p=&a; printf(%u,p); p++; printf(\n %u,p); getch(); } 6) Pointer allows you to resize the data structure whenever needed. For e.g. if you have an array of size 10 it can not be resized but array created using pointer can be resized.

e.g.

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 2

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 3) Explain pointer arithmetic. Ans:

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

1) Pointers can be incremented or decremented according to the size of the variable to which it points. Example: int *p; int a; p=&a; printf(%u,p); p++; printf(\n %u,p);

2) If p1 and p2 are properly declared and initialized, the following operations are valid: a=a+*p1; *p1=*p1+1; mul=*p1 * *p2; div=*p1 / *p2; In the last statement, blank space must be kept between / and * because if we write /* then it will be considered as beginning of the comment. 3) We can also use expressions like p1==p2, p1<p2, p1!=p2. 4) Pointer variable can be assigned null value. NOTE:
1) Two pointers are never added. 2) Integer numbers can be added or subtracted from pointer. & operator is used with pointer to get the address of variable which stores the value. 3) When pointer is incremented, it is incremented as per the size of variable to which it points. 4) We can not use multiplication and division of a pointer variable with constant.

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 3

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 4) Explain working of array with pointers. Ans: 1) One-dimensional array: Syntax: datatype arrayname[size]; e.g. int a[5]; One-dimensional array can also be used with pointer.

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

Suppose if a is one-dimensional array, then the declaration of it is int a[10]; if pa is a pointer to an integer, then declared as int *p; p=&a[0] point to the element zero of a; that is pa contains the address of a[0]. It means that the address of the first array element can be expressed as either &a[0] or simply a. Now the assignment x=*p will copy the contents of a[0] into x. If p points to particular element of array, then by definition p+1 points to the next element. p a[0] Example: #include<stdio.h> void main() { int a[5]={10,11,12,13,14}; int *p; clrscr(); p=&a[0]; printf(\n %u,p); printf(\n %d,*p); printf(\n %u,(p+1)); printf(\n %d,*(p+1)); getch(); } p+1 p+2

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 4

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

2) Two dimensional or multi dimensional array: Syntax: datatype arrayname[row-size][column-size]; e.g. int a[5][5]; Consider the example: int a[3][3]={{1,2,3},{4,5,6},{7,8,9}}; a *a (address) **a (value) 1 *a+1 (address) *(*a+1) (value) 2 *a+2 (address) *(*a+2) (value) 3 First 1-d array

*(a+1) (address) a+1 *(*(a+1)) (value) 4 *(a+2) (address) a+2 *(*(a+2)) (value) 7

*(a+1)+1 (address) *(*(a+1)+1) (value) 5 *(a+2)+1 (address) *(*(a+2)+1) (value) 8

*(a+1)+2 (address) *(*(a+1)+2) (value) 6

Second 1-d array

Third 1-d array *(a+2)+2 (address) *(*(a+2)+2) (value) 9

5) Write a short note on static and dynamic array and differentiate both of them. Ans: Static array: Static array is the fixed size array which means that size of such array can not be changed once their storage has been allocated. This is known as static or fixed size array. OR When the array is declared as int a[5], memory is allocated for the elements of array when the program starts and this memory remains allocated during lifetime of program. This is known as static array allocation. In static array, programmer does not have to free any memory

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 5

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 Dynamic array:-

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

Need Of Dynamic Array: There are the cases in which we dont know how large an array will be needed. In such case, it is suitable to allocate the memory to the array while the program is running. Definition of Dynamic Array: The dynamic array is an array data structure which can be resized during runtime which means that elements can be added and removed. In dynamic array, malloc() is used for memory allocation. malloc() allocates required bytes at required time from the memory heap. Example of Dynamic Array: #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int *a; int i,n; clrscr(); printf(\n enter the number of elements); scanf(%d,&n); a=(int *)malloc(n*sizeof(int)); for(i=0;i<n;i++) { printf(\n enter the number); scanf(%d,&a[i]); } for(i=0;i<n;i++) { printf(\n DYNAMIC ARRAY); printf(\n value of %d is %d,i,a[i]); } printf(\n size=%d,i); getch(); }

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 6

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

When dynamically allocated memory is no longer required, it should be released back to runtime system. This is done by using free(). free(a); a=NULL; Static Array 1) Static Array is used when you know the size of array at compile time. 2) Size of such array will not change during execution of program. 3) There is no need to free the Memory that is allocated. 4) Memory Wastage is there is In static array. 5) Memory allocation in static array is done just like variables. Dynamic Array 1) Dynamic array is used When you dont know the size of array at compile time. 2) Size of such array can be Changed during execution of program. 3) It is the programmers responsibility to free up memory allocated. 4) Memory wastage is not there in dynamic array. 5) Memory allocation in dynamic array is done using malloc().

6) Explain pointer to the structure with example. Ans: Structure is the data structure which consists of group of elements that may or may not have same data type. firstly defining the structure

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 7

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 struct student { int roll; char name[20]; char lname[20]; }; struct student s1; Defining the pointer to structure

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

struct student *p; It is already known that pointer must be initialized before it is used. pointer to the structure can also be allocated memory dynamically. p=(struct student *)malloc(sizeof(struct student));

Example: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int roll; char name[20]; char lname[20]; } struct student s1; void main() { struct student *p; p=&s1; clrscr(); s1.roll=1; strcpy(s1.name,karishma); strcpy(s1.lname,kotecha); printf(\n rollno=%d,p->roll);

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 8

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 printf(\n name=%s,p->name); printf(\n lname=%s,p->lname); getch(); }

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

Write a program to find minimum and maximum number from the elements of 1-d array using array and pointer. #include<stdio.h> #include<conio.h> void main() { int a[6],i,min,max; int *p; clrscr(); printf(\n enter the first element); scanf(%d,&a[0]); p=&a[0]; min=a[0]; max=a[0]; for(i=0;i<5;i++,p++) { printf(\n enter array elements); scanf(%d,p); if(*p<min) { min=*p; } if(*p>max) { max=*p; }

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 9

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030 } printf(\n minimum=%d,min); printf(\n maximum=%d,max); getch(); }

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

Output:-

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 10

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

MCQ
1) P is known as pointer if a) b) c) d) P contains the address of an element in DATA P points to the address of the first element in DATA P can store only memory address P contains the DATA and the address of DATA 2) Array is known as..data structure. a. Heterogeneous b. Homogeneous c. Complex d. None of the above 3) & operator is known as. a. Dereference operator b. Go-to operator c. reference operator d. Address-of operator 4) Which of the following is the proper declaration of a pointer? a. int x b. int &x c. ptr x d. int *x

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 11

SHREE H. N. SHUKLA COLLEGE OF I.T. & MGMT.


(AFFILIATED TO SAURASHTRA UNIVERSITY)

Diwanpara Main Road Nr. Shaligram App. Opp. Diwanpara Police Station Rajkot 360001 Ph. No(0281)2224362,2235030

2 Vaishalinagar Nr. Amrapali Railway Fatak Raiya Road Rajkot - 360001 Ph.No(0281)2440478,2472590

5) Which of the following is the proper keyword to allocate the memory in C? a. new b. malloc c. create d. value 6) Which of the following is the proper keyword to de allocate the memory in C? a. new b. free c. delete d. clear 7) Two pointers can not be added a. true b. false c. None of the above 8) what is chain pointer? Ans: pointer variable is also pointed by another pointer variable. That process is known as chain pointer.

9) chain pointer is indicated by. a. * b. *** c. ** d. None of the above 10) pointer is incremented or decremented by a. as the value is incremented or decremented b. as per the size of the variable c. Both are correct d. None of the above

Shree H.N.Shukla College of I.T & Management

Prepared By: Karishma kotecha 12

Você também pode gostar