dynamic array, manual realloc, renames
This commit is contained in:
+45
-21
@@ -3,48 +3,72 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
struct IntArray{
|
||||
struct IntArrayStr{
|
||||
int *data;
|
||||
size_t length;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
void readintarray(struct IntArray *input) {
|
||||
printf("inputlength:%zu\n", input->length);
|
||||
for (size_t i = 0; i < input->length; i++) {
|
||||
printf("digit:%d = %d\n", i, input->data[i]);
|
||||
void read_int_array_str(struct IntArrayStr *in_str) {
|
||||
printf("inputlength:%zu\n", in_str->length);
|
||||
for (size_t i = 0; i < in_str->length; i++) {
|
||||
printf("digit:%zu = %d\n", i, in_str->data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
struct IntArray *create_int_array(size_t length) {
|
||||
struct IntArray *myintarray = malloc(sizeof(struct IntArray)); /* allocate memory for struct */
|
||||
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
||||
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); /* allocate memory for struct */
|
||||
|
||||
myintarray->data = malloc(sizeof(int) * length); /* make sure the pointer in the struct points to the array */
|
||||
myintarray->length = length; //forgot to actually assign a value to the struct
|
||||
mystr->data = malloc(sizeof(int) * in_capacity); /* make sure the pointer in the struct points to the array */
|
||||
mystr->length = 0; //forgot to actually assign a value to the struct
|
||||
mystr->capacity = in_capacity;
|
||||
|
||||
if (myintarray->data == NULL) {
|
||||
if (mystr->data == NULL) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
return myintarray;
|
||||
return mystr;
|
||||
}
|
||||
|
||||
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
|
||||
if (in_str->length == in_str->capacity) {
|
||||
in_str->capacity*=2;
|
||||
printf("intarray capacity is now:%zu\n", in_str->capacity);
|
||||
int *temp;
|
||||
temp = realloc(in_str->data, sizeof(int)*in_str->capacity);
|
||||
if(temp == NULL){exit(0);};
|
||||
in_str->data = temp;
|
||||
printf("intarray length is now:%zu\n", in_str->length);
|
||||
}
|
||||
in_str->data[in_str->length] = in_int;
|
||||
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
|
||||
in_str->length++; //didn't read the flow when transfering and accidentally put this in the if block
|
||||
//also had an off-by-one error because I increased length before doing anything
|
||||
}
|
||||
|
||||
int main() {
|
||||
srand(time(NULL));
|
||||
printf("how many numbers?\n");
|
||||
int length;
|
||||
scanf("%d", &length);
|
||||
// change to while loop and take inputs rather than fill with random numbers
|
||||
printf("input numbers:\n");
|
||||
|
||||
struct IntArray *myintarray = create_int_array(length);
|
||||
//int input;
|
||||
size_t length = 10;
|
||||
/* while (input != -1) { */
|
||||
/* scanf("%d", &input); */
|
||||
/* length++; */
|
||||
/* } */
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
myintarray->data[i] = rand() % 10;
|
||||
printf("number:%d = %d\n",i,myintarray->data[i]);
|
||||
struct IntArrayStr *myintarraystr = create_int_array_str(2);
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
int input = rand() % 10;
|
||||
push_int_array_str(input, myintarraystr);
|
||||
}
|
||||
|
||||
readintarray(myintarray);
|
||||
read_int_array_str(myintarraystr);
|
||||
|
||||
free(myintarray->data);
|
||||
free(myintarray);
|
||||
free(myintarraystr->data);
|
||||
free(myintarraystr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user