Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.

Similar presentations


Presentation on theme: "Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer."— Presentation transcript:

1 Pointers PART - 2

2 Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection. A pointer variable must be declared before it can be used.

3 Pointers contd….. Memory can be conceptualized as a linear set of data locations. Variables reference the contents of a locations Pointers have a value of the address of a given location Contents1 Contents11 Contents16 ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 ADDR6 * * * ADDR11 * * ADDR16

4 Pointers cond…. Examples of pointer declarations: int *a; float *b; char *c; The asterisk, when used as above in the declaration, tells the compiler that the variable is to be a pointer, and the type of data that the pointer points to, but NOT the name of the variable pointed to.

5 Example 1 #include int j, k; int *ptr; int main(void) { j = 1; k = 2; ptr = &k; printf("\n"); printf("j has the value %d and is stored at %d\n", j, &j); printf("k has the value %d and is stored at %d\n", k, &k); printf("ptr has the value %p and is stored at %d\n", ptr, &ptr); printf("The value of the integer pointed to by ptr is %d\n", *ptr); return 0; }

6 & and * Operators & - "address operator" which gives or produces the memory address of a data variable.  int a=3; Now &a will refer to address of a means it will have value 3040. * - "dereferencing operator" which provides the contents in the memory location specified by a pointer.  *(&a) will give value at address 3040 i.e. 3. 3040 3 a

7 Pointer Type Pointer is only store memory address but depending upon which type of variable’s address they have will categorize it into four types:  Integer Pointer  Float Pointer  Character Pointer  Double Pointer

8 Pointer Arithmetic

9 A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.

10

11

12

13

14 Do not try! Addition of two pointers. Multiplication of two pointers with constant. Division of pointer with constant.

15 Pointer with sizeof This keyword can be used to determine the number of bytes in a data type, a variable, or an array Example: double array [10]; sizeof (double); /* Returns the value 8 */ sizeof (array); /* Returns the value 80 */ sizeof(array)/sizeof(double); /* Returns 10 */

16 Pointers and Array Array are stored in consecutive memory locations so they can be accessed easily with the help of pointer. With the help of pointer arrays can be passed to function.

17 Example 2 #include int my_array[] = {1,23,17,4,-5,100}; int *ptr; void main(void) { int i; ptr = &my_array[0]; /* point our pointer to the first element of the array */ for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); }

18 Example 3: Passing Array to function

19 2D Array or Matrix Example: Storing Roll No. & Marks together #include void main( ) { int stud[4][2], i; for (i=0; i<=3; i++) { printf("\n Enter roll no. and marks of student"); scanf ("%d %d",&stud[i][0],&stud[i][1]); } for( i=0; i<=3; i++) printf("\n%d %d",stud[i][0],stud[i][1]); }

20 2-D Array Consider a 2D array declared as Memory Map of 2D array

21 Pointers and 2D Array #include void main( ) { int s[4][2]={{1234,56},{1212,33},{1434,80},{1312,78}}, i; for (i=0;i<=3;i++) printf("\nAddress of %d th 1-D array = %u",i,s[i]); }

22 2D Array with Pointers #include void main( ) { int s[4][2]={{1234,56},{1212,33},{1434,80},{1312,78}}, i, j; for(i=0; i<=3; i++) {printf("\n"); for(j=0; j<=1; j++) printf("%d ",*(*(s+i)+j)); }

23 Pointers & Function Pointers can be used to pass addresses of variables to call functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as "call by value ".

24 Pointer and Function If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. The following shows the swap function modified from a "call by value" to a "call by reference". Note that the values are now actually swapped when the control is returned to main function.

25 #include void swap ( int a, int b ) ; void main ( ) { int a = 5, b = 6; swap (a, b) ; printf(“ Within Main function”); printf("a=%d b=%d\n",a,b) ; } void swap( int a, int b ) { int temp; temp= a; a= b; b = temp ; printf(“ Within Swap Function”); printf ("a=%d b=%d\n", a, b); } Call by Value Function

26 #include void swap ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; swap (&a, &b) ; printf(“Within Main Function”); printf("a=%d b=%d\n",a,b) ; return 0 ; } void swap( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf(“Within Swap Function”); printf ("a=%d b=%d\n", *a, *b); } Call by Reference Function

27 Assignment Compare call by value and Cal by reference with the help of one example. Define an array in main method and print it using a function by passing reference of array to the function.


Download ppt "Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer."

Similar presentations


Ads by Google