lec23-cs110 computational engineering
DESCRIPTION
A keynote on Problem Solving using ComputersTRANSCRIPT
Pointers - Continued
V. Kamakoti
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?
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
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
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?
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
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?
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
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?
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
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);
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
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
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));
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”.
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.
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;