Aims and Objectives
Pointers and Addresses
The Organisation of Memory
Pointers and Arrays
2-dimensional character Arrays
Managing Memory
Arrays and Memory Manipulation
if (!valid(user_action, context)) print_error_message(user_action, context, error_code); /* */
int *ip; /* declares ip to be a pointer to an integer */
if (! valid(a, b, &error_code)) { print_error_message(a, b, error_code); } int test_range(int a, int b, int *error_code) { int in_error = 1; if (b == 1 && a > 2) { in_error = 0; if (a == 3) { *error_code = 1; } else if (a == 4) { *error_code = 2; } ... } return in_error; }
main() { int a = 4, b = 7; int *ip; /* ip just declared as a pointer */ ip = &a; /* ip now points to a */ *ip = 10; /* a now = 10 */ b = *ip; /* b also equal to 10 */ /* .... */ }
*ip = value | /* update address */ |
ip = &variable | /* point to variable */ |
ip = 0 | /* for conditionals */ |
++ip | /* point to next logical cell */ |
--ip | /* point to previous logical cell */ |
++*ip | /* increment value at ip */ |
--*ip | /* decrement value at ip */ |
ip + integer | /* point to cell */ |
ip - integer | /* point to cell */ |
*ip op integer | /* update variable at ip */ |
void strcopy(char b[], char a[]); main() { char another_str[] ="Hello, world", string[20]; strcopy(string, another_str); printf("%s\n", string); } void strcopy(char b[], char a[]) { int i; for (i = 0; a[i] != '\0'; i++) b[i] = a[i]; b[i] = '\0'; }
void strcopy(char *b, char *a); main() { char another_str[] ="Hello, world", string[20]; strcopy(string, another_str); /* No & needed */ printf("%s\n", string); } void strcopy(char *b, char *a) { while (*b++ = *a++) ; }
char months[13][10] = /* 13 months, max len 10 */
{"","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char months[] =
" JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember";
char *alloc(int n) /* return pointer to n chars */ void mfree(char *p) /* free storage from p */
#define SIZE 10000 /* size of stack */ static char allocbuf[SIZE]; /* stack */ static char *allocp = allocbuf; /* free pos */ char *alloc(int n) { if (allocbuf + SIZE - allocp >= n) { /* there is enough available space */ allocp += n; /* next free space */ return allocp - n; /* space allocated */ } else return 0; /* request denied */ } void mfree(char *p) { if (p >= allocbuf && p < allocbuf + SIZE) /* *p is pointing to element inside stack */ allocp = p; }
#include <stdio.h> #include <string.h> #define MAXLEN 80 /* max length of input line */ int getline(char *, int); char *alloc(int); /* read input lines */ int readlines(char *lineptr[], int maxlines) { int len, nlines; char *p, line[MAXLEN]; nlines = 0; while ((len=getline(line, MAXLEN)) > 0) if (nlines >= maxlines || (p = alloc(len)) == NULL) return -1; /* no space */ else { line[len - 1] = '\0'; /* delete \n */ strcpy(p, line); lineptr[nlines++] = p; } } return nlines; }
readlines(char *lineptr[], int maxlines)
array[0] -------------> "January" array[1] -------------> "February" array[2] -------------> "March"
void swap(char a[], char b[]) { int i; char temp[MAX]; /* if the elements need swapping then */ for (i = 0; b[i] != '\0'; i++) temp[i] = b[i]; temp[i] = '\0'; for (i = 0; a[i] != '\0'; i++) b[i] = a[i]; b[i] = '\0'; for (i = 0; temp[i] != '\0'; i++) a[i] = temp[i]; a[i] = '\0'; }
void swap(char *a[], int i, int j) { char *temp; temp = a[i]; a[i] = a[j]; a[j] = temp; }