Last active
January 19, 2021 22:55
-
-
Save sojohnnysaid/e8a50e5b724635246e5e30903bf3f17c to your computer and use it in GitHub Desktop.
malloc is difficult to understand but hopefully this helps
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
/* | |
Have an array of char's that can hold | |
the name "3Max" and read from a file | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> // atoi() | |
int main(int argc, char * argv[]) | |
{ | |
if(argc != 2) | |
printf("bzzzz include the infile\n"); | |
// create an infile pointer | |
FILE * infile_pointer = fopen(argv[1], "r"); | |
if(infile_pointer == NULL) | |
printf("something went wrong...\n"); | |
// create a static array variable to hold 3 chars | |
char static_data[3]; | |
//imagine the first byte is our "header info" | |
//telling us how the width or number of chars | |
//in our file | |
char header; | |
fread(&header, sizeof(char), 1, infile_pointer); | |
// read the file's 3 chars into our variable | |
fread(static_data, sizeof(char), 3, infile_pointer); | |
// print to the screen | |
printf("static data is lame %s\n", static_data); | |
printf("we can only hold %c chars\n", header); | |
// close the pointer because we have to! | |
fclose(infile_pointer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment