Skip to content

Instantly share code, notes, and snippets.

@Trinitek
Created January 1, 2015 06:30
Show Gist options
  • Save Trinitek/ba0690a0e400cbe81560 to your computer and use it in GitHub Desktop.
Save Trinitek/ba0690a0e400cbe81560 to your computer and use it in GitHub Desktop.
CRASH COURSE: C POINTERS
// Declaring a new string
char string1[] = "This is a string";
// Declaring an uninitialized array with specified size
char string2[8];
// Declaring an uninitialized array with undefined size
// (SmallerC does not implement this!!)
char string3[];
/*
Reading from an array using pointers...
*/
// Let's call a function to get the third character in string1
// the & before string1 means "get the location of string1 in memory, AKA the pointer to string1"
char c = readFromArray(&string1, 3);
// =====================
char readFromArray(char* strArray, unsigned char index) {
// 'char* strArray' means that strArray is a pointer to a data structure made up of chars
char c;
// the * means to "dereference" the pointer, or to get the actual value in memory that the pointer...uh...points to
// here, we're taking the memory address of wherever our string array is, and adding the index to it, so, in effect...
// ...it's the same as writing string1[index] !!
c = *(strArray + index);
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment