Skip to content

Instantly share code, notes, and snippets.

@alberthdev
Created March 8, 2016 21:43
Show Gist options
  • Select an option

  • Save alberthdev/f52d08f2e609d5808407 to your computer and use it in GitHub Desktop.

Select an option

Save alberthdev/f52d08f2e609d5808407 to your computer and use it in GitHub Desktop.
Mick's ENEE150 Stuff
/*
* ASCII Table: http://www.asciitable.com/
*
*/
#include <stdio.h>
int main() {
int x = 5;
int *y = &x;
printf("Location of x: %x\n", y);
printf("Value stored at x: %d\n", *y);
*y += 1;
printf("New value stored at x: %d\n", *y);
return 0;
}
#include <stdio.h>
int main() {
char a[] = "Abcdefgh";
int i = 0;
for (i=0; i <= strlen(a); i++) {
// printf("%c", a[i]);
printf("%c", *(a+i));
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *abc = NULL;
int len = 100;
abc = malloc(sizeof(char) * len);
// Check if memory is allocated...
// If malloc returned NULL, this condition will become true.
if (!abc) {
printf("ERROR: Could not allocate memory!\n");
return 1;
}
strcpy(abc, "Hello, world!");
printf("%s\n", abc);
}
/* Good use case for realloc
* http://www.cplusplus.com/reference/cstdlib/realloc/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int *numabc = NULL;
int *numabc_tmp = NULL;
int len = 100;
int len2 = 200;
int i = 0;
// I want 100 integers
// WRONG WAY
// sizeof(int) = 4
// len = 100
// Total bytes dedicated/allocated: 100 bytes, NOT ENOUGH
//numabc = malloc(len);
// Right way
// sizeof(int) = 4
// len = 100
// Total bytes dedicated/allocated: 400 bytes
numabc = (int *) malloc(sizeof(int) * len);
// Check if memory is allocated...
// If malloc returned NULL, this condition will become true.
if (!numabc) {
printf("ERROR: Could not allocate memory!\n");
return 1;
}
for (i = 0; i < len; i++) {
printf("%d ", *(numabc + i));
}
printf("\n");
*numabc = 0;
*(numabc + 1) = 1;
for (i = 0; i < len; i++) {
*(numabc + i) = i;
}
printf("%i\n", *numabc);
printf("%i\n", *(numabc + len - 1));
numabc_tmp = (int *) realloc(numabc, sizeof(int) * len2);
if (numabc_tmp != NULL) {
numabc = numabc_tmp;
} else {
free (numabc);
puts ("Error (re)allocating memory");
exit (1);
}
for (i = 0; i < len2; i++) {
*(numabc + i) = i;
}
printf("%i\n", *numabc);
printf("%i\n", *(numabc + len2 - 1));
}
/* File reading */
/* fopen: http://www.cplusplus.com/reference/cstdio/fopen/ */
/* fgetc: http://www.cplusplus.com/reference/cstdio/fgetc/ */
/* fgets: http://www.cplusplus.com/reference/cstdio/fgets/ */
#include <stdio.h>
#include <stdlib.h>
int main() {
/* This is a pointer, but you don't need to care about that */
FILE *file;
char c;
file = fopen("myfile.txt", "r");
if (file == NULL) {
printf("ERROR: File can't be opened!\n");
return 1;
}
c = fgetc(file);
while (c != EOF) {
printf("Char: %c\n", c);
c = fgetc(file);
}
fclose(file);
return 0;
}
/* PREPROCESSOR MACRO TUTORIAL */
#include <stdio.h>
// Conditional macros are processed ONCE - right before they are compiled.
#define bla
#ifdef bla
// Do this code
#else
// Do this code instead
#endif
// This is how the code looks like if bla is defined:
// ...stdio.h pasted here...
DO THiS CODE
// This is how the code looks like if bla is NOT defined:
// ...stdio.h pasted here...
DO THiS CODE INSTEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment