Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State of examples after class. TODOs are complete
- Loading branch information
Showing
20 changed files
with
184 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
/** Arrays/points in the command line */ | ||
/** Arrays/pointers in the command line */ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(int argc, char *argv[]) { | ||
// RTFM man 7 environ | ||
extern char **environ; | ||
|
||
//ANSWER: This way (3rd param to main) is deprecated | ||
// https://www.securecoding.cert.org/confluence/display/seccode/ENV31-C.+Do+not+rely+on+an+environment+pointer+following+an+operation+that+may+invalidate+it | ||
int main(int argc, char *argv[], char *env[]) { | ||
// argc -- argument cont | ||
// argv -- argument vector | ||
// Let's talk about argv!! | ||
|
||
int i; | ||
for (i=0; i < argc; i++) { | ||
printf("argv[%d]='%s'\n", i, argv[i]); | ||
} | ||
|
||
//QUESTION: show how to properly access environment variables | ||
//ANSWER: This way is deprecated | ||
// https://www.securecoding.cert.org/confluence/display/seccode/ENV31-C.+Do+not+rely+on+an+environment+pointer+following+an+operation+that+may+invalidate+it | ||
for (i=0; NULL==env[i]; i++) { | ||
printf("env[%d]='%s'\n", i, env[i]); | ||
} | ||
|
||
// RTFM getenv | ||
char *path=getenv("PATH"); | ||
printf("PATH is '%s'\n", path); | ||
// RTFM man 7 environ | ||
char **ep=environ; | ||
while (NULL!=*ep) { | ||
char *env_assignment=*ep; | ||
ep++; | ||
puts(env_assignment); | ||
} | ||
|
||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
/** Arrays of pointers */ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define DAYS_IN_WEEK 7 | ||
|
||
void printAray(char *days[]); | ||
|
||
//ANSWERED: Why did this qsort example fail!? Other than not practicing! | ||
//qsort passes pointers to each element being sorted. | ||
//so the parameters here are really pointers to pointers! | ||
int ourstrcmp(const void *a_pointer_to_string, const void *b) { | ||
static int number_of_debug_outputs=3; | ||
// Step 1: Call it what it is -- a pointer to a pointer | ||
char **pointer2pointer1=(char **)a_pointer_to_string; | ||
// Step 2: Dereference it to get what we want -- a "char *" | ||
char *string1 = *pointer2pointer1; | ||
char **pointer2pointer2=(char **)b; | ||
char *string2 = *pointer2pointer2; | ||
if (0!=number_of_debug_outputs) { | ||
number_of_debug_outputs--; | ||
printf("string1=%s\n",string1); | ||
printf("string2=%s\n",string2); | ||
} | ||
int result = strcmp(string1,string2); | ||
return result; | ||
} | ||
|
||
|
||
int main(int argc, char *argv[]) { | ||
char *days[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; | ||
char *days[] = { "Sunday", "Monday", "Tuesday", "Wed", "Beer day", "Fri", | ||
"Saturday" }; | ||
printAray(days); | ||
//qsort(days,DAYS_IN_WEEK,sizeof(char *), strcmp);//attempt to "cheat" | ||
qsort(days,DAYS_IN_WEEK,sizeof(char *), ourstrcmp);//attempt to "cheat" | ||
printAray(days); | ||
return 0; | ||
} | ||
|
||
void printAray(char *days[]) { | ||
printf("This is an array of seven pointers to strings.\n"); | ||
int i; | ||
for (i=0; i < 7; ++i) { | ||
for (i=0; i < DAYS_IN_WEEK; ++i) { | ||
printf("\t%s\n", days[i]); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
22May2012Talk | ||
============= | ||
|
||
Files related to my 5/22/12 guest appearance in Dr. Hahn's CSCI 1840 | ||
Files related to my 5/22/12 guest appearance in Dr. Hahn's CSCI 1840 | ||
|
||
Code will be kept up to date at https://github.com/payne/22May2012Talk |
Oops, something went wrong.