Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
#include <stdlib.h> #include <stdio.h> /* getline() reads one line from standard input and copies it to line array * (but no more than max chars). * It does not place the terminating \n in line array. * Returns line length, or 0 for empty line, or EOF for end-of-file. */ int getline(char line[], int max) { int nch = 0, c; max = max - 1; /* leave room for '\0' */ while ((c = getchar()) != EOF) { if (c == '\n') break; if (nch < max) { line[nch] = c; nch = nch + 1; } } if (c == EOF && nch == 0) return EOF; line[nch] = '\0'; return nch; } int main() { char * line; int linelen; scanf("%d\n", &linelen); if (linelen <= 0) { printf("ERROR: line length must be positive!\n"); return 1; } line = (char *) malloc(linelen); if (line == NULL) { printf("ERROR: out of memory!\n"); return 1; } getline(line, linelen); printf("%s\n", line); return 0; } |
*p = 0; |
char *somestring, *copy; ... copy = (char *) malloc(strlen(somestring) + 1); /* +1 for \0 */ /* check malloc's return value */ strcpy(copy, somestring); |
int *ip = (int *) malloc(100 * sizeof(int)); |
int *ip = (int *) malloc(100 * sizeof(int)); |
int *ip = (int *) malloc(100 * sizeof(int)); if (ip == NULL) { printf("ERROR: out of memory\n"); return -1; } |
free(p); |
free(p); |
free(p); p = NULL; |
ip = realloc(ip, 200 * sizeof(int)); |
/* reads lines of text from the standard input. Treats each line as an integer * by calling atoi, and stores each integer in a * dynamically-allocated "array" */ #include <stdlib.h> #include <stdio.h> #define MAXLINE 100 #define INITSIZE 2 int main() { char line[MAXLINE]; int *ip; int nalloc, nitems; nalloc = INITSIZE; ip = (int *) malloc(nalloc * sizeof(int)); /* initial allocation */ if (ip == NULL) { printf("out of memory\n"); return -1; } nitems = 0; while (getline(line, MAXLINE) != EOF) { if (nitems >= nalloc) { /* increase allocation */ int *newp; nalloc *= 2; newp = realloc(ip, nalloc * sizeof(int)); if (newp == NULL) { printf("out of memory\n"); return -1; } ip = newp; } ip[nitems++] = atoi(line); } printf("Allocated %d items, initialize %d items\n", nalloc, nitems); return 0; } |
#include <stdio.h> typedef struct item { int data; struct item * next; } Item; int main () { char carray[16]; char * cp = (char *) malloc(16 * sizeof(char)); printf("char\t\t%d bytes\n", sizeof(char)); printf("unsigned char\t%d bytes\n", sizeof(unsigned char)); printf("short\t\t%d bytes\n", sizeof(short)); printf("unsigned short\t%d bytes\n", sizeof(unsigned short)); printf("int\t\t%d bytes\n", sizeof(int)); printf("unsigned int\t%d bytes\n", sizeof(unsigned int)); printf("long\t\t%d bytes\n", sizeof(long)); printf("unsigned long\t%d bytes\n", sizeof(unsigned long)); printf("float\t\t%d bytes\n", sizeof(float)); printf("double\t\t%d bytes\n", sizeof(double)); printf("carray\t\t%d bytes\n", sizeof(carray)); printf("cp\t\t%d bytes\n", sizeof(cp)); printf("Item\t\t%d bytes\n", sizeof(Item)); return 0; } |
#include <stdlib.h> #include <stdio.h> typedef struct item { int data; struct item * next; } Item; Item * push_back(Item * head, int data) { Item * tail = head; Item * new = (Item *) malloc(sizeof(Item)); /* construct new item */ if (!new) { printf("ERROR: out of memory!\n"); return NULL; } new->data = data; new->next = NULL; if (tail == NULL) /* empty list */ return new; while (tail->next != NULL) /* search last item */ tail = tail->next; tail->next = new; /* insert new item as last */ return head; } void print(Item * head) { if (!head) return; printf("%d", head->data); for (head = head->next; head; head = head->next) printf(" %d", head->data); printf("\n"); } int main() { Item * head = NULL; if (!(head = push_back(head, 1))) return -1; if (!(head = push_back(head, 2))) return -1; if (!(head = push_back(head, 3))) return -1; if (!(head = push_back(head, 4))) return -1; if (!(head = push_back(head, 5))) return -1; print(head); return 0; } |
Item * reverse1(Item * head) { Item * item = head, * next; if (!head || !head->next) return head; next = head->next; head->next = NULL; while (next) { Item * tmp = next->next; next->next = item; item = next; next = tmp; } return item; } |
Item * reverse2(Item * head) { Item * item, * next; if (!head || !head->next) return head; next = head->next; item = reverse2(head->next); next->next = head; head->next = NULL; return item; } |
typedef struct item { int data; struct item * next; } Item; typedef struct list { int size; Item * head; Item * tail; } List; |
typedef struct item { int data; struct item * prev; struct item * next; } Item; typedef struct list { int size; Item * head; Item * tail; } List; |