Advanced+pointers

1,598 views

Published on

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

No Downloads
Views
Total views
1,598
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Advanced+pointers

  1. 1.
  2. 2. <ul><li>Declaration: </li></ul><ul><li>returnType (*varName)(parameterTypes); </li></ul><ul><li>Examples: </li></ul><ul><li>int (*f)(int, float); </li></ul><ul><li>int *(*g[])(int, float); </li></ul><ul><li>int *(*g[])(int, float); </li></ul>pointer to a function that takes an integer argument and a float argument and returns an integer pointer to a function that takes an integer argument and a float argument and returns a pointer to an integer An array of pointers to functions – Each function takes an integer argument and a float argument and returns a pointer to an integer
  3. 3. <ul><li>Function Pointers are pointers, i.e. variables, which point to the address of a function. </li></ul><ul><li>A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the  same parameters and return-type! </li></ul><ul><li>Example: Task to perform one of the four basic arithmetic operation </li></ul><ul><li>Solution: i)The task is first solved using a switch- statement. </li></ul><ul><li>ii)Then it is shown, how the same can be done using a function pointer </li></ul>CS 3090: Safety Critical Programming in C
  4. 4. Void Pointers CS 3090: Safety Critical Programming in C
  5. 5. <ul><li>  void  pointer seems to be of limited </li></ul><ul><li>when combined with the ability to cast such a pointer to another type, they turn out to be quite useful and flexible . </li></ul>CS 3090: Safety Critical Programming in C
  6. 6. <ul><li>int * const ptr2  indicates that  ptr2 is a pointer which is constant. This means that ptr2 cannot be made to point to another integer. However the integer pointed by ptr2 can be changed. For eg. int x=10, </li></ul><ul><li>y=20; int * const ptr=&x; *ptr=30; </li></ul><ul><li>ptr=&y; //illegal Where as a Pointer to constant is  const int * ptr1  indicates that ptr1 is a pointer that points to a constant integer. The integer is constant and cannot be changed. However, the pointer ptr1 can be made to point to some other integer. </li></ul><ul><li>For eg. int x=10; y=20; const int *ptr=&x; x=20; ptr=&y; </li></ul><ul><li>*ptr=30; //Illegal </li></ul>
  7. 7. <ul><li>1. Near pointer </li></ul><ul><li>2. Far pointer </li></ul><ul><li>3. Huge pointer </li></ul>CS 3090: Safety Critical Programming in C
  8. 8. <ul><li>Entire RAM has divided in numbers of equal parts, which are known as memory cells. Following diagram represents the 256 MB RAM. </li></ul><ul><li>Each cell can store one-byte data. Data are stored in the  binary number system. That is a character data reserves one memory cell while floating data reserves four memory cells. </li></ul><ul><li>Each memory cell has unique address. Address is always in whole number and must be in increasing order. </li></ul>CS 3090: Safety Critical Programming in C
  9. 9.
  10. 10. <ul><li>int a = 4; </li></ul><ul><li>Q. If you know memory address of first cell is 0x5000 then  what would be the memory address of next memory cell? </li></ul><ul><li>A. It will 5001 since integer data always stores at  continuous memory location and as we know memory address always in increasing order. </li></ul>CS 3090: Safety Critical Programming in C
  11. 11. <ul><li>RAM has divided into two parts: </li></ul><ul><li>(1)        Extended memory (useless) </li></ul><ul><li>(2)        Residence memory   </li></ul><ul><li>When any program is executed it is stored in the residence memory. </li></ul><ul><li>All the c variables are stored in the residence memory. </li></ul><ul><li>Depending upon the compiler the residence memory is defined. For example. In Turbo C 3.0 its 1MB. </li></ul><ul><li>In turbo C 3.0, 20 bits address of the memory cell is known as physical address or real address. In 20 bits, we can represent address   from 0x00000 to 0xFFFFF. That is all c variables must have memory address within this range. </li></ul>
  12. 12. CS 3090: Safety Critical Programming in C
  13. 13. A C programmer cannot not decides what will be the memory address of any variables.
  14. 14. <ul><li>Residential memory of RAM of size 1MB has divided into 16 equal parts. These parts is called segment. Each segment has size is 64 KB. </li></ul><ul><li>16 * 64 KB = 1 MB </li></ul>CS 3090: Safety Critical Programming in C
  15. 15.
  16. 16. <ul><li>Note: In turbo c 3.0 physical addresses of any variables are stored in the 20 bits. But we have not any pointers  of size 20 bits. So pointers cannot access whole residence memory address. To solve this problem we there are three types pointers in c language. They are </li></ul><ul><li>1. Near pointer </li></ul><ul><li>2. Far pointer </li></ul><ul><li>3. Huge pointer </li></ul>CS 3090: Safety Critical Programming in C
  17. 17. <ul><li>Each segment has divided into two parts. </li></ul><ul><li>1. Segment no (4 bit) </li></ul><ul><li>2. Offset address (16 bit) </li></ul>CS 3090: Safety Critical Programming in C
  18. 18. <ul><li>#include<stdio.h> </li></ul><ul><li>int main(){ </li></ul><ul><li>int x; </li></ul><ul><li>printf(&quot;%u &quot;,&x); //To print offset address </li></ul><ul><li>printf(&quot;%p &quot;,x); //To print segment address </li></ul><ul><li>printf(&quot;%p &quot;,&x); //To print offset address </li></ul><ul><li>printf(&quot;%fp &quot;,&x); //To print segment address : offset address </li></ul><ul><li>return 0;} </li></ul>CS 3090: Safety Critical Programming in C
  19. 19. <ul><li>All the segments are used for specific purpose. Like segment number 15 is used for ROM, segment number 14 is used for BIOS etc. </li></ul>CS 3090: Safety Critical Programming in C
  20. 20. CS 3090: Safety Critical Programming in C
  21. 21. <ul><li>Segment number eight has special name which is known as data segment. This segment has been divided into four parts. This is very important for c programming </li></ul>CS 3090: Safety Critical Programming in C
  22. 22. <ul><li>1. Stack area:- All automatic variables are created into stack area.Default storage class of any local variable is auto.This variable may be int, char, float, array, pointer, struct, union etc.It also return function argument and return address.It follow LIFO data structure. It has two part one for initialize variable another for non-initialize variable.All initialize variable are more nearer than uninitialized variable and vice versa. 2. Data area : All static and extern variable are created in the data area. </li></ul>CS 3090: Safety Critical Programming in C
  23. 23. <ul><li>3. Heap area: Malloc and calloc always allocate memory in the heap area.It is used for dynamic memory allocation.It’s size depends upon free space in the memory. 4. Code area : Function pointer can only access code area.Size of this area is always fixed and it is read only area </li></ul>CS 3090: Safety Critical Programming in C
  24. 24. <ul><li>The pointer which can points only 64KB data segment or segment number 8 is known as near pointer. </li></ul>CS 3090: Safety Critical Programming in C
  25. 25. <ul><li>The limitation is that you can only access 64kb of data at a time, because that is the size of a segment - 64kb </li></ul>
  26. 26. <ul><li>The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as far pointer. </li></ul>CS 3090: Safety Critical Programming in C Far Pointer
  27. 27. 09/27/11 CS 3090: Safety Critical Programming in C
  28. 28. <ul><li>Size of far Pointer is 4 byte or 32 bit </li></ul><ul><li>#include<stdio.h> int main(){ int x=10; </li></ul><ul><li>int far *ptr; ptr=&x; </li></ul><ul><li>printf(&quot;%d&quot;,sizeof ptr); return 0;} Output: 4 </li></ul>CS 3090: Safety Critical Programming in C
  29. 29. CS 3090: Safety Critical Programming in C <ul><li>Same as the far Pointer </li></ul><ul><li>They are normalized. </li></ul>
  30. 30. <ul><li>Size of far Pointer is 4 byte or 32 bit </li></ul><ul><li>#include<stdio.h> int main(){ int x=10; </li></ul><ul><li>int far *ptr; ptr=&x; </li></ul><ul><li>printf(&quot;%d&quot;,sizeof ptr); return 0;} Output: 4 </li></ul>CS 3090: Safety Critical Programming in C
  31. 31. <ul><li>int main(int argc, char *argv[]) </li></ul><ul><li>{ ... } </li></ul><ul><li>When main is called, argc will be a count of the number of command-line arguments, and argv will be an array of the arguments themselves. Since each word is a string which is represented as a pointer-to-char, argv is an array-of-pointers-to-char. </li></ul>
  32. 32. #include<stdio.h> int main(int argc, char *argv[]) { printf(&quot;No. of Argument::%d&quot;,argc); int cnt=argc-1; while(cnt>=1) { printf(&quot;nEntered numbers are%s&quot;,argv[cnt]); cnt--; }return 0;}
  33. 33. <ul><li>#include <stdio.h> </li></ul><ul><li>main( int argc, char *argv[] ) </li></ul><ul><li>{ if( argc == 2 ) </li></ul><ul><li>printf(&quot;The argument supplied is %sn&quot;, argv[1]); </li></ul><ul><li>else if( argc > 2 ) </li></ul><ul><li>printf(&quot;Too many arguments supplied.n&quot;); </li></ul><ul><li>else </li></ul><ul><li>printf(&quot;One argument expected.n&quot;); </li></ul><ul><li>} </li></ul>CS 3090: Safety Critical Programming in C
  34. 34. typedef struct IntNode { int value; struct IntNode *next; } INTNODE; INTNODE *search_list(INTNODE *node, int const key) { while (!node) { if (node->value == key) break; node = node->next; } return node; } CS 3090: Safety Critical Programming in C OK, but it only works for nodes containing integer data. If you want a list of strings, you’ll need to define a new type and new function.
  35. 35. typedef struct Node { void *value; struct Node *next; } NODE; void construct_node(NODE *node, void *value, NODE *next) { node->value = value; node->next = next; } NODE *new_node(void *value, NODE *next) { NODE *node = (NODE *)malloc(sizeof(NODE)); construct_node(node, value, next); return node; } CS 3090: Safety Critical Programming in C void* is compatible with any pointer type. So, this member can hold (a pointer to) any value!
  36. 36. <ul><li>What is it that makes the old search_list only work for integers? </li></ul><ul><ul><li>The key parameter is of type int </li></ul></ul><ul><ul><li>The == operator is used to compare int values – </li></ul></ul><ul><ul><li>but == will not work for many types (e.g. structs, strings) </li></ul></ul><ul><li>A solution: pass in an additional argument – </li></ul><ul><li>a comparison function! </li></ul><ul><ul><li>Programmer must supply a comparison function that’s appropriate for the data type being stored in the nodes </li></ul></ul><ul><ul><li>This function argument is called a callback function : </li></ul></ul><ul><ul><ul><li>Caller passes in a pointer to a function </li></ul></ul></ul><ul><ul><ul><li>Callee then “calls back” to the caller-supplied function </li></ul></ul></ul>CS 3090: Safety Critical Programming in C
  37. 37. NODE *search_list(NODE *node, void const *key, int (*compare)(void const *, void const *)) { while (node) { if (!compare(node->value, key)) break; node = node->next; } return node; } CS 3090: Safety Critical Programming in C Assumption: compare returns zero if its parameter values are equal; nonzero otherwise
  38. 38. <ul><li>If our nodes hold strings, we have a compare function already defined: strcmp or strncmpy </li></ul><ul><li>#include <string.h> </li></ul><ul><li>… </li></ul><ul><li>match = search_list(root, &quot;key&quot;, &strcmp); </li></ul>CS 3090: Safety Critical Programming in C Note: you may get a warning, since strcmp is not strictly of the right type: its parameters are of type char * rather than void * & is optional here – compiler will implicitly take the address
  39. 39. <ul><li>If our nodes hold other kinds of data, we may need to “roll our own” compare function </li></ul><ul><li>int compare_ints(void const *a, void const *b) { </li></ul><ul><li>const int ia = *(int *)a, ib = *(int *)b; </li></ul><ul><li>return ia != ib; </li></ul><ul><li>} </li></ul><ul><li>… </li></ul><ul><li>match = search_list(root, key, &compare_ints); </li></ul>CS 3090: Safety Critical Programming in C
  40. 40. <ul><li>In some cases, a nice alternative to long, repetitive switch statements, like this: </li></ul><ul><li>double add(double, double); </li></ul><ul><li>double sub(double, double); </li></ul><ul><li>double mul(double, double); </li></ul><ul><li>double div(double, double); </li></ul><ul><li>switch(oper) { </li></ul><ul><li>case ADD: result = add(op1, op2); break; </li></ul><ul><li>case SUB: result = sub(op1, op2); break; </li></ul><ul><li>case MUL: result = mul(op1, op2); break; </li></ul><ul><li>case DIV: result = div(op1, op2); break; </li></ul><ul><li>} </li></ul>CS 3090: Safety Critical Programming in C
  41. 41. <ul><li>Jump table alternative: </li></ul><ul><li>double add(double, double); </li></ul><ul><li>double sub(double, double); </li></ul><ul><li>double mul(double, double); </li></ul><ul><li>double div(double, double); </li></ul><ul><li>double (*oper_func[])(double, double) = { </li></ul><ul><li>add, sub, mul, div </li></ul><ul><li>}; </li></ul><ul><li>result = oper_func[oper](op1, op2); </li></ul>CS 3090: Safety Critical Programming in C Array of pointers to functions. Each function takes two double s and returns a double
  42. 42. <ul><li>What if uninitialized function pointer value is accessed? </li></ul><ul><ul><li>Safest outcome: memory error, and program is terminated </li></ul></ul><ul><ul><li>But what if the “garbage” value is a valid address? </li></ul></ul><ul><ul><ul><li>Worst case: address contains program instruction – </li></ul></ul></ul><ul><ul><ul><li>execution continues, with random results </li></ul></ul></ul><ul><ul><ul><li>Hard to trace the cause of the erroneous behavior </li></ul></ul></ul>CS 3090: Safety Critical Programming in C
  43. 43. <ul><li>C programs can be called from the command line, with certain arguments entered along with the program name: </li></ul><ul><li>e.g. Registration program register </li></ul><ul><li>You may register with an existing ID by the </li></ul><ul><li>– i option: register -i ID </li></ul><ul><li>Otherwise, an ID will be generated </li></ul>CS 3090: Safety Critical Programming in C
  44. 44. <ul><li>main function can be declared with two arguments: </li></ul><ul><li>int main(int argc, char **argv) </li></ul><ul><li>argc holds the number of arguments </li></ul><ul><li>argv is an array of strings: the n th command line string is stored at argv[n-1] </li></ul><ul><li> register -i wallace </li></ul>CS 3090: Safety Critical Programming in C 3 argc argc [0] [1] [2] [3] NUL
  45. 45. int main(int argc, char **argv) { char id[ID_LIMIT]; switch(argc) { case 1: generate_ID(id); break; case 3: if (strcmp(argv[1], &quot;-i&quot;) exit(INVALID_ARG); strcpy(id, argv[2]); break; default: exit(INVALID_ARG_COUNT); } register(id); } CS 3090: Safety Critical Programming in C
  46. 46. <ul><li>The Function Pointer Tutorials. http://www.newty.de/fpt/index.html </li></ul>CS 3090: Safety Critical Programming in C

×