Skip to content

Instantly share code, notes, and snippets.

View joegasewicz's full-sized avatar
🦧
🍾

Joe Gasewicz joegasewicz

🦧
🍾
View GitHub Profile
@joegasewicz
joegasewicz / types.c
Last active August 16, 2023 09:33
C Primitive Types
#include <stdio.h>
#include <stdlib.h>
int main()
{
int myInt;
signed int mySignedInt;
unsigned int myUnsignedInt;
short myShort;
@joegasewicz
joegasewicz / double_pointers.c
Created December 6, 2022 08:59
Double Pointers (pointer to a pointer)
#include <stdio.h>
#include <stdlib.h>
int main()
{
/* ====================================================================== */
/* Double Pointers (Pointer to a Pointer) */
/* ====================================================================== */
// 2 levels of indirection
int **dblPtr;
@joegasewicz
joegasewicz / dynamic_memory_location.c
Last active November 26, 2022 02:25
Dynamic Memory Allocation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/* ====================================================================== */
/* Dynamic Memory Allocation */
/* ====================================================================== */
char *str = NULL;
@joegasewicz
joegasewicz / pass_by_reference.c
Last active November 26, 2022 00:52
Pass By Reference
#include <stdio.h>
#include <stdlib.h>
void square_num(int *num);
int main()
{
/* ====================================================================== */
/* Pass By Reference */
@joegasewicz
joegasewicz / str_len.c
Last active November 25, 2022 22:23
Count the length of a string in C
#include <stdio.h>
#include <stdlib.h>
int calc_len_str(const char *str);
int main()
{
char str[] = "How are you today!";
int result = calc_len_str(str);
printf("result 1 = %d\n", result);
@joegasewicz
joegasewicz / pointers_and_strings.c
Last active November 25, 2022 21:23
Pointer & Strings
int main()
{
char string1[] = "A string to be copied.";
char string2[50];
copyStrong1(string2, string1);
return 0;
}
@joegasewicz
joegasewicz / pointer_arithmetic.c
Created November 25, 2022 18:39
Pointer Arithmetic
int main()
{
/* ====================================================================== */
/* Pointer Arithmetic */
/* ====================================================================== */
/*
int urn[3];
int *ptr1, *ptr2
@joegasewicz
joegasewicz / pointers_to_arrays.c
Last active November 25, 2022 18:40
C Pointers to Arrays
/* ====================================================================== */
/* Pointers to Arrays */
/* ====================================================================== */
// point to first item in an array
char arr[] = {'a', 'b', 'c'};
// apply the address operator to the first element in the array
char *arrPtr = &arr[0]; // without address operator you would get a value
// C compiler treats the array name (arr) without subscript ([])
// as a pointer to the array.
@joegasewicz
joegasewicz / void_pointers.c
Created November 23, 2022 13:44
Void Pointers
/* ====================================================================== */
/* Void Pointers*/
/* ====================================================================== */
// void* absence of a type means you are able to store any type
// - Often used as a return type or a parameter type of functions
// - You must cast it to a type when you dereference it
int i = 10;
float f = 2.34;
char ch = 'k';
@joegasewicz
joegasewicz / pointers.c
Last active August 16, 2023 10:54
C Pointers
// * - indirection operator
int *pNumber = NULL, myNum = 1;
char *my_str = "hello";
long num1 = 0L, num2 = 0L, *pNum = NULL;
if (pNumber == NULL)
printf("pNumber is currently NULL\n");
pNumber = &myNum;