Download presentation
Presentation is loading. Please wait.
1
Day 04 Introduction to C
2
Functions – pointers as arguments
#include <stdio.h> int SumAndInc(int *pa, int *pb,int* pc); int main(int argc, char *argv[]) { int a=4, b=5, c=6; int *ptr = &b; int total = SumAndInc(&a,ptr,&c); /* call to the function */ printf(“The sum of 4 and 5 is %d and c is %p\n”,total,c); } SumAndInc(int *pa, int *pb,int *pc ){/* pointers as arguments */ *pc = *pc+1; /* return a pointee value */ /*NOT *(pc+1)*/ return (*pa+*pb); /* return by value */
3
In main() a 4 4000 b 5 4004 c 6 4008 ptr 4004 4012 pa 4000 6000 pb 4004 6004 pc 4008 6008 In function
4
In main() after the function call
4 4000 b 5 4004 c 7 4008 ptr 4004 4012 In main() after the function call
5
What’s wrong with this ? #include <stdio.h>
void DoSomething(int *ptr); int main(int argc, char *argv[]) { int *p; DoSomething(p); printf(“%d”, *p); /* will this work ? */ return 0; } void DoSomething(int *ptr){ /* passed and returned by reference */ int temp= 5+3; ptr = &(temp); /* compiles correctly, but gives incorrect output */
6
p ? 4000 In main() ptr 6000 temp 6004 In the function ? 8 6004
7
p ? 4000 In main() after the function call
8
Functions: Pass by value
int main(){ int p, q, r; p = 5; q = 6; r = Sum_1(p, q + 1); return 0; } void Sum_1(int a, int b){ int c; c = a + b; return c; } a b c 4000 4004 4008 p q r 5 6 2000 2004 2008 12 12
9
Problem 2 – pass pointer by value
int main(){ int *x,*y; int p = 5; int q = 6; x = &p; y = &q; Sum_1(p, y); return 0; } void Sum_1(int a, int *b){ int input; printf(“Enter an integer:”); scanf("%d", &input); *b = a + input; } Example15.c a b input 4000 4004 4008 x y p q 5 2000 2004 2008 2012 15 6 20
10
Functions - Passing and returning arrays
#include <stdio.h> void init_array( int array[], int size ) ; int main(int argc, char *argv[] ) { int list[5]; init_array( list, 5); for (i = 0; i < 5; i++) printf(“next:%d”, list[i]); } void init_array(int array[], int size) { /* why size ? */ /* arrays ALWAYS passed by reference */ int i; for (i = 0; i < size; i++) array[i] = 0;
11
Passing/Returning a structure
/* pass struct by value */ void DisplayYear_1( struct birthday mybday ) { printf(“I was born in %d\n”, mybday.year); } /* - inefficient: why ? */ /* pass pointer to struct */ void DisplayYear_2( struct birthday *pmybday ) { printf(“I was born in %d\n”, pmybday->year); /* Note: ‘->’, not ‘.’, after a struct pointer*/ } /* return struct by value */ struct birthday GetBday(void){ struct birthday newbday; newbday.year=1971; /* ‘.’ after a struct */ return newbday; } /* - also inefficient: why ? */
12
Example: Array and pointer
Statically allocated data int a[5]; int *p; int i = 0; for(i=0; i < 5; i++) a[i] = 3 + i; p= a; p = (int *)malloc(3 * sizeof(int)); a[0] a[1] a[2] a[3] a[4] p 2000 2004 2008 2012 2016 2020 2000 8000 p[0] p[1] p[2] 8000 8004 8008 Heap (space for dynamically allocated data)
13
Problem 3: pointer to a struct
void print_student_age_1(Student any){ printf("Age is %d\n", any.age); any.gpa = 2.0; } void print_student_age_2(Student * any){ printf("Age is %d\n", any->age); any->gpa = 2.0; void print_student_age_3(const Student * any){ int main(){ Student john; Student * john_ptr = &john; john.age = 20; john.gpa = 3.5; print_student_age_1(john); printf("gpa is %.1f\n", john.gpa); print_student_age_2(john_ptr); return 0; } any.age any.gpa 20 4000 4008 Example16.c 3.5 2.0 john.age john.gpa john_ptr 20 2000 2004 2008 3.5 2.0 any 2000 4000
14
Problem 4: malloc and structs
Student * many = (Student *)malloc(3 * sizeof(Student)); many[0].age = 20; many[0].gpa = 3.5; //void print_student_info( Student class [ ], int count ); OR //void print_student_info( Student * class, int count ); print_student_info(___________________, 3 ); Example17.c many 8000 2000 many[0].age many[0].gpa many[1].age many[1].gpa many[2].age many[2].gpa 23 3.4 22 3.6 21 4.0 8000 8004 8008 8012 8016 8020
15
Input/Output statements
fprintf(stdout,”….”,…); - buffered output Equivalent to printf(“….”,…) fscanf(stdin,…); Equivalent to scanf(…) fprintf(stderr,”…”,…); - un-buffered output Use for error messages. perror(…); Use to print messages when system calls fail.
16
Storage classes Allocate memory only when function is executed
Automatic (default for local variables) Allocate memory only when function is executed e.g. auto int i; Register Direct compiler to place variable in a register e.g. register counter = 1; Static Allocate memory as soon as program execution begins Scope is local to the function that declares the variable. Value is retained and space is de-allocated only when program (not function) quits. e.g. static int i;
17
Storage classes - contd
Extern Default for function names. For a variable shared by two or more files: int i; //global variable in file 1 extern int i; //global in files 2,3 … x For a function shared by 2 or more files, place a function prototype at the beginning of the files.
18
Program with multiple files
#include <stdio.h> #include “mypgm.h” void Myproc() { int mydata=g_data * 2; . . . /* some code */ } #include <stdio.h> #include “mypgm.h” int g_data=5; int main() { Myproc(); return 0; } mypgm.c main.c main.c Library headers Standard User-defined void Myproc(); extern int g_data; mypgm.h
19
To compile gcc main.c mypgm.c –o run or gcc main.c –c -o main.o
gcc mypgm.c –c -o mypgm.o gcc mypgm.o main.o –o run Can also use a makefile.
20
enum - enumerated data types
#include <stdio.h> enum month{ JANUARY, /* like #define JANUARY 0 */ FEBRUARY, /* like #define FEBRUARY 1 */ MARCH /* … */ }; In main: enum month birthMonth; If(birthMonth = = JANUARY){…} /* alternatively, …. */ JANUARY=1, /* like #define JANUARY 1 */ MARCH=3, /* like #define MARCH 3 */ FEBRUARY=2, /* … */ Note: if you use the #define, the value of JANUARY will not be visible in the debugger. An enumerated data type’s value will be.
21
Before you go…. Always initialize anything before using it (especially pointers) Don’t use pointers after freeing them Don’t return a function’s local variables by reference No exceptions – so check for errors everywhere An array is also a pointer, but its value is immutable.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.