Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lec23-CS110 Computational Engineering

228 views

Published on

A keynote on Problem Solving using Computers

Published in: Education
  • Be the first to comment

  • Be the first to like this

Lec23-CS110 Computational Engineering

  1. 1. Pointers - Continued V. Kamakoti
  2. 2. Let us revise #include <stdio.h> main() { What is the output? int u1, u2; int v = 3; int *pv; u1 = 2* (v + 5); pv = &v; u2 = 2*(*pv + 5); printf(“n u1=%d u2=%d”,u1,u2); }
  3. 3. Let us revise #include <stdio.h> main() { u1 = 16 u2 = 16 int u1, u2; int v = 3; int *pv; u1 = 2* (v + 5); pv = &v; u2 = 2*(*pv + 5); printf(“n u1=%d u2=%d”,u1,u2); }
  4. 4. Let us revise char my_word[50]; int a[100]; scanf(“%s %d”,my_word,&a[50]); Note: No “&” for arrays, but “&” required for reading an element of an array
  5. 5. Passing partial arrays #include<stdio.h> main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]); } What is the output? void process(int a[]) { a[0] = 5; a[1] = 6; }
  6. 6. Passing partial arrays #include<stdio.h> main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]); } 56 void process(int a[]) { a[0] = 5; a[1] = 6; }
  7. 7. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); printf(“%d %d”,a[0],a[1]); } What is the output? int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); }
  8. 8. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); printf(“%d %d”,a[0],a[1]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } 56
  9. 9. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); z[3] = 8; printf(“Output: %d”,a[3]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } What is the Output?
  10. 10. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); z[3] = 8; printf(“Output: %d”,a[3]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } Output: 8
  11. 11. Pointers and 1-D arrays • • • • • int x[100]; x : pointer to x[0] - say address 1002 x + 1: pointer to x[1] - address 1006 x + 2: pointer to x[2] - address 1010 x + i: pointer to x[i] = 1000 + i * sizeof(int); = Base Address + i * sizeof(int);
  12. 12. What happens main() { int z[100]; scanf(“%d”, z+5); printf(“%d %d”,z[5],*(z+5)); } If you enter 10, the code outputs 10 10
  13. 13. What is missing? • You should know the size of the array while writing the code • Else space allocation cannot be done • It is a limitation to the user of the code • Any solution? – Yes - through Dynamic Memory Allocation
  14. 14. These are Equivalent • int x[10]; • Another way – #define SIZE 10 – int x[SIZE] • int x[10] = {1,2,3,5,6,7,8,9,10,11}; • int x[] = {1,2,3,4,5,6,7,8,9,10,11}; • Another way – int *x; – x = (int *) malloc(10 * sizeof(int));
  15. 15. These are Equivalent – int *x; – x = malloc(10 * sizeof(int)); – This shall return a pointer to character inconsistent with definition of “x” - which is an integer pointer. – x = (int *) malloc(10* sizeof(int)); – This is called “Pointer Casting”.
  16. 16. Look at this main() { int p; int *q; printf(“Enter size of arrayn”); scanf(“%d”,&p); q = (int *) malloc(p * sizeof(int)); } Creates an array of size as desired by user.
  17. 17. So What? int line[80]; int *pl; line[2] = line[1]; //is equivalent to *(line + 2) = line[1]; *(line + 2) = *(line + 1); line[2] = *(line + 1); p1 = &line[1]; //Is equivalent to p1 = line + 1;

×