Skip to content

Instantly share code, notes, and snippets.

@markd2
Created April 26, 2012 03:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markd2/2495629 to your computer and use it in GitHub Desktop.
Save markd2/2495629 to your computer and use it in GitHub Desktop.
Scan/Stop examples
#import "stdio.h"
// clang -g -Wall -o scanstop scanstop.m
static short intArray[] = { 1, 1, 2, 3, 5, 7, 12, 19, 31 };
static char *ravenArray[] = { "once", "upon", "a", "midnight", "dreary" };
typedef struct Groovy {
int value;
const char *string;
float shoeSize;
} Groovy;
static Groovy groovyArray[] = {
{ 1, "once", 10.5 },
{ 1, "upon", 8.0 },
{ 2, "a", 11.0 },
{ 3, "midnight", 9.0 },
{ 5, "dreary", 12.5 },
{ 7, "while", 13.0 },
{ 12, "I", 8.5 },
{ 19, "pondered", 9.5}
};
static void printIntegers () {
printf ("integers:\n");
short *scan = intArray;
short *stop = scan + sizeof(intArray) / sizeof(*intArray); // 18 / 2
while (scan < stop) {
printf (" %d\n", *scan);
scan++;
}
} // printIntegers
static void printStrings () {
printf ("strings:\n");
char **scan = ravenArray;
char **stop = scan + sizeof(ravenArray) / sizeof(*ravenArray);
while (scan < stop) {
printf (" %s\n", *scan);
scan++;
}
} // printStrings
static void printGroovy () {
printf ("groovy:\n");
Groovy *scan = groovyArray;
Groovy *stop = scan + sizeof(groovyArray) / sizeof(*groovyArray);
while (scan < stop) {
printf (" %d: %s -> %.1f\n", scan->value, scan->string, scan->shoeSize);
scan++;
}
} // printGroovy
int main (void) {
printIntegers ();
printStrings ();
printGroovy ();
return 0;
} // main
#if 0
When run, this outputs:
% ./scanstop
integers:
1
1
2
3
5
7
12
19
31
strings:
once
upon
a
midnight
dreary
groovy:
1: once -> 10.5
1: upon -> 8.0
2: a -> 11.0
3: midnight -> 9.0
5: dreary -> 12.5
7: while -> 13.0
12: I -> 8.5
19: pondered -> 9.5
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment