1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <stdio.h> #include <string.h> struct person { char name[50]; int age; }; void capFirst(char str[]){ str[0] -= 32; } void a(int i, int j){ printf("i: %d j: %d", i, j); } int* dontdothis(){ int i = 4; return &i; } void someotherfun(){ int i = 23; } int main() { printf("Hello, World!\n"); // int *ip = malloc(sizeof(int)); // *ip = 5; // // a(*ip, 3); char str1[] = "hello"; printf("%s\n", str1); capFirst(str1); printf("%s\n", str1); char *str2 = malloc(sizeof(char) * 6); strcpy(str2, "hello"); printf("%s\n", str2); capFirst(str2); printf("%s\n", str2); printf("%c\n", str2[3]); printf("%c\n", (*str2+3)); // pointer arithmetic printf("%p\n", (str2+3)); // printing the pointer itself int* pi = dontdothis(); printf("pi: %d\n", *pi); someotherfun(); printf("pi: %d\n", *pi); // Illustating writing past the bounds of the array, not using null terminator. char* a1 = malloc(sizeof(char)); a1[0] = 'a'; a1[1] = 'b'; printf("a1: %s\n", a1); free(a1); //a1 = NULL; // Always set to null after freeing, otherwise you have a dangling pointer. printf("a1: %s\n", a1); // a2 is allocated at the same spot a1 was. char* a2 = malloc(sizeof(char)); //a2[0] = 'c'; printf("a2: %s\n", a2); // Making a 2d array int** arr2d = malloc(sizeof(int*) * 4); for ( int i = 0; i < 4; i++ ){ arr2d[i] = malloc(sizeof(int) * 4); } return 0; } |