Download presentation
Presentation is loading. Please wait.
Published byJuliet Tate Modified over 8 years ago
1
Copyright © 2016 Robert W. Hasker
3
exponent + 31 +/-mantissa 1 bit 6 bits9 bits
6
#include main() { long long P = 1, E = 2, T = 5, A = 61, L = 251, N = 3659, R = 271173410, G = 1479296389, x[] = { G * R * E * E * T, P * L * A * N * E * T }; puts((char*)x); } $ gcc hello.c $ a.out Hello World!
8
// messed_up.c #include main() { int a = f() + f(8, 9, 10); printf("a = %d\n", a); } f(int b) { return b; } messed_up.c:3:1: warning: type specifier missing, defaults to 'int’ main() ^ messed_up.c:5:12: warning: implicit declaration of function 'f' is invalid in C99 int a = f() + f(8, 9, 10); ^ messed_up.c:8:1: warning: type specifier missing, defaults to 'int’ f(int b) ^
13
Another C++ oddity: Friend declaration Access to private, protected sections Use sparingly!! class Rectangle { int width, height; public: int area () { return (width * height); } void convert (Square a); }; class Square { friend class Rectangle; private: int side; public: Square (int a) : side(a) {} }; void Rectangle::convert (Square a) { width = a.side; height = a.side; }
16
import java.io.ByteArrayOutputStream; public class MysteryCode { public static void main(String[] unused) throws Exception { ByteArrayOutputStream stoned = new ByteArrayOutputStream(20480); int[] magic = {104, 116, 116, 112, 58, 47, 47, 98, 105, 116, 46, 108, 121, 47, 49, 98, 87, 119, 51, 75, 111}; for (int weird : magic) stoned.write(weird); int crazy, unknown = 0; java.io.InputStream wtf = new java.net.URL(stoned.toString()).openStream(); while((crazy = wtf.read()) != -1) stoned.write(crazy); for (int strange : stoned.toByteArray()) { if (unknown == 2) { if (strange == 38) break; System.out.print((char) strange); } else if (17 + (unknown + 1) * 21 == strange) { unknown++; }
24
Function pointers are “typed” to the prototype of a function. Some functions: int compareLex(char* a, char* b); int compareNum(char* a, char* b); Selecting a function at runtime: 1.Create function pointer to a function that returns and int, and accepts two char* parameters int (*comp)(char*, char*); 2.Assigned chosen function to the pointer comp = compareLex; /* name of the function */ 3.Call function via function pointer int answer = (*comp)(mystring1, mystring2); Note parentheses.
25
/* Function Pointers */ #include /* returns < 0 if "a" < "b" */ int compareLex(char* a, char* b) { return strcmp(a, b); } /* returns < 0 if a < b */ int compareNum(char* a, char* b) { // convert to numbers first int va = atoi(a); int vb = atoi(b); return va - vb; } void swap(char **a, char **b) { char* temp = *a; *a = *b; *b = temp; } char *num0 = "100"; char *num1 = "98"; char *num2 = "95"; /* function pointer */ int (*comp)(char*, char*); /* set to numeric order */ comp = compareLex; /* hardcode 3-way sort with minimum comparisons */ if( (*comp)(num0, num1) > 0 ) /* num0 is > num1 - swap */ { swap(&num0, &num1); } if( (*comp)(num1, num2) > 0 ) /* num1 is > num2 - swap */ { swap(&num1, &num2); } if( (*comp)(num0,num1) > 0 ) /* num0 is > num1 - swap */ { swap(&num0,&num1); }
26
Another use: OO Crude form of polymorphism typedef struct shape { int x, y, width, height; void (*draw)(struct shape *); } shape; void drawRect(shape* this) { printf("drawing a %d x %d rectangle at %d, %d\n", this->width, this->height, this->x, this->y); } void drawCircle(shape* this) { printf("drawing a %d radius circle at %d, %d\n", this->width, this->x, this->y); } shape* newRectangle(int x, int y, int width, int height) { shape r = malloc(sizeof(shape)); r->x = x; r->y = y; r->width = width; r->height = height; r->draw = drawRect; return r; } shape *s = newRectangle(5, 10, 15, 20); s->draw(s);
27
Callbacks Here: pass function to system; will call this function when event trigered Generally: client function called by library /* Function Pointers - signal handler */ #include void mySigProcess(int arg) { printf("\n\nYou have pressed ctrl-c - exiting\n"); exit(0); } int main() { int i; // register our own signal handler for SIGINT (Ctrl-C) signal(SIGINT, mySigProcess); while ( 1 ) { printf("%d - ", ++i); } // no return – never reach here! }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.