lec23-cs110 computational engineering

17

Click here to load reader

Upload: sriharsha-p

Post on 22-May-2015

50 views

Category:

Education


0 download

DESCRIPTION

A keynote on Problem Solving using Computers

TRANSCRIPT

Page 1: Lec23-CS110 Computational Engineering

Pointers - Continued

V. Kamakoti

Page 2: Lec23-CS110 Computational Engineering

Let us revise#include <stdio.h>main() { 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);}

What is the output?

Page 3: Lec23-CS110 Computational Engineering

Let us revise#include <stdio.h>main() { 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);}

u1 = 16 u2 = 16

Page 4: Lec23-CS110 Computational Engineering

Let us revise

char my_word[50];

int a[100];

scanf(“%s %d”,my_word,&a[50]);

Note: No “&” for arrays, but “&” required forreading an element of an array

Page 5: Lec23-CS110 Computational Engineering

Passing partial arrays

#include<stdio.h>main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]);}

void process(int a[]) { a[0] = 5; a[1] = 6;}

What is the output?

Page 6: Lec23-CS110 Computational Engineering

Passing partial arrays

#include<stdio.h>main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]);}

void process(int a[]) { a[0] = 5; a[1] = 6;}

5 6

Page 7: Lec23-CS110 Computational Engineering

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 );}

What is the output?

Page 8: Lec23-CS110 Computational Engineering

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 );}

5 6

Page 9: Lec23-CS110 Computational Engineering

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?

Page 10: Lec23-CS110 Computational Engineering

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

Page 11: Lec23-CS110 Computational Engineering

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);

Page 12: Lec23-CS110 Computational Engineering

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

Page 13: Lec23-CS110 Computational Engineering

What is missing?

• You should know the size of the arraywhile 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

Page 14: Lec23-CS110 Computational Engineering

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));

Page 15: Lec23-CS110 Computational Engineering

These are Equivalent– int *x;– x = malloc(10 * sizeof(int));– This shall return a pointer to character -

inconsistent with definition of “x” - which is aninteger pointer.

– x = (int *) malloc(10* sizeof(int));– This is called “Pointer Casting”.

Page 16: Lec23-CS110 Computational Engineering

Look at thismain() { int p; int *q; printf(“Enter size of array\n”); scanf(“%d”,&p); q = (int *) malloc(p * sizeof(int));}

Creates an array of size as desired by user.

Page 17: Lec23-CS110 Computational Engineering

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;