



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Lecture 9: Integer Representation................................................. 1 Size of types....................................................................................... 1 Lecture 10 –................................................................................. 2 File I/O - #include <stdio.h>................................................................ 2 Strings................................................................................................ 2 Lecture 11................................................................................... 2 Pointers & Arrays................................................................................ 2 Pointer Arithmetic............................................................................... 3 Lecture 12: Dynamic memory allocation......................................... 3 Lecture 13: Pointers and Multi-Dimensional Arrays......................... 3 Lecture 14: Structs....................................................................... 5 Alignment – struct fields.................................................................................................. 5
Integers Type Bit s Byte s Signed Unsigned char 8 1 -128 to 127 0 to 255 short 16 2 -32,768 to 32,767 0 to 65, int 32 4 -2,147,483,648 to 2,147,483, 0 to 4,294,967, long 64 8 - 9,233,372,036,854,775, 808 to 9,233,372,036,854,775, 807 0 to 18,446,744,073,709, , long long 64 8 - 9,233,372,036,854,775, 808 to 9,233,372,036,854,775, 807 0 to 18,446,744,073,709, , Real numbers Type Bit Byte Minimum Maximum
s s float 32 4 23 bits (~7 digits) about 10-38^ … 10^38 double 64 8 52 bits (~15 digits) About 10-308^ … 10^308 long double
16 64 bits (~19 digits) About 10-4930^ … 10^4930 pointer 8
fopen: FILE *stream = fopen(“file-name.txt”, “r”); o “r” or “w” o FILE *stream is a stream and pointer to the file, NULL fprintf: int fprintf(FILE *stream, const char format[], …); o stream = stream to write to fscanf: int fscanf(FILE *stream, const char format[], …); fclose: int fclose(FILE *stream); hexdump: utility that shows exact sequence of bytes in a file $ hexdump -C someFile.txt -C: show numeric codes and characters Character I/O (stdio) Terminal: o int getchar(void): reads a single character o int putchar(int ch): writes a single character Stream: o int fgetc(FILE *stream): reads a single char from the given stream o int fputc(int ch, FILE *stream): writes a single char to the given stream int ungetc(int c, FILE *stream); pushes character c back to the given stream and clears EOF condition if EOF has hit int rewind(FILE *stream); Go back to the start of a file to re-read or re- write it int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream);
An array, a, evaluates to a pointer to the 1st^ element. The pointer is the same type stored in the array
element palette[0]+1 Points to element in row 0,col 1 palette + 1 Points to 2nd^ row of palette void f( short palette[][3]) Pass array to a function short(cptr)[3] Pointer to an array of 3 shorts short cptr[3] short (cptr[3]) Array of 3 pointers to shorts f(short p[][3]) f(short (p)[3]) Pass Array of? rows and 3 columns Dynamically allocating o 2D arrays short (pal)[3]; int N = 1000; pal = (short ()[3])malloc(N3sizeof(short)); // malloc returns void pointer so doesn’t need to be cast pal[5][2] = 850; o Multi-dimensional arrays int R = 500; int C = 400; int (a)[C]; a = (int ()[C])malloc(RCsizeof(int)); for (int i=0; i<R; i++) for (int j=0; j<C; j++) a[i][j]=i*j; free(a) – Free these arrays from memory Pointer to an array of pointers ( like in Java) int **b; b = (int **)malloc(R * sizeof(int *)); for (int i=0; i<R; i++) b[i] = (int )malloc(Csizeof(int)); //free for (int i=0; i<R; i++) free(b[i]); free(b); String char words[][14] //? strings with 14 chars char *words[] // pointers to (unmodifiable)strings with their own length Pointers to functions o Function: int f(char str) o Pointer to function: int (fp)(char *str); fp = myFunction;
int x = fp(“hello world”); // call function via pointer o How to use: int applyFunction(char strings, int stringCount, int (f)(char)) { int total=0; … } applyFunction(argv, argc, stringLength); applyFunction(argv, argc, countVowels);
struct Person { char name[10]; double height; int age; }; struct Person p1; // make an instance p1.height = 1.75; // set fields p1.age = 24; strcpy(p1.name, “Angela”); struct Person p2 = {“William”, 1.85, 27}; struct Person p3 = {.age=33, .name = “Agatha”; .height = 1.7}; struct Person p4 = {.height = 1.234}; // other fields are 0 Declare and define struct at the same time struct Grade{ double minimum; char letter; } aGrade = {90.0, ‘A’}; Anonymous Struct: struct { double latitude; double longitude; } Raleigh = {35.7806, 78.6389};
Natural alignment: Systems store any primitive type of size n at addresses that’s a multipe of the size of the thing 2-byte alignment: Anything larger than a byte may be sxotred starting at an even memory address 4-byte alignment: Anything larger than 2 bytes is stored starting at an address that’s a multiple of 4