Last active
May 10, 2019 15:12
-
-
Save Adobe-Android/63cc7e9a4ddec1bd646640b6efc83554 to your computer and use it in GitHub Desktop.
A gist collection of C concepts
This file contains hidden or 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
#include <stdio.h> | |
#include <inttypes.h> | |
#include <string.h> | |
// We change the value of the first character in greeting to a lowercase 't' | |
// Pointer a will change because it is pointing to the value of greeting | |
// Our greetingCpy will not change because memcpy copied the value into our new variable | |
// We can prove this by comparing the memory addresses as we have done below | |
int main(int argc, char const *argv[]) | |
{ | |
char greeting[] = "Test\n"; | |
char greetingCpy[10]; | |
memcpy(greetingCpy, greeting, sizeof(greeting)); | |
char *a = greeting; | |
greeting[0] = 't'; | |
printf("greeting memory address: 0x%" PRIXPTR "\n", (uintptr_t)greeting); | |
printf("greeting value: %s", greeting); | |
printf("a pointer memory address: 0x%" PRIXPTR "\n", (uintptr_t)a); | |
printf("a pointer value: %s", a); | |
printf("0x%" PRIXPTR "\n", (uintptr_t)greetingCpy); | |
printf("greetingCpy value: %s", greetingCpy); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
int x = 4; | |
// create pointer which references the value of x | |
int *address_of_x = &x; | |
printf("x lives at this memory location %p\n", &x); | |
printf("address_of_x points to x's memory address %p\n", address_of_x); | |
printf("pointer's memory address %p\n", &address_of_x); | |
printf("dereferenced pointer holding the value of x %p\n", *address_of_x); | |
// read and assign the value to a new variable | |
int value_stored = *address_of_x; | |
printf("x is %i\n", x); | |
// reassign the value the pointer references to 2 | |
*address_of_x = 2; | |
printf("x has been reassigned to %i\n", x); | |
// value_stored hasn't been changed as it doesn't hold the same memory position | |
printf("value_stored is still %i\n", value_stored); | |
printf("value_stored lives at this memory location %p\n", &value_stored); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
// The "JQK" string literal is a constant and is copied into the constants memory block | |
// This section of memory is read-only | |
// This pointer only refers to that read-only memory block | |
char *seg_fault = "JQK"; | |
// That's why we get a segmentation fault when we try to modify the value | |
// seg_fault[0] = seg_fault[1]; | |
// We can safely set a pointer to a string literal by labeling it as const | |
const char *cards = "JQK"; | |
// Now, this illegal operation will throw a compiler error when we try to build | |
cards[0] = cards[1]; | |
puts(cards); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
void go_south_east(int* lat, int* lon) | |
{ | |
*lat = *lat - 1; | |
*lon = *lon + 1; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int latitude = 32; | |
int longitude = -64; | |
go_south_east(&latitude, &longitude); | |
printf("Avast! Now at: [%i, %i]\n", latitude, longitude); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#include <string.h> | |
char tracks[][80] = { | |
"I left my heart in Harvard Med School", | |
"Newark, Newark - a wonderful town", | |
"Dancing with a Dork", | |
"From here to maternity", | |
"The girl from Iwo Jima", | |
}; | |
void find_track(char search_for[]); | |
int main(int argc, char const *argv[]) | |
{ | |
char search_for[80]; | |
printf("Search for: "); | |
fgets(search_for, sizeof(search_for), stdin); | |
search_for[strlen(search_for) - 1] = '\0'; | |
find_track(search_for); | |
return 0; | |
} | |
void find_track(char search_for[]) | |
{ | |
for(int i = 0; i < 5; i++) | |
{ | |
if (strstr(tracks[i], search_for)) { | |
printf("Track %i: '%s'\n", i, tracks[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment