MA 511: Computer Programming
Lecture 10: Pointers (Contd..)
http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html
Partha Sarathi Mandal
[email protected] Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Passing Arguments to Function
• Passing values (call by value)
• Passing pointers (call by reference)
• Passing a pointer to an array
• Passing a pointer to a character string
• Passing a pointer to a character
MA511: Computer Programming
Partha S Mandal, IITG
Example: A function can return a pointer
int *func(int *p);
main(){ int *func(int *p){
int a[5]={10,20,30,40,50}; int i, imax, max = 0;
int *maxptr; for(i=0; i<5; i++){
maxptr = func(a); if(*(p+i) > max){
printf(“max=%d”, *maxptr); max =*(p+i);
} imax = i;
}
}
return(p+imax);
}
MA511: Computer Programming
Partha S Mandal, IITG
Example: Passing function to other function
float guest1(float x, float y);
float guest2(float x, float y);
float host(float (*pt) (float x, float y));
float guest1(float x, float y){
main(){
float x, y; return(x+y);
}
x = host(guest1); float guest2(float x, float y){
printf(“x = %f\n",x);
y = host(guest2); return(x*y);
printf(“y = %f\n",y); }
} /* end of main */ float host(float (*pt) (float x, float y)){
float a=5.0, b=3.5, c;
c=(*pt)(a,b);
return(c);
}
Dynamic memory allocation
int x[10];
Or
int *x;
x = (int *) malloc(10*sizeof(int));
• malloc library function reserves a block of
memory size is equivalent to 10 integer for the
above example.
• stdlib.h or malloc.h header file is required.
• malloc return a pointer to the beginning of the
allocated space.
• in general malloc(a) allocate a bytes of memory.
Example: Dynamic memory in 1D
#include<stdio.h>
#include<stdlib.h>
main(){
int *a, i, n;
printf("Type n");
scanf("%d",&n);
a = (int *)malloc(n*sizeof(int));
for(i=0; i<n; i++){
printf("type %d th entry",i);
scanf("%d", a+i);
}
for(i=0; i<n; i++){
printf("%d", *(a+i));
}
}
Example: Dynamic memory in 2D
#include<stdio.h>
#include<stdlab.h>
#define COL 50
void read_ip(int *a[COL], int nrow, int ncol); void read_ip(int *a[ROW], int m, int n){
void write_op(int *a[COL], int nrow, int ncol); int row, col;
main(){ for(row =0; row<m; row++)
int *a[COL], nrow, row; for(col =0; col<n; col++)
Printf(“Type no of rows & cols”); scanf(“%d”, (a[row]+col));
scanf(“%d%d”,&nrow, &ncol);
for(row=0; row<=nrow; row++){
}
void write_op(int *a[ROW], int m, int n){
a[row]=(int *) malloc(ncol*sizeof(int));
int row, col;
}
for(row =0; row<m; row++){
read_ip(a, nrow, ncol);
for(col =0; col<n; col++)
write_op(a, nrow, ncol);
printf(“%4d”, *(a[row]+col));
}
printf(“\n”);
}
}
Structures and pointers
typedef struct { struct {
int acct_no; int acct_no;
char acct_type; char acct_type;
char name[80]; char name[80];
float balance; float balance;
} account;
} customer, *ptr;
account customer, *ptr;
ptr = &customer
ptr = &customer
MA511: Computer Programming
Partha S Mandal, IITG
Structures and pointers
typedef struct {
int month;
int day;
int year;
} date;
typedef struct {
int acct_no;
char acct_type;
char name[80];
float balance;
date lastpayment;
} customer, *ptr = &customer;
MA511: Computer Programming
Partha S Mandal, IITG
Example: Structures and pointers
ptr->acct_no = 12341; //customer.aact_no = 12341;
ptr->acct_type = "S/B"; //customer.acct_type ="S/B";
main(){ ptr->name = "xyz"; //customer.name = "xyz";
ptr->balance = 2345.00; //...
typedef struct { ptr->lastpayment.month=02;
int month; ptr->lastpayment.day=15;
ptr->lastpayment.year=2009;
int day;
int year; printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
} date; printf("AccNo=%d, Balance=%f, Year=%d\n",
ptr->acct_no, ptr->balance, ptr->lastpayment.year );
struct {
}
int acct_no;
char *acct_type; Account= S/B Name=xyz
char *name; AccNo=12341, Balance=2345.000000
float balance;
date lastpayment;
} customer, *ptr = &customer;
MA511: Computer Programming
Partha S Mandal, IITG
Example: Structures and pointers
ptr->acct_no = 12341; //customer.aact_no = 12341;
ptr->acct_type = "S/B"; //customer.acct_type ="S/B";
main(){
ptr->name = "xyz"; //customer.name = "xyz";
ptr->balance = 2345.00; //...
typedef struct {
ptr->lastpayment.month=02;
int month;
ptr->lastpayment.day=15;
int day;
ptr->lastpayment.year=2009;
int year;
} date;
printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
printf("AccNo=%d, Balance=%f, Year=%d\n",
typedef struct {
ptr->acct_no, ptr->balance, ptr->lastpayment.year );
int acct_no;
char *acct_type;
}
char *name;
float balance;
Account= S/B Name=xyz
date lastpayment;
AccNo=12341, Balance=2345.000000
} account;
account customer, *ptr = &customer;
MA511: Computer Programming
Partha S Mandal, IITG
Passing structures to functions
main(){
typedef struct { void get_data(account *p);
int acct_no; account customer, *ptr;
ptr = &customer;
char *acct_type;
char *name; ptr->acct_no = 12341; //customer.aact_no = 12341;
ptr->acct_type = "S/B"; //customer.acct_type ="S/B";
float balance; ptr->name = "xyz"; //customer.name = "xyz";
ptr->balance = 2345.00; //...
} account;
printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
printf("AccNo=%d, Balance=%f\n", ptr->acct_no, ptr->balance);
void get_data(account *p){
get_data(ptr); //get_data(&customer);
p->acct_no = 55555;
p->acct_type = "C/A"; printf("Account= %s Name=%s\n", ptr->acct_type, ptr->name);
printf("AccNo=%d, Balance=%f\n", ptr->acct_no, ptr->balance);
p->name = "abc"; }
p->balance = 777777.00;Account= S/B Name=xyz
AccNo=12341, Balance=2345.000000
} Account= C/A Name=abc
AccNo=55555, Balance=777777.000000
MA511: Computer Programming
Partha S Mandal, IITG
Passing structures to functions
main(){
# define N 2 account customer[N], *ptr;
typedef struct { ptr = customer;
int acct_no; int i;
char *acct_type; for(i=0; i<N; i++){
ptr=get_data(ptr+i); //get_data(customer+i);
char *name;
float balance; printf("Account= %s Name=%s\n", ptr->acct_type,
} account; ptr->name);
printf("AccNo=%d, Balance=%f\n", ptr->acct_no,
account *get_data(account *p){ ptr->balance);
}
p->acct_no = 55555; }
p->acct_type = "C/A";
p->name = "abc"; Account= C/A Name=abc
p->balance = 777777.00; AccNo=55555, Balance=777777.000000
return(p) Account= C/A Name=abc
AccNo=55555, Balance=777777.000000
}
MA511: Computer Programming
Partha S Mandal, IITG