Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Last active January 19, 2021 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/e8a50e5b724635246e5e30903bf3f17c to your computer and use it in GitHub Desktop.
Save sojohnnysaid/e8a50e5b724635246e5e30903bf3f17c to your computer and use it in GitHub Desktop.
malloc is difficult to understand but hopefully this helps
/*
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