diff --git a/DynamicArrays/IntMalloc1D.c b/DynamicArrays/IntMalloc1D.c new file mode 100644 index 0000000..58b0d31 --- /dev/null +++ b/DynamicArrays/IntMalloc1D.c @@ -0,0 +1,36 @@ +#include +#include +#include + +/*In this program we will be introducing dynamic memory allocation to create*/ +/*a 1-dimensional array of integers*/ + +int main() +{ + /*We need to create a pointer to an integer*/ + int i,n,*array; + srand(time(NULL)); + + fprintf(stdout,"Size of array: "); + scanf("%d",&n); + + array = (int *)malloc(n * (sizeof(int))); + /*We just allocated n * sizeof(int) amount of memory to our pointer *array*/ + + if (array == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-1); + } + /*Always check if malloc was successful.*/ + /*Now it works like a regural array.*/ + for(i=0;i +#include +#include + +/*This program is a continuation of intmalloc1D.c*/ +/*Now we will be create a 2D array by creating an array of pointers*/ +/*that point to arrays of integers.*/ + +int main() +{ + /*The beginning is almost the same*/ + int i,j,x,y,**array; + srand(time(NULL)); + + fprintf(stdout,"Size of array(x,y): "); + scanf("%d%d",&x,&y); + + /*Now we will create an array size y of (int *)*/ + array = (int **)malloc(y * sizeof(int *)); + if (array == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-1); + } + /*Each pointer of the array will be pointing to a 1D array*/ + for(i=0;i IntMalloc2D.c -> StructMalloc.c or StrMalloc.c -> +-> StrMallocArr.c -> Realloc.c \ No newline at end of file diff --git a/DynamicArrays/Realloc.c b/DynamicArrays/Realloc.c new file mode 100644 index 0000000..353b6fe --- /dev/null +++ b/DynamicArrays/Realloc.c @@ -0,0 +1,99 @@ +#include +#include +#include +#define TRUE 1 +#define FALSE 0 + +/*There will be time when we the memory that we have allocated won't be enough.*/ +/*That means that we need to allocate more. Thankfully for us, C has got us covered*/ + +int main() +{ + int counter = 0, capacity = 2; + int **array,**forrealloc; + int i,j,n=3,flag=TRUE,res; + char c; + + while (flag == TRUE) + { + /*Because realloc() is a slow function, we will use a capacity integer*/ + /*and we will allocate and reallocate memory based on it.*/ + if (counter == 0) + { + array = (int **)malloc(capacity * sizeof(int *)); + if(array == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-1); + } + for(i=0;i= capacity) + { + capacity += 2; + forrealloc = (int **)realloc(array,(capacity * sizeof(int *))); + if(forrealloc == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-3); + } + array = forrealloc; + /*We only need to allocate memory to the newly created pointers.*/ + for(i=counter;i +#include +#include + +/*In this program we will create a dynamic string. Strings work different in C*/ +/*They are an array of characters. So malloc works here too.*/ + +int main() +{ + int i,size; + char *str,c; + + fprintf(stdout,"Give size of word: "); + scanf("%d",&size); + while((c = getchar()) != '\n'); + + /*When we are allocating memory for a string, we always need to add*/ + /*one more space for the '\0'*/ + str = (char *)malloc((size * sizeof(char)) + 1); + if(str == NULL) + { + fprintf(stderr,"Couldn't allocate memory.\n"); + exit(-1); + } + + fprintf(stdout,"Word: "); + fgets(str,size+1,stdin); + str[strcspn(str,"\n")] = '\0'; + + fprintf(stdout,"The word is: %s\n",str); + free(str); + return 0; +} \ No newline at end of file diff --git a/DynamicArrays/StrMallocArr.c b/DynamicArrays/StrMallocArr.c new file mode 100644 index 0000000..214bdc1 --- /dev/null +++ b/DynamicArrays/StrMallocArr.c @@ -0,0 +1,50 @@ +#include +#include +#include + +/*In this program we will create a dynamic array of strings. Because as we*/ +/*said before strings are arrays of characters, so we need to malloc twice.*/ + +int main() +{ + int amount,size,i; + char **str,c; + + fprintf(stdout,"Choose number of words and size of each word: "); + scanf("%d%d",&amount,&size); + while((c = getchar()) != '\n'); + + str = (char **)malloc(amount * (sizeof(char *))); + if(str == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-1); + } + for(i=0;i +#include +#include + +/*We will create a dynamic array of structs*/ + +typedef struct +{ + char firstname[20]; + char lastname[20]; + int age; +}People; + +int main() +{ + int i,totalp; + char c; + + /*(People *person) creates a pointer to the struct we have created*/ + People *person; + + fprintf(stdout,"Amount of people: "); + scanf("%d",&totalp); + while((c = getchar()) != '\n'); + person = (People *)malloc(totalp * sizeof(People)); + /*Everything after this works exactly like a regural dynamic array.*/ + if (person == NULL) + { + fprintf(stderr,"Couldn't allocate memory\n"); + exit(-1); + } + + for(i=0;i