CS 414/415 Section C For Java Programmers: Indranil Gupta
CS 414/415 Section C For Java Programmers: Indranil Gupta
Indranil Gupta
void delete (struct list *head, struct list *tail){ struct list *temp; if(head==tail){ free(head); head=tail=NULL; } else{ temp=head->next; free(head); head=temp; } }
Simple Example
#include <stdio.h> void main(void) { printf(Hello World. \n \t and you ! \n ); /* print out a message */ return; }
void main(void){ } is the only code executed printf( /* message you want printed */ ); \n = newline \t = tab Dessert: \ in front of other special characters within
printf.
Example !
#include <stdio.h> void main(void) { int nstudents = 0; /* Initialization, required */
printf(How many students does Cornell have ?:); scanf (%d, &nstudents); /* Read input */ printf(Cornell has %d students.\n, nstudents);
return ;
}
$How many students does Cornell have ?: 20000 (enter) Cornell has 20000 students. $
Type conversion
#include <stdio.h> void main(void) { int i,j = 12; /* i not initialized, only j */ float f1,f2 = 1.2; i = (int) f2; f1 = i; /* explicit: i <- 1, 0.2 lost */ /* implicit: f1 <- 1.0 */
Example
#include <stdio.h> #define DANGERLEVEL 5 /* C Preprocessor - substitution on appearance */ /* like Java final */ void main(void) { float level=1; /* if-then-else as in Java */ if (level <= DANGERLEVEL){ /*replaced by 5*/ printf(Low on gas!\n); } else printf(Good driver !\n); return; }
One-Dimensional Arrays
#include <stdio.h> void main(void) { int number[12]; /* 12 cells, one cell per student */ int index, sum = 0; /* Always initialize array before use */ for (index = 0; index < 12; index++) { number[index] = index; } /* now, number[index]=index; will cause error:why ?*/
for (index = 0; index < 12; index = index + 1) { sum += number[index]; /* sum array elements */ }
return;
More arrays
Strings
char name[6]; name = {C,S,4,1,4,\0}; /* \0= end of string */ printf(%s, name); /* print until \0 */
Multi-dimensional arrays
So be careful !
Arrays
Always initialize before use
int number[12]; printf(%d, number[20]);
10
12.5
9. 8
4300
4304
4308
4312
4316 4317
f_addr
? 4300
? 4304
? 4300
4300 4304
3.2 4300
4300 4304
1.3 4300
4300 4304
Pointer Example
#include <stdio.h>
*ptr=4; j=*ptr; }
Error Handling
Moral from example:
unlike Java, no explicit exceptions need to manually check for errors
Whenever using a function youve not written Anywhere else errors might occur
Arguments by reference
#include <stdio.h> int sum(int *pa, int *pb); /* function prototype at start of file */ void main(void){ int a=4, b=5; int *ptr = &b; int total = sum(&a,ptr); /* call to the function */ printf(The sum of 4 and 5 is %d, total); } int sum(int *pa, int *pb){ /* the function itself - arguments passed by reference */ return (*pa+*pb); /* return by value */ }
void dosomething(int *ptr){ /* passed and returned by reference */ int temp=32+12; ptr = &(temp); } /* compiles correctly, but gives run-time error */
Header info
100 Code 400
all malloc()s
560
Data - Heap
Dynamic memory
Data - stack
hw.c
mypgm.c
void myproc(void); int mydata;
Library headers
Standard User-defined
mypgm.h
Externs
#include <stdio.h> extern char user2line [20]; char user1line[30]; void dummy(void); void main(void) { char user1line[20]; . . . } /* global variable defined in another file */ /* global for this file */
Structures
Equivalent of Javas classes with only data (no methods)
#include <stdio.h> struct birthday{ int month; int day; int year; }; main() { struct birthday mybday; /* - no new needed ! */ /* then, its just like Java ! */ mybday.day=1; mybday.month=1; mybday.year=1977; printf(I was born on %d/%d/%d, birth.day, birth.month, birth.year); }
More on Structures
struct person{ char name[41]; int age; float height; struct { int month; int day; int year; } birth; }; struct person me; me.birth.year=1977; struct person class[60]; /* array of info about everyone in class */ class[0].name=Gun; class[0].birth.year=1971;
/* embedded structure */
Passing/Returning a structure
/* pass struct by value */ void display_year_1(struct birthday mybday) { printf(I was born in %d\n, mybday.year); } /* - inefficient: why ? */ . . . . /* pass struct by reference */ void display_year_2(struct birthday *pmybday) { printf(I was born in %d\n, pmybday->year); /* warning ! ->, not ., after a struct pointer*/ } . . . . /* return struct by value */ struct birthday get_bday(void){ struct birthday newbday; newbday.year=1971; /* . after a struct */ return newbday; } /* - also inefficient: why ? */
/* JANUARY is the same as month.JANUARY */ /* alternatively, . */ enum month{ JANUARY=1, FEBRUARY, MARCH };
typedef struct person Person; Person me; /* same as struct person me; */
typedef struct person *Personptr; Personptr ptrtome; /* same as struct person *ptrtome;*/
More pointers
int month[12]; /* month is a pointer to base address 430*/ month[3] = 7; /* month address + 3 * int elements => int at address (430+3*4) is now 7 */
ptr = month + 2; /* ptr points to month[2], => ptr is now (430+2 * int elements)= 438 */ ptr[5] = 12; /* ptr address + 5 int elements => int at address (434+5*4) is now 12. Thus, month[7] is now 12 */ ptr++; /* ptr <- 438 + 1 * size of int = 442 */ (ptr + 4)[2] = 12; /* accessing ptr[6] i.e., array[9] */
Now , month[6], *(month+6), (month+4)[2], ptr[3], *(ptr+3) are all the same integer variable.
2-D arrays
2-dimensional array
int weekends[52][2];
[0][0]
[0][1]
[1][0]
[1][1]
[2][0]
[2][1]
[3][0]
. . .
weekends
Strings
#include <stdio.h> main() char char char { msg[10]; /* array of 10 chars */ *p; /* pointer to a char */ msg2[]=Hello; /* msg2 = Hello\0 */
msg = Bonjour; /* ERROR. msg has a const address.*/ p = Bonjour; /* address of Bonjour goes into p */ msg = p; /* ERROR. Message has a constant address. */ /* cannot change it. */ p = msg; /* OK */ p[0] = H, p[1] = i,p[2]=\0; /* *p and msg are now Hi */
Pointer to function
func(); /*function returning integer*/ *func(); /*function returning pointer to integer*/ (*func)(); /*pointer to function returning integer*/ *(*func)(); /*pointer to func returning ptr to int*/
/* do something with d */
void delete (struct list *head, struct list *tail){ struct list *temp; if(head==tail){ free(head); head=tail=NULL; } else{ temp=head->next; free(head); head=temp; } }