Successfully reported this slideshow.
Your SlideShare is downloading. ×

An imperative study of c

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Fp201 unit5 1
Fp201 unit5 1
Loading in …3
×

Check these out next

1 of 76 Ad

More Related Content

Slideshows for you (20)

Advertisement

More from Tushar B Kute (20)

Advertisement

Recently uploaded (20)

An imperative study of c

  1. 1. Tushar B. Kute, Department of Information Technology, Sandip Institute of Technology and Research Centre, Nashik An Imperative Study of ‘C’
  2. 2. Outline <ul><li>Basic programming concepts. </li></ul><ul><li>Decision making statements. </li></ul><ul><li>Arrays and Strings. </li></ul><ul><li>Functions. </li></ul><ul><li>Structures. </li></ul><ul><li>Pointers. </li></ul>
  3. 3. Why ‘C’ only?
  4. 4. Problem Solving <ul><li>Analysis </li></ul><ul><li>Design </li></ul><ul><li>Code </li></ul><ul><li>Maintain </li></ul><ul><li>Test </li></ul>
  5. 5. Two views of program user interface software layers hidden by user interface user’s view programmer’s view
  6. 6. Alphabets, Digits, Special Symbols. Constants, Variables, And Keywords Instructions Program. Forming a C program
  7. 7. Compiling a C program
  8. 8. Basic Programming Concepts <ul><li>Constants. </li></ul><ul><li>Data Types. </li></ul><ul><li>Keywords. </li></ul><ul><li>Operators. </li></ul><ul><ul><li>Arithmetic </li></ul></ul><ul><ul><li>Logical </li></ul></ul><ul><ul><li>Bitwise </li></ul></ul><ul><ul><li>Relational </li></ul></ul><ul><ul><li>Conditional </li></ul></ul>
  9. 9. Sample C Program <ul><li>#include<stdio.h> </li></ul><ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li> int i = 10, j = 12; </li></ul><ul><li> float k, m = 12.6; </li></ul><ul><li> k = (i + j) / m; </li></ul><ul><li> printf(“Input : %d %d %f”, i, j, m); </li></ul><ul><li> printf(“Output : %f ”, k); </li></ul><ul><li>} </li></ul>
  10. 10. Control Statements <ul><li>Selection Statements </li></ul><ul><ul><li>Using if and if...else </li></ul></ul><ul><ul><li>Nested if Statements </li></ul></ul><ul><ul><li>Using switch Statements </li></ul></ul><ul><ul><li>Conditional Operator </li></ul></ul><ul><li>Repetition Statements </li></ul><ul><ul><li>Looping: while , do-while , and for </li></ul></ul><ul><ul><li>Nested loops </li></ul></ul><ul><ul><li>Using break and continue </li></ul></ul>
  11. 11. if Statements <ul><li>if (Condition) </li></ul><ul><li>{ </li></ul><ul><li>statement(s); </li></ul><ul><li>} </li></ul><ul><li>Example: </li></ul><ul><li>if (i > 0) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;i = %d “, i ); </li></ul><ul><li>} </li></ul>Condition ? statement1 statement2 statement3 true false
  12. 12. The if...else Statement <ul><li>if (condition) </li></ul><ul><li>{ </li></ul><ul><li>statement(s)-for-the-true-case; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>statement(s)-for-the-false-case; </li></ul><ul><li>} </li></ul>
  13. 13. if...else Example <ul><li>if (radius >= 0) </li></ul><ul><li>{ </li></ul><ul><li>area = radius*radius*PI; </li></ul><ul><li>printf(&quot;The area for the circle of radius %d is =%d&quot; , radius, area); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>printf(“Radius can not be Negative &quot;); </li></ul><ul><li>} </li></ul>
  14. 14. Multiple Alternative if Statements <ul><li>if (score >= 90) </li></ul><ul><li>grade = ‘A’; </li></ul><ul><li>else </li></ul><ul><li>if (score >= 80) </li></ul><ul><li>grade = ‘B’; </li></ul><ul><li>else </li></ul><ul><li>if (score >= 70) </li></ul><ul><li>grade = ‘C’; </li></ul><ul><li>else </li></ul><ul><li>if (score >= 60) </li></ul><ul><li>grade = ‘D’; </li></ul><ul><li>else </li></ul><ul><li>grade = ‘F’; </li></ul>if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; else if (score >= 60) grade = ‘D’; else grade = ‘F’;
  15. 15. switch Statements <ul><li>switch (variable-name) </li></ul><ul><li>{ </li></ul><ul><li>case value1: </li></ul><ul><li> Execute this; </li></ul><ul><li>break; </li></ul><ul><li>case value2: </li></ul><ul><li> Execute this; </li></ul><ul><li>break; </li></ul><ul><li>case valueN: </li></ul><ul><li> Execute this; </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>Execute this; </li></ul><ul><li>} </li></ul>
  16. 16. switch Statement Flow Chart
  17. 17. Repetitions <ul><li>while Loops </li></ul><ul><li>do-while Loops </li></ul><ul><li>for Loops </li></ul><ul><li>break and continue </li></ul>
  18. 18. while Loop while (continuation-condition) { // loop-body; }
  19. 19. while Loop Flow Chart, cont. int i = 0; while (i < 100) { printf(&quot;Welcome to C!&quot;); i++; }
  20. 20. do-while Loop do { // Loop body; } while (continue-condition);
  21. 21. for Loop for (initialization; condition; increment/decrement) { //loop body; }
  22. 22. Caution <ul><li>Adding a semicolon at the end of the for clause before the loop body is a common mistake, as shown below: </li></ul><ul><li>for (int i=0; i<10; i++); </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;i is %d“,i); </li></ul><ul><li>} </li></ul>Wrong
  23. 23. Caution, cont. <ul><li>Similarly, the following loop is also wrong: </li></ul><ul><li>int i=0; </li></ul><ul><li>while (i<10); </li></ul><ul><li>{ </li></ul><ul><li> printf(&quot;i is %d“,i); </li></ul><ul><li> i++; </li></ul><ul><li>} </li></ul><ul><li>In the case of the do loop, the following semicolon is needed to end the loop. </li></ul><ul><li>int i=0; </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li> printf(&quot;i is %d“,i); </li></ul><ul><li> i++; </li></ul><ul><li>} while (i<10); </li></ul>Wrong Correct
  24. 24. Which Loop to Use?
  25. 25. The break Keyword
  26. 26. Example: break statement <ul><li>int a = 10; </li></ul><ul><li>while( a >= 0 ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“Value of a = %d”,a); </li></ul><ul><li>a--; </li></ul><ul><li>if(a==5) </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>Output: </li></ul><ul><li>Value of a = 10 </li></ul><ul><li>Value of a = 9 </li></ul><ul><li>Value of a = 8 </li></ul><ul><li>Value of a = 7 </li></ul><ul><li>Value of a = 6 </li></ul>
  27. 27. The continue Keyword
  28. 28. Example: continue statement <ul><li>int a = 6; </li></ul><ul><li>while( a >= 0 ) </li></ul><ul><li>{ </li></ul><ul><li>a--; </li></ul><ul><li>if(a==3) </li></ul><ul><li>continue; </li></ul><ul><li>printf(“Value of a = %d”,a); </li></ul><ul><li>} </li></ul>
  29. 29. Arrays Array is a data structure that represents a collection of the same types of data. An Array of 10 Elements of type int
  30. 30. Declaring Array Variables <ul><li>datatype arrayname[index]; </li></ul><ul><li>Example: </li></ul><ul><li>int list[10]; </li></ul><ul><li> char num[15]; </li></ul><ul><li>float hat[20]; </li></ul>
  31. 31. Creating Arrays <ul><li>datatype array-name[size]; </li></ul><ul><li>Example: </li></ul><ul><li>int num[10]; </li></ul><ul><li>num[0] references the first element in the array. </li></ul><ul><li>num[9] references the last element in the array. </li></ul>
  32. 32. Declaring and Creating in One Step <ul><li>datatype arrayname[arraySize]= {values seperated by comma}; </li></ul><ul><li>Example : </li></ul><ul><li>char c[5] = {‘a’,‘F’,‘4’,‘=’,‘n’}; </li></ul><ul><li>int i[4] = {12,15,0,2}; </li></ul>
  33. 33. The Length of Arrays <ul><li>Once an array is created, its size is fixed. It cannot be changed. </li></ul><ul><li>For example , </li></ul><ul><li>int arr[10]; </li></ul><ul><li>You can not insert any number to arr[11] </li></ul><ul><li>location because it is not initialized. </li></ul>
  34. 34. Declaring, creating, initializing Using the Shorthand Notation <ul><li>float list[4] = {1.9, 2.9, 3.4, 3.5}; </li></ul><ul><li>This shorthand notation is equivalent to the following statements: </li></ul><ul><li>float list[4]; </li></ul><ul><li>list[0] = 1.9; </li></ul><ul><li>list[1] = 2.9; </li></ul><ul><li>list[2] = 3.4; </li></ul><ul><li>list[3] = 3.5; </li></ul>
  35. 35. CAUTION <ul><li>Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: </li></ul><ul><li>float list; </li></ul><ul><li>list = {1.9, 2.9, 3.4, 3.5}; </li></ul>
  36. 36. Copying Arrays <ul><li>With direct assignment: </li></ul><ul><li>int array1[5] = {2, 3, 1, 5, 10}; </li></ul><ul><li>int array2[5]; </li></ul><ul><li>array2 = array1; </li></ul>
  37. 37. Multidimensional Arrays <ul><li>Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays </li></ul><ul><li>int matrix[10][10]; </li></ul><ul><li>for (i=0; i<10; i++) </li></ul><ul><li>for (j=0; j<10; j++) </li></ul><ul><li>{ </li></ul><ul><li>matrix[i][j] = i * j; </li></ul><ul><li>} </li></ul><ul><li>float mat[5][5]; </li></ul>
  38. 38. Multidimensional Array Illustration
  39. 39. Function
  40. 40. What is a Function ? <ul><li>Function is a self contained block of statements that perform a coherent task of some kind. </li></ul><ul><li>Every C program can be a thought of the collection of functions. </li></ul><ul><li>main( ) is also a function. </li></ul>
  41. 41. Types of Functions <ul><li>Library functions. </li></ul><ul><li>These are the in-built functions of ‘C’ library. These are already defined in header files. </li></ul><ul><li>e.g. printf( ); is a function which is used to print at output. It is defined in ‘stdio.h’ file . </li></ul><ul><li>User defined functions. </li></ul><ul><li>Programmer can create their own function in C to perform specific task. </li></ul>
  42. 42. User defined functions <ul><li>e.g. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>message( ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“Hello”); </li></ul><ul><li>} </li></ul><ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>message( ); </li></ul><ul><li>} </li></ul>
  43. 43. Declaration of many functions. <ul><li>#include<stdio.h> </li></ul><ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“I am in main”); </li></ul><ul><li>poly( ); </li></ul><ul><li>engg( ); </li></ul><ul><li>agri( ); </li></ul><ul><li>} </li></ul><ul><li>poly( ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“I am in polytechnic”); </li></ul><ul><li>} </li></ul><ul><li>engg( ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“I am in engineering”); </li></ul><ul><li>} </li></ul><ul><li>agri( ) </li></ul><ul><li>{ </li></ul><ul><li>printf(“I am in agri”); </li></ul><ul><li>} </li></ul>
  44. 44. Some conclusions <ul><li>Any C Program must contain at least one function. </li></ul><ul><li>If program contains only one function it must be </li></ul><ul><li>main( ). </li></ul><ul><li>There is no limit on the number of functions present in a C program. </li></ul><ul><li>Each function in a program is called in the sequence specified by functions call in main( ). </li></ul><ul><li>After each function has done its thing, control returns to main( ). When main( ) run out of function calls program ends. </li></ul>
  45. 45. Summarization <ul><li>C Program is a collection of one or more functions. </li></ul><ul><li>A function gets called when its name is followed by a semicolon. </li></ul><ul><li>e.g. main( ) </li></ul><ul><li> { </li></ul><ul><li>fun( ); </li></ul><ul><li> } </li></ul><ul><li> fun( ) </li></ul><ul><li> { </li></ul><ul><li>statement1; </li></ul><ul><li>statement2; </li></ul><ul><li>statement3; </li></ul><ul><li> } </li></ul>
  46. 46. Summarization Contd………. <ul><li>Any function can be called by any other function. </li></ul><ul><ul><li>e.g. main( ) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>printf(“I am in main.”); </li></ul></ul><ul><ul><li>fun( ); </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li> fun( ) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>printf(“I am in fun.”); </li></ul></ul><ul><ul><li>main( ); </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li>This will run in infinity. </li></ul></ul>
  47. 47. Summarization Contd………. <ul><li>A function can be called by number of times. </li></ul><ul><ul><li>e.g. main( ) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>printf(“I am in main.”); </li></ul></ul><ul><ul><li>fun( ); </li></ul></ul><ul><ul><li>fun( ); </li></ul></ul><ul><ul><li>fun( ); </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li> fun( ) </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li>printf(“I am in fun.”); </li></ul></ul><ul><ul><li> } </li></ul></ul>
  48. 48. Summarization Contd………. <ul><li>A function can call itself, it is called as a ‘recursion’. </li></ul><ul><li>e.g. main ( ) </li></ul><ul><li> { </li></ul><ul><li>printf(“I am in main”); </li></ul><ul><li>main( ); </li></ul><ul><li> } </li></ul><ul><li>A function can be called from another function but can not be defined in another function. It should be defined outside of the main. </li></ul>
  49. 49. Why use functions ? <ul><li>Writing functions avoids rewriting of the same code again and again in the program. </li></ul><ul><li>Using a function it becomes easier to write program to keep track of what they are doing. </li></ul>
  50. 50. Passing values to functions. <ul><li>The values that we pass to the function called as function arguments. </li></ul><ul><li>e.g. main( ) </li></ul><ul><li> { </li></ul><ul><li>int i=10, j=12, k=20; </li></ul><ul><li> calsum(i,j,k); </li></ul><ul><li>} </li></ul><ul><li> calsum(int x, int y, int z) </li></ul><ul><li>{ </li></ul><ul><li> printf(“Sum is : %d”,(x+y+z)); </li></ul><ul><li> } </li></ul>i, j, k are actual arguments x, y, z are formal arguments.
  51. 51. Returning a value <ul><li>The keyword ‘return’ to used to return value from function which is present in the parenthesis of return. </li></ul><ul><li>On executing return statement it immediately transfers control back to the main program. </li></ul><ul><li>e.g. main( ) </li></ul><ul><li>{ </li></ul><ul><li>int a = 30, j = 14; </li></ul><ul><li>int k; </li></ul><ul><li>k = addin(a, j); </li></ul><ul><li>printf(“%d”, k); </li></ul><ul><li>} </li></ul><ul><li>int addin(int x, int y) </li></ul><ul><li>{ </li></ul><ul><li>int z; </li></ul><ul><li>z = (x + y) +1; </li></ul><ul><li>return(z); </li></ul><ul><li>} </li></ul>
  52. 52. return <ul><li>There is no restriction of return statements in a function. </li></ul><ul><li>Whenever the control returns from the function some value is definitely returned. It should be accepted in the calling program. </li></ul><ul><li>e.g. int sum; </li></ul><ul><li>sum = calsum(a,b,c); </li></ul><ul><li>here the value returned by calsum must be of type int. </li></ul><ul><li>Some valid return statements. </li></ul><ul><li>return a; /*a is a variable name */ </li></ul><ul><li>return (23); </li></ul><ul><li>return (15.32); </li></ul><ul><li>return (‘v’); </li></ul><ul><li>return; </li></ul><ul><li>If you are not returning any value from a function they it should be declared as void. (void is a keyword). </li></ul><ul><li>e.g. void display( ) </li></ul><ul><li>display is not returning any value. </li></ul>
  53. 53. Function declaration and Prototype. <ul><li>Any C function by default returns and integer value. More specifically, whenever a call is made to a function, the compiler assumes that this function would return value of type int. </li></ul><ul><li>Function prototype : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>int addin(int, int); </li></ul><ul><li>void main( ) </li></ul><ul><li>{ </li></ul><ul><li>statements; </li></ul><ul><li>statements; </li></ul><ul><li>} </li></ul><ul><li>int addin(int a, int b) </li></ul><ul><li>{ </li></ul><ul><li> statements; </li></ul><ul><li>} </li></ul>Function prototype.
  54. 54. Structures <ul><li>In general, we can call a structure is a collection of different types of data. </li></ul><ul><li>A Structure contains number of data types grouped together. These data types may be or may not be of same data type. </li></ul>
  55. 55. ‘ C’ implementation of Structure. <ul><li>The keyword ‘struct’ is used for creating a structure. </li></ul><ul><li>Syntax : </li></ul><ul><li>struct structure-name </li></ul><ul><li>{ </li></ul><ul><li>datatype1 varname1; </li></ul><ul><li>datatype1 varname2; </li></ul><ul><li>datatype1 varname3; </li></ul><ul><li>}; </li></ul><ul><li>creating the object of structure : </li></ul><ul><li>struct structure-name var1, var2, var3; </li></ul>
  56. 56. Accessing structure elements <ul><li>. (dot operator) is used to access individual structure element. </li></ul><ul><li>e.g. struct list </li></ul><ul><li>{ </li></ul><ul><li>int roll; </li></ul><ul><li>char name[10]; </li></ul><ul><li>float marks; </li></ul><ul><li>}; </li></ul><ul><li>struct list a , b , c; </li></ul><ul><li>a.roll – is the integer element of structure a. </li></ul><ul><li>a.name – is char array element of structure a </li></ul><ul><li>b.marks – is a float element of structure b. </li></ul><ul><li>a.marks – is a float element of structure b. </li></ul><ul><li>scanf(“%d”, &b.roll); this statement can accept an integer roll of structure b from user. This is applied to all the elements of a structure. </li></ul>
  57. 57. How structure elements are stored <ul><li>e.g. struct book </li></ul><ul><li>{ </li></ul><ul><li>char name; </li></ul><ul><li>int pages; </li></ul><ul><li>float price; </li></ul><ul><li>}; </li></ul><ul><li>struct book z = {‘s’, 125, 90.0}; </li></ul><ul><li>Here value of : </li></ul><ul><li>z.name is ‘s’. </li></ul><ul><li>z.pages is 125 and z.price is 90.0 </li></ul><ul><li>Memory map: </li></ul>z.name z.pages z.price ‘ s’ 125 90.0
  58. 58. Valid declaration <ul><li>struct list { </li></ul><ul><li>int roll; </li></ul><ul><li>char name[10]; </li></ul><ul><li>float marks; </li></ul><ul><li> }; </li></ul><ul><li>struct list a , b , c; </li></ul><ul><li>It is equivalent to : </li></ul><ul><li>struct list { </li></ul><ul><li>int roll; </li></ul><ul><li>char name[10]; </li></ul><ul><li>float marks; </li></ul><ul><li> }a, b, c; </li></ul>
  59. 59. Remember: <ul><li>The closing bracket of structure type declaration must be followed by a semicolon. </li></ul><ul><li>Usually structure declaration appears at the top of the source code file before any variables and structures are defined. </li></ul><ul><li>Accessing single variable in a structures must be followed by a . (dot operator). </li></ul>
  60. 60. Example: Creating a student database of 3 students. <ul><li>#include<stdio.h> </li></ul><ul><li>void main( ) </li></ul><ul><li>{ </li></ul><ul><li>struct student { </li></ul><ul><li>int roll; </li></ul><ul><li>char name[10]; </li></ul><ul><li>float marks; </li></ul><ul><li> } a, b, c; </li></ul><ul><li>printf(“Enter all information of students:” ); </li></ul><ul><li>scanf(“%d %s %f”, &a.roll, a.name, &a.marks); </li></ul><ul><li>scanf(“%d %s %f”, &b.roll, b.name, &b.marks); </li></ul><ul><li>scanf(“%d %s %f”, &c.roll, c.name, &c.marks); </li></ul><ul><li>printf(“You entered this information:”); </li></ul><ul><li>printf(“%d %s %f”, a.roll, a.name, a.marks); </li></ul><ul><li>printf(“%d %s %f”, b.roll, b.name, b.marks); </li></ul><ul><li>printf(“%d %s %f”, c.roll, c.name, c.marks); </li></ul><ul><li>} </li></ul>
  61. 61. Array of structures <ul><li>We can create the array of structures. Thus, we can use the same structure for more than one variables which adds more flexibility in your program. Let’s view the previous example. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main( ) </li></ul><ul><li>{ </li></ul><ul><li>struct student { </li></ul><ul><li>int roll; </li></ul><ul><li>char name[10]; </li></ul><ul><li>float marks; </li></ul><ul><li> } a[10]; </li></ul><ul><li>int j; </li></ul><ul><li>printf(“Enter all information of students:” ); </li></ul><ul><li>for(j = 0 ; j < 10 ; j++) </li></ul><ul><li>scanf(“%d %s %f”, &a.roll[i], a.name[i], &a.marks[i]); </li></ul><ul><li>for(j = 0 ; j < 10 ; j++) </li></ul><ul><li>printf(“%d %s %f”, a.roll[i], a.name[i], a.marks[i]); </li></ul><ul><li>} </li></ul>
  62. 62. Memory map for arrays a[i].roll a[i].name a[i].marks i 2 1 9 3 0 7 6 5 4 8 a[5].roll a[9].name
  63. 63. Pointers
  64. 64. Pointer Variables <ul><li>Pointers are often referred to as references </li></ul><ul><li>The value in a pointer variable is interpreted as a memory address </li></ul><ul><li>Usually, pointer variables hold references to specific kinds of data ( e.g.: address of an int , address of a char , etc) </li></ul>int * p; /* variable p can hold the address of a memory location that contains an int */ char * chptr; /* chptr can hold the address of a memory location that contains a char */
  65. 65. Dereferencing Operator <ul><li>The expression *p denotes the memory cell to which p points </li></ul><ul><li>Here, * is called the dereferencing operator </li></ul><ul><li>Be careful not to dereference a pointer that has not yet been initialized: </li></ul>int *p; p ? *p = 7; Address in p could be any memory location Attempt to put a value into an unknown memory location will result in a run-time error , or worse, a logic error
  66. 66. <ul><li>To help keep things straight, it’s useful to pretend there are parentheses in the pointer declarations: </li></ul><ul><ul><li>(int *) p; means that p is of type int* ( i.e.: that is, p is a pointer to an int location) </li></ul></ul><ul><ul><li>int (*p); means that location *p has the potential to hold an int value </li></ul></ul><ul><li>Don’t actually write declarations this way </li></ul>Pointer
  67. 67. The Address Operator <ul><li>The expression &x denotes the address of a variable x </li></ul><ul><li>Here, & is called the address operator or the reference operator </li></ul>int x, *p; p ? x ? p = &x; *p = 4; p x ? p x 4 Value of x has been changed Value of p has been changed
  68. 68. The Null Pointer <ul><li>The null pointer is a special constant which is used to explicitly indicate that a pointer does not point anywhere </li></ul><ul><ul><li>NULL is defined in the standard library <stdlib.h> </li></ul></ul>
  69. 69. Pointer Example int *p, x, y, *q = NULL; p = &x; *p = 4; p x (or *p) y 4 ? p = &y; p x y 4 ? *p is now another name for y q q . .
  70. 70. *p = 8; p x y 4 8 q = p; p x y 4 8 q *p or *q q *p .
  71. 71. p = &x; p x y 4 8 *q *p = *q; p x y 8 8 q *p q *p *q 8
  72. 72. Arrays of Pointers <ul><li>It’s possible to have arrays of pointers </li></ul><ul><li>The array name is a pointer to an array of pointers: </li></ul>int j = 6; k = 4; int * arrayOfPtr[4]; j k 6 4 arrayOfPtr ? ? ? ? 0 1 2 3 Pointers in array are not initialized yet
  73. 73. arrayOfPtr[0] = &k; arrayOfPtr[2]=&j; j k 6 4 arrayOfPtr ? 0 1 2 3 ?
  74. 74. Array Names as Pointers <ul><li>Array name is really a pointer to the first element in the array </li></ul><ul><li>Consider the declaration int arr[5]; </li></ul><ul><ul><li>arr has the same meaning as &arr[0] </li></ul></ul><ul><ul><li>*arr has the same meaning as arr[0] </li></ul></ul><ul><ul><li>Indexing into an array is really a pointer dereferencing operation </li></ul></ul>
  75. 75. Functions Calls <ul><li>Call by value. </li></ul><ul><li>Call by reference. </li></ul>
  76. 76. References <ul><li>“ Let us C” by Yashwant Kanetkar, BPB Publications. </li></ul><ul><li>“ Programming in ANSI C” by E Balagurusamy, Tata McGraw Hill Publishing. </li></ul><ul><li>“ How to Program: C”, by Deital and Deital, Prentice Hall of India. </li></ul><ul><li>“ The Complete Reference: C” by Herbert Schildt, McGraw Hill International Publishing. </li></ul>

×