1/40
These flashcards cover essential concepts related to strings, pointers, memory allocation, and file handling in the context of C programming.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
String Terminator
The character used to signify the end of a string in C, represented as '\0'.
Dynamic Memory Allocation
The process of allocating memory at runtime using functions such as malloc and calloc.
Memory Leak
A situation where allocated memory is not freed, leading to a reduction in available memory.
uses heap memory and pointers
Pointers
Variables that store the memory address of another variable.
Pass by Reference\ pass by pointer
A method of passing arguments to functions where the function receives a reference to the variable, allowing it to modify the original value.
ex: int functionName( int* var1, int* var2){}
File Pointer
A pointer used to refer to an open file in C for reading or writing data.
strlen() function
A standard library function that returns the length of a string excluding the null terminator.
malloc() function
A standard library function that allocates a specified number of bytes in memory and returns a pointer to it.\
ex: data type *ptrVar = (data type*)malloc(sizeof(var) * length);
fopen() function
A function used to open a file and return a file pointer.
fclose() function
A function used to close an opened file and free any resources associated with it.
argc and argv
Parameters in the main function that represent the count of command line arguments and the list of arguments themselves, respectively.
Dereferencing a pointer (* - asterisk)
The act of accessing the value stored at the memory address that a pointer is pointing to.
2D Array of Strings
An array that contains strings organized in rows and columns.
Freeing Memory
The process of releasing previously allocated memory back to the system to prevent memory leaks.
ampersand (&)
The operator used in C to obtain the address of a variable.
calloc() function
A function that allocates memory for an array of elements, initializing all bytes to zero.
ex: data type* varName = calloc(numOfElements, sizeof(data type));
library need for things like strcpy() or strlen()
string.h
what is around characters
single quotes- ‘A’
what is around strings/string literals
double quotes- “Hello”
what should pointers be initialized to
NULL
how to declare a pointer
data type *variableNamePointer = &varibaleName;
this syntax allows the pointer to hold the address of the variable as its “value”.
what is the notation of a pointer address
hexadecimal notation
what do pointers allow for functions
to access local variables from other functions and to manipulate data in memory directly.
how to process a file
declare a file pointer: FILE *ptrName;
set file pointer to the desired file using fopen function: ptrName = fopen("filename.txt", "mode");
do what you need with the text file
and then close the file: fclose(ptrName).
3 types of file modes
"r" for read, "w" for write, "a" for append.
fscanf();
A function used to read formatted input from a file. It allows for retrieving data from a file based on specified format specifiers.
fscanf syntax
fscanf(filePointer, “formatIdentifier”, variable);
ex: fscanf(stdin, "%d", &num);
fprintf();
A function used to write formatted output to a file. It allows for displaying data to a file based on specified format specifiers.
fprintf syntax
fprintf(filePointer, “formatIdentifier”, variable);
ex: fprintf(stdout, "Hello, %s!\n", name);
stack memory
A type of memory allocation that occurs in a last-in, first-out manner, used for storing local variables and function call information. Memory is automatically managed and released when a function exits.
global memory
Memory that is accessible throughout the program, typically used for storing global variables and data that must persist beyond a single function call. This memory remains allocated until the program terminates.
heap memory
A type of memory allocation used for dynamic storage where variables are allocated and freed manually, with no automatic management. It allows for more flexible memory usage but requires careful handling to avoid memory leaks.
realloc()
Used to change the size of a previously allocated memory block, allowing dynamic resizing of memory. It can either expand or shrink the allocated memory while preserving the contents, returning a pointer to the reallocated memory.
realloc syntax
data type* realloc(pointerVar, sizeof(var) * num of elements)
free()
must use after allocating memory
free syntax
free(pointerVar);
can you use scanf to read in strings
yes, but it stops at the first whitespace.
arrays with 2d strings
dataype[x][y];
where x = number of strings
and y = number of chars in each string
ex: char words[5][20]; (holds 19 chars + \0)
setting a variable to an address
int i = &x
setting a variable to a value at an address
int i = *x
example of a memory leak