Your SlideShare is downloading. ×
0
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
C programming session 05
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C programming session 05

1,539

Published on

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,539
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
176
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Begin the session by explaining the objectives of the session.
  • Pointers contain addresses of variables of a specific type. Hence, the pointer and the variable to which it is pointing should be of the same type. The number of bytes used by the pointer to hold an address does not depend on the type of the variable the variable it is pointing to. The number of bytes used by a pointer is generally 4 bytes, or 2 bytes in some implementations. Type casting may be used to initialize a pointer with the address of a variable of a different type. For example, the following code is valid : char *ptr ; int i = 4 ; ptr = (char *) (&i) ; though this is seldom done. Pointers to type void can contain addresses of any data. It does not make sense to do any arithmetic with these pointers.
  • Use this slide to test the student’s understanding on declaring pointers.
  • Preceding a pointer with ampersand will yield the address of the pointer itself, and can be stored in a pointer to a pointer. Consider the following code: char *ptr, **pptr, chr; ptr = &chr; /* address of a character */ pptr = &ptr; /* address of a pointer */
  • Use this slide to test the student’s understanding on declaring and initializing pointers.
  • Generally, only the operators + and – are used with pointers. This results in the address in the pointer getting altered by a value equal to the size of the data type it is associated with. The following declaration is for a pointer to an array of 10 elements: char (*ptr) [10] ; /* note, it is not an array of pointers */ If the pointer ptr is initialized and contains the address 100 , ptr++ results in the address in ptr being scaled by 10. ptr will now contain 110.
  • Use this slide to test the student’s understanding on pointers arithmetic.
  • Use this slide to test the student’s understanding on one-dimensional arrays and pointers.
  • Use this slide to test the student’s understanding on 2-D arrays and pointers.
  • Use this and the next slide to summarize the session.
  • Transcript

    • 1. <ul><li>In this session, you will learn to: </li></ul><ul><ul><li>Declare and manipulate pointers </li></ul></ul><ul><ul><li>Use pointers to manipulate character arrays </li></ul></ul>Objectives
    • 2. <ul><li>Every stored data item occupies one or more contiguous memory cells. </li></ul><ul><li>Every cell in the memory has a unique address. </li></ul><ul><li>Any reference to a variable, declared in memory, accesses the variable through the address of memory location. </li></ul><ul><li>Pointers are variables, which contain the addresses of other variables (of any data type) in memory. </li></ul>Declaring and Manipulating Pointers
    • 3. <ul><li>A pointer variable must be declared before use in a program. </li></ul><ul><li>A pointer variable is declared preceded by an asterisk. </li></ul><ul><li>The declaration: </li></ul><ul><li> int *ptr; /* ptr is pointing to an int */ </li></ul><ul><li>Indicates that ptr is a pointer to an integer variable. </li></ul><ul><li>An uninitialized pointer may potentially point to any area of the memory and can cause a program to crash. </li></ul><ul><li>A pointer can be initialized as follows: </li></ul><ul><ul><li>ptr= &amp;x; </li></ul></ul><ul><ul><li>In the preceding initialization, the pointer ptr is pointing to x . </li></ul></ul>Declaring Pointers
    • 4. <ul><li>In the following declaration: </li></ul><ul><li>float *ptr_to_float; </li></ul><ul><li>The pointer variable ptr_to_float is pointing to a variable of type ____________. </li></ul><ul><li>2. Is the following declaration valid? </li></ul><ul><li>*ptr_to_something; </li></ul><ul><li>3. State whether True or False: </li></ul><ul><li> An integer is declared In the following declaration: </li></ul><ul><li>int *ptr_to_int; </li></ul><ul><li>4. Is the following declaration valid? </li></ul><ul><li>int some_int, *ptr_to_int; </li></ul>Practice: 4.1
    • 5. <ul><li>Solution: </li></ul><ul><ul><li>1. float </li></ul></ul><ul><ul><li>2. No. When a pointer variable is being declared, the type of variable to which it is pointing to ( int , float , or char ) should also be indicated. </li></ul></ul><ul><ul><li>3. False. A pointer to an integer is being declared and not an integer. </li></ul></ul><ul><ul><li>4. Yes. It is okay to club declaration of a certain type along with pointers to the same type. </li></ul></ul>Practice: 4.1 (Contd.)
    • 6. <ul><li>Pointers can be manipulated like variables. </li></ul><ul><li>The unary operator * gives value of the variable a pointer is pointing to. </li></ul><ul><li>The unary operator * is also termed as the indirection operator. </li></ul><ul><li>The indirection operator can be used only on pointers. </li></ul>Manipulating Pointers
    • 7. <ul><li>The symbol _________ is used to obtain the address of a variable while the symbol__________ is used to obtain the value of the variable to which a pointer is pointing to. </li></ul><ul><li>With the following declarations: </li></ul><ul><li>int x, y, *ptr; </li></ul><ul><li>Which of the following are meaningful assignments? </li></ul><ul><ul><li>a. x = y; </li></ul></ul><ul><ul><li>b. y=*ptr; </li></ul></ul><ul><ul><li>c. x = ptr; </li></ul></ul><ul><ul><li>d. x = &amp;.ptr; </li></ul></ul><ul><ul><li>e. ptr = &amp;x; </li></ul></ul><ul><ul><li>f. x = &amp;y; </li></ul></ul>Practice: 4.2
    • 8. <ul><li>3. Consider the following sequence of statements and complete the partially-filled table: </li></ul><ul><li>int x, y, *ptrl, *ptr2; </li></ul><ul><li>x = 65; </li></ul><ul><li>y = 89; </li></ul><ul><li>ptr1 = &amp;x; /*ptrl points to x */ </li></ul><ul><li>ptr2 = &amp;y/; /* ptr2 points to y */ </li></ul><ul><li>x = *ptr1; /* statement A*) </li></ul><ul><li>ptr1 = ptr2: /* statement B */ </li></ul><ul><li>x = *ptr1; /* statement C*/ </li></ul><ul><li>After statement &amp;x x &amp;y y ptr1 ptr2 </li></ul><ul><ul><li>A 1006 1018 </li></ul></ul><ul><ul><li>B </li></ul></ul><ul><ul><li>C </li></ul></ul>Practice: 4.2 (Contd.)
    • 9. <ul><li>4. What is the output of the following sequence of statements: </li></ul><ul><ul><li>int x, y, temp,*ptrl, *ptr2; /* declare */ </li></ul></ul><ul><ul><li>x = 23; </li></ul></ul><ul><ul><li>y = 37; </li></ul></ul><ul><ul><li>ptrl = &amp;x; /* ptrl points to x */ </li></ul></ul><ul><ul><li>ptr2 = &amp;y; /* ptr2 points to y */ </li></ul></ul><ul><ul><li>temp = *ptrl; </li></ul></ul><ul><ul><li>*ptr1 = *ptr2; </li></ul></ul><ul><ul><li>*ptr2 = temp; </li></ul></ul><ul><ul><li>printf(“x is %d while y is %d”, x, y); </li></ul></ul>Practice: 4.2 (Contd.)
    • 10. <ul><li>Solution: </li></ul>Practice: 4.2 (Contd.)
    • 11. <ul><li>Pointer Arithmetic: </li></ul><ul><ul><li>Arithmetic operations can be performed on pointers. </li></ul></ul><ul><ul><li>Therefore, it is essential to declare a pointer as pointing to a certain datatype so that when the pointer is incremented or decremented, it moves by the appropriate number of bytes. Consider the following statement: </li></ul></ul><ul><ul><li>ptr++; </li></ul></ul><ul><ul><li>It does not necessarily mean that ptr now points to the next memory location. The memory location it will point to will depend upon the datatype to which the pointer points. </li></ul></ul><ul><ul><li>May be initialized when declared if done outside main() . </li></ul></ul><ul><ul><li>Consider the following example: </li></ul></ul><ul><ul><ul><li>#include &lt;stdio.h&gt; </li></ul></ul></ul><ul><ul><ul><li>char movie[]= “Jurassic Park”; </li></ul></ul></ul><ul><ul><ul><li>main() { </li></ul></ul></ul><ul><ul><ul><li>char *ptr; </li></ul></ul></ul>Pointer Arithmetic
    • 12. <ul><ul><li>Consider the following example: </li></ul></ul><ul><ul><ul><li>#include &lt;stdio.h&gt; char movie[]= “Jurassic Park”; main() { char *ptr; ptr=movie; printf(“%s”, movie); /* output: Jurassic Park */ printf(“%s”,ptr); /* output: Jurassic Park */ ptr++; printf(“%s”,movie); /* output: Jurassic Park */ printf(“%s&amp;quot;,prr); /* output: urassic Park */ ptr++; printf(“%s”,movie); /* output; Jurassic Park */ printf(“%s”,ptr); /* output: rassic Park */ /* Note that the incrementing of the pointer ptr does not in any way affect the pointer movie */ } </li></ul></ul></ul>Pointer Arithmetic (Contd.)
    • 13. <ul><li>Consider the following code snippet: </li></ul><ul><ul><li>#include &lt;stdio.h&gt; </li></ul></ul><ul><ul><li>int one_d[] = {l,2,3}; </li></ul></ul><ul><ul><li>main(){ </li></ul></ul><ul><ul><li>int *ptr; </li></ul></ul><ul><ul><li>ptr = one_d; </li></ul></ul><ul><ul><li>ptr +=3; /* statement A*/ </li></ul></ul><ul><ul><li>printf(“%dn”, *ptr); /*statement B */ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>After statement A is executed, the new address of ptr will be ____ bytes more than the old address . </li></ul></ul><ul><ul><li>State whether True or False: </li></ul></ul><ul><ul><li>The statement B will print 3 . </li></ul></ul>Practice: 4.3
    • 14. <ul><li>Solution: </li></ul><ul><ul><li>12 ( Size of integer = 4*3) </li></ul></ul><ul><ul><li>False. Note that ptr is now pointing past the one-d array. So, whatever is stored (junk or some value) at this address is printed out. Again, note the dangers of arbitrary assignments to pointer variables. </li></ul></ul>Practice: 4.3 (Contd.)
    • 15. <ul><li>Array name contains the address of the first element of the array. </li></ul><ul><li>A pointer is a variable, which can store the address of another variable. </li></ul><ul><li>It can be said that an array name is a pointer. Therefore, a pointer can be used to manipulate an array. </li></ul>Using Pointers to Manipulate Character Arrays
    • 16. <ul><li>One-Dimensional Arrays and Pointers: </li></ul><ul><ul><li>Consider the following example: </li></ul></ul><ul><ul><ul><li>#include &lt;stdio.h&gt; char str[13]={“Jiggerypokry”}; char strl[]={ “Magic”}; main() { char *ptr; printf(“We are playing around with %s&amp;quot;, str); /* Output: We are playing around with Jiggerypokry*/ </li></ul></ul></ul><ul><ul><ul><li>ptr=str ; /* ptr now points to whatever str is pointing to */ </li></ul></ul></ul><ul><ul><ul><li>printf(“We are playing around with %s&amp;quot; ,ptr); </li></ul></ul></ul><ul><ul><ul><li>/* Output: We are playing around with Jiggerypokry */ </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>One-Dimensional Arrays and Pointers
    • 17. <ul><ul><li>In the preceding example the statement: </li></ul></ul><ul><ul><li>ptr=str; </li></ul></ul><ul><ul><li>Gives the impression that the two pointers are equal. However, there is a very subtle difference between str and ptr . str is a static pointer, which means that the address contained in str cannot be changed. While ptr is a dynamic pointer. The address in ptr can be changed. </li></ul></ul>One-Dimensional Arrays and Pointers (Contd.)
    • 18. <ul><li>Given the declaration: </li></ul><ul><ul><li>char some_string [10]; </li></ul></ul><ul><ul><li>some_string points to _________. </li></ul></ul><ul><li>State whether True or False: </li></ul><ul><li>In the following declaration, the pointer err_msg contains a valid address: </li></ul><ul><ul><li>char *err_msg = “Some error message”; </li></ul></ul><ul><li>3. State whether True or False: </li></ul><ul><li>Consider the following declaration: </li></ul><ul><ul><li>char *err_msg = “Some error message”; </li></ul></ul><ul><ul><li>It is more flexible than the following declaration: </li></ul></ul><ul><ul><li>char err_msg[19]=”Some error message”; </li></ul></ul>Practice: 4.4
    • 19. <ul><li>Solution: </li></ul><ul><ul><li>1. some_string [0] </li></ul></ul><ul><ul><li>2. True </li></ul></ul><ul><ul><li>3. True. Note that one does not have to count the size of the error message in the first declaration. </li></ul></ul>Practice: 4.4 (Contd.)
    • 20. <ul><li>Two-dimensional arrays can be used to manipulate multiple strings at a time. </li></ul><ul><li>String manipulation can also be done by using the array of pointers, as shown in the following example: </li></ul><ul><ul><li>char *things[6]; /* declaring an array of 6 pointers to char */ </li></ul></ul><ul><ul><li> things[0]=”Raindrops on roses”; </li></ul></ul><ul><ul><ul><li>things[1]=”And Whiskers on kettles”; </li></ul></ul></ul><ul><ul><ul><li>things[2]=”Bright copper kettles”; </li></ul></ul></ul><ul><ul><ul><li>things[3]=”And warm woolen mittens”; </li></ul></ul></ul><ul><ul><ul><li>things[4]=”Brown paper packages tied up with strings”; </li></ul></ul></ul><ul><ul><ul><li>things[5]=”These are a few of my favorite things”; </li></ul></ul></ul>Two-Dimensional Arrays and Pointers
    • 21. <ul><ul><li>The third line of the song can be printed by the following statement: </li></ul></ul><ul><ul><ul><li>printf(“%s”, things[2]); </li></ul></ul></ul><ul><ul><ul><li>/*Output: Bright copper kettles */ </li></ul></ul></ul>Two-Dimensional Arrays and Pointers (Contd.)
    • 22. <ul><li>State whether True or False: </li></ul><ul><li>While declaring two-dimensional character arrays using pointers, yon do not have to go through the tedium of counting the number of characters in the longest string. </li></ul><ul><li>2. Given the following error messages: </li></ul><ul><ul><li>All&apos;s well </li></ul></ul><ul><ul><li>File not found </li></ul></ul><ul><ul><li>No read permission for file </li></ul></ul><ul><ul><li>Insufficient memory </li></ul></ul><ul><ul><li>No write permission for file </li></ul></ul><ul><li>Write a program to print all the error messages on screen, using pointers to array. </li></ul>Practice: 4.5
    • 23. <ul><li>Solution: </li></ul><ul><ul><li>True. New strings can be typed straight away within the {}. As in: </li></ul></ul><ul><ul><ul><li>char *err_msg_msg[]= { </li></ul></ul></ul><ul><ul><ul><li>“ All&apos;s well”, </li></ul></ul></ul><ul><ul><ul><li>“ File not found”, </li></ul></ul></ul><ul><ul><ul><li>“ No read permission for file”, </li></ul></ul></ul><ul><ul><ul><li>“ Insufficient memory”, </li></ul></ul></ul><ul><ul><ul><li>“ No write permission for file” </li></ul></ul></ul><ul><ul><ul><li>}; </li></ul></ul></ul><ul><ul><li>The number of strings will define the size of the array. </li></ul></ul>Practice: 4.5 (Contd.)
    • 24. <ul><ul><li>2. The program is: </li></ul></ul><ul><ul><ul><li># include&lt;stdio.h&gt; </li></ul></ul></ul><ul><ul><ul><li># define ERRORS 5 </li></ul></ul></ul><ul><ul><ul><li>char *err_msg[]= { /*Note the missing index*/ </li></ul></ul></ul><ul><ul><ul><li>“ All&apos;s well”, </li></ul></ul></ul><ul><ul><ul><li>“ File not found”, </li></ul></ul></ul><ul><ul><ul><li>“ No read permission for file”, </li></ul></ul></ul><ul><ul><ul><li>“ Insufficient memory”, </li></ul></ul></ul><ul><ul><ul><li>“ No write permission for file” }; </li></ul></ul></ul><ul><ul><ul><li>main() { </li></ul></ul></ul><ul><ul><ul><li>int err_no; </li></ul></ul></ul><ul><ul><ul><li>for ( err_no = 0; err_no &lt; ERRORS; err_no++ ) { </li></ul></ul></ul><ul><ul><ul><li>printf ( “nError message %d is : %sn”, err_no + 1, err_msg[err_no]); } } </li></ul></ul></ul>Practice: 4.5 (Contd.)
    • 25. <ul><li>Consider the following two-d array declaration: </li></ul><ul><ul><li>int num[3][4]= { {3, 6, 9, 12}, {15, 25, 30, 35}, {66, 77, 88, 99} }; </li></ul></ul><ul><ul><li>This statement actually declares an array of 3 pointers (constant) num[0] , num[l] , and num[2] each containing the address of the first element of three single-dimensional arrays. </li></ul></ul><ul><ul><li>The name of the array, num , contains the address of the first element of the array of pointers (the address of num[0] ). </li></ul></ul><ul><ul><li>Here, </li></ul></ul><ul><ul><li>*num is equal to num[0] because num[0] points to num[0][0] . *(*num) will give the value 3. *num is equal to num[0] . </li></ul></ul>Two-Dimensional Arrays and Pointers (Contd.)
    • 26. <ul><li>Pointers can be used to write string-handling functions. </li></ul><ul><ul><li>Consider the following examples: </li></ul></ul><ul><ul><ul><li>/* function to calculate length of a string*/ </li></ul></ul></ul><ul><ul><ul><li>#include &lt;stdio.h&gt; </li></ul></ul></ul><ul><ul><ul><li>main() { </li></ul></ul></ul><ul><ul><ul><li>char *ptr, str[20]; </li></ul></ul></ul><ul><ul><ul><li>int size=0; </li></ul></ul></ul><ul><ul><ul><li>printf(“nEnter string:”); </li></ul></ul></ul><ul><ul><ul><li>gets(str); </li></ul></ul></ul><ul><ul><ul><li>fflush(stdin); </li></ul></ul></ul><ul><ul><ul><li>for(ptr=str ; *ptr != &apos;0&apos;; ptr++) { </li></ul></ul></ul><ul><ul><ul><li>size++; </li></ul></ul></ul><ul><ul><ul><li>} printf(“String length is %d”, size); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>String-Handling Functions Using Pointers
    • 27. <ul><ul><ul><li>/*function to check for a palindrome */ </li></ul></ul></ul><ul><ul><ul><li># include &lt;stdio.h&gt; </li></ul></ul></ul><ul><ul><ul><li>main() { </li></ul></ul></ul><ul><ul><ul><li>char str[50],*ptr,*lptr; </li></ul></ul></ul><ul><ul><ul><li>printf(“nEnter string :”); </li></ul></ul></ul><ul><ul><ul><li>gets(str); fflush(stdin); </li></ul></ul></ul><ul><ul><ul><li>for(lptr=str; *lptr !=’0&apos;; lptr++); /*reach the string terminator */ </li></ul></ul></ul><ul><ul><ul><li>lptr--; /*position on the last character */ </li></ul></ul></ul><ul><ul><ul><li>for(ptr=str; ptr&lt;=lptr; lptr--,ptr++) { </li></ul></ul></ul><ul><ul><ul><li>if(*ptr != *lptr) </li></ul></ul></ul><ul><ul><ul><li>break;} </li></ul></ul></ul><ul><ul><ul><li>if(ptr&gt;lptr) </li></ul></ul></ul><ul><ul><ul><li>printf(“%s is a palindrome” ); </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>printf(“%s is not a palindrome&amp;quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>Using Pointers to Manipulate Character Arrays (Contd.)
    • 28. <ul><li>In this session, you learned that: </li></ul><ul><ul><li>A pointer is a variable, which contains the address of some other variable in memory. </li></ul></ul><ul><ul><li>A pointer may point to a variable of any data type. </li></ul></ul><ul><ul><li>A pointer can point to any portion of the memory. </li></ul></ul><ul><ul><li>A pointer variable is declared as: </li></ul></ul><ul><ul><ul><li>datatype *&lt;pointer variable name&gt; </li></ul></ul></ul><ul><ul><li>A pointer variable is initialized as: </li></ul></ul><ul><ul><ul><li>pointer variable name&gt; = &amp;&lt;variable name to which </li></ul></ul></ul><ul><ul><ul><li>the pointer will point to&gt; </li></ul></ul></ul><ul><ul><li>The &amp; returns the address of the variable. </li></ul></ul><ul><ul><li>The * before a pointer name gives the value of the variable to which it is pointing. </li></ul></ul>Summary
    • 29. <ul><ul><li>Pointers can also be subjected to arithmetic. </li></ul></ul><ul><ul><li>Incrementing a pointer by 1 makes it point to a memory location given by the formula: </li></ul></ul><ul><ul><ul><li>New address = Old address + Scaling factor </li></ul></ul></ul><ul><ul><li>One-dimensional character arrays can be declared by declaring a pointer and initializing it. </li></ul></ul><ul><ul><li>The name of a character array is actually a pointer to the first element of the array. </li></ul></ul><ul><ul><li>Two-dimensional character arrays can also be declared by using pointers. </li></ul></ul>Summary (Contd.)

    ×