Feb 7
style notes from Alex He.
2. declaration styles - to point or not to point
typedef struct _node{
struct _node *node1;
struct _node *node2;
} *node;
int main(){
node n;
}
vs
typedef struct _node node;
struct _node {
node *node_ptr1;
node *node_ptr2;
};
int main(){
node *node_ptr;
}
C tutorial, Fridays
Talk about Cory's intention to do a 2-credit C tutorial
instead of this class. Could be an option for others
if this material is too much right now.
homework
What do you think about this code?
int main(){
char* input;
printf("Please enter a word: ");
scanf("%s", &input);
printf("You said '%s'.\n", &input);
return 0;
}
We wrote this in class,
as an example of dynamic list
of dynamic struct :
#include <stdio.h>
#include <stdlib.h>
typedef struct _point {
int x;
int y;
} point;
int main(){
// allocate a pointer to a list of pointers to points.
point** ptrs; // pointer to list of point
// OR allocate all the space at compile time
// point data[128];
int count;
int x,y;
int i;
printf("How many points? ");
scanf("%d", &count);
ptrs = malloc(sizeof(point*) * count);
for (i=0; i<count; i++){
printf("%d : x y = ", i);
scanf("%d %d", &x, &y);
ptrs[i] = malloc(sizeof(point));
ptrs[i]->x = x;
ptrs[i]->y = y;
}
return 0;
}
- What sorts of debugging print statements can we put in to look at what's going on?
- What are some of the various other ways to do this? Where can the memory be put for the string?
---
discuss big vs little endian, and what C looks like to detect it
What does this do, and how does it work?
(show_bytes is mostly from the textbook's code examples.)
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len) {
int i;
printf(" at %p : 0x ", start);
for (i = 0; i < len; i++) printf(" %.2x", start[i]);
printf("\n");
}
int main(){
int x = 1234;
int y = -10;
char* buffer;
char hello[] = "hello";
show_bytes(&x, 16);
show_bytes("12345", 16);
show_bytes(&y, 16);
show_bytes(buffer, 16);
show_bytes(&buffer, 16);
show_bytes(hello, 16);
show_bytes(&hello, 16);
return 0;
}
work through the other assigned C exercises :
int any_odd_one(unsigned x) {
return 2; /* what goes here? */
}
int threefourths(int x){
return 2; /* what goes here? */
}
signed integers
What are the bytes in "(int) -1001"?
(a) by hand
(b) C code to display 'em.
Some obviously always true integer facts ... or are they?
#include <stdio.h>
int main(){
int i;
printf("Please enter i : ");
scanf("%d", &i);
printf(" i = %d \n", i);
printf(" -i = %d \n", -i);
printf(" -i < i ? %s \n", -i < i ? "yes" : "no");
printf(" -i + i = %d \n", -i + i);
return 0;
}
pg 52: practice problem 2.11
pg 57: typical ranges of C integral data
bits=? bytes=? min=? max=?
char
unsigned char
short
unsigned short
int
unsigned int
long
unsigned long
long long
unsigned long long
(the word "int" is optional after long)
/* WARNING: THIS IS BUGGY CODE (pg 77 of text) */
float sum_elements(float a[], unsigned length){
int i;
float result = 0;
for (i=0; i <= length-1; i++) result += a[i];
return result;
}
... explain what the bug is.
Then check out practice 2.26, pg 77.
datalab
Explore the datalab code
and btest infrastructure, and try some of 'em in class.
homework for next week
Which of these would be the most helpful?
1. continue on to finish chapter 2
- read pg 99 and following
- floating point material: pg 101 & following
2. more practice with integers and bit operations
- re-read chap 2 given this week's discussion, up to pg 99
- datalab
3. more C practice with pointers: implement & test a binary tree in C
- re-read K&R or C book in Sci217
- also in algorithms class assignment
- either (a) integer data or (b) string data (more work)
- recursive algorithm: go left or right if smaller or bigger than current data
# API
node *new_node(int data); // or (char* data_str)
void insert(node *root, int data);
int contains(node *root, int data); true if the binary tree has that data
after these calls
node *root = new_node(10);
insert(root, 20);
insert(root, 4);
insert(root, 6);
printf("%s", contains(root, 11) ? 'yes' : 'no'); \\ prints 'no'
printf("%s", contains(root, 6) ? 'yes' : 'no'); \\ prints 'yes'
and the tree would be in a configuration something like this.
10
/ \
4 20
/ \ / \
6