3,4 handling strings in/using a struct, redid naming
This commit is contained in:
+59
-27
@@ -1,68 +1,100 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
struct IntArrayStr{
|
|
||||||
int *data;
|
/*
|
||||||
|
todo
|
||||||
|
change intarray into str array
|
||||||
|
make push array malloc for new str
|
||||||
|
make input strings instead of int (should use fgets?)
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct StringArray{
|
||||||
|
char **data;
|
||||||
size_t length;
|
size_t length;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
};
|
};
|
||||||
|
|
||||||
void read_int_array_str(struct IntArrayStr *in_str) {
|
void print_SA(struct StringArray *in_str) {
|
||||||
for (size_t i = 0; i < in_str->length; i++) {
|
for (size_t i = 0; i < in_str->length; i++) {
|
||||||
printf("digit:%zu = %d\n", i, in_str->data[i]);
|
printf("word:%zu = %s\n", i, in_str->data[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IntArrayStr *create_int_array_str(size_t in_capacity) {
|
struct StringArray *SA_create(size_t in_capacity) {
|
||||||
struct IntArrayStr *mystr = malloc(sizeof(struct IntArrayStr)); //allocate memory for struct
|
struct StringArray *mystr = malloc(sizeof(struct StringArray)); //allocate memory for struct
|
||||||
|
|
||||||
mystr->data = malloc(sizeof(int) * in_capacity); //make sure the pointer in the struct points to the array
|
mystr->data = malloc(sizeof(char*) * 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->length = 0; //forgot to actually assign a value to the struct
|
||||||
mystr->capacity = in_capacity;
|
mystr->capacity = in_capacity;
|
||||||
|
|
||||||
if (mystr->data == NULL) {
|
if (mystr->data == NULL) {
|
||||||
exit(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mystr;
|
return mystr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int get_string_len(char *in_string){
|
||||||
|
int length = 0;
|
||||||
|
char currchar;
|
||||||
|
do {
|
||||||
|
currchar = in_string[length];
|
||||||
|
length++;
|
||||||
|
}
|
||||||
|
while (currchar != '\0');
|
||||||
|
|
||||||
|
printf("length of word: %d\n", length); //for some reason length is always 2 more than expected, so 1 more than necessary? apparently because of \n, not sure how to filter that.
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* push value to struct, also handle realloc */
|
/* push value to struct, also handle realloc */
|
||||||
void push_int_array_str(int in_int, struct IntArrayStr *in_str){
|
void SA_push(char *in_string, struct StringArray *in_str){
|
||||||
if (in_str->length == in_str->capacity) { //check if already full
|
if (in_str->length == in_str->capacity) { //check if already full
|
||||||
in_str->capacity*=2; //if so we should double
|
in_str->capacity*=2; //if so we should double
|
||||||
printf("intarray capacity is now:%zu\n", in_str->capacity);
|
printf("array capacity is now:%zu\n", in_str->capacity);
|
||||||
int *temp; //create a temporary pointer
|
char **temp; //create a temporary pointer
|
||||||
temp = realloc(in_str->data, sizeof(int)*in_str->capacity); //realloc the data into a new bucket
|
temp = realloc(in_str->data, sizeof(char*)*in_str->capacity); //realloc the data into a new bucket
|
||||||
if(temp == NULL){exit(0);}; //check if actually valid
|
if(temp == NULL){exit(0);}; //check if actually valid
|
||||||
in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
|
in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
|
||||||
}
|
}
|
||||||
in_str->data[in_str->length] = in_int;
|
|
||||||
printf("number:%zu = %d\n", in_str->length, in_str->data[in_str->length]);
|
char *newword = malloc(sizeof(char) * get_string_len(in_string)); //allocate memory for new string, apparantly my function already includes the length of null term
|
||||||
|
strcpy(newword, in_string); //copy word into memory
|
||||||
|
in_str->data[in_str->length] = newword;
|
||||||
|
printf("word:%zu = %s\n", in_str->length, in_str->data[in_str->length]);
|
||||||
in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
|
in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
|
||||||
printf("intarray length is now:%zu\n", in_str->length);
|
printf("array length is now:%zu\n", in_str->length);
|
||||||
//also had an off-by-one error because I increased length before doing anything
|
//also had an off-by-one error because I increased length before doing anything
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SA_free(struct StringArray *inp_str) {
|
||||||
|
for (size_t i = 0; i < inp_str->length; i++) { //for each pointer in array
|
||||||
|
free(inp_str->data[i]); //free at pointer
|
||||||
|
}
|
||||||
|
free(inp_str->data); //free pointer to pointers
|
||||||
|
free(inp_str); //free the struct
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
printf("input numbers:\n");
|
printf("input words:\n");
|
||||||
|
|
||||||
struct IntArrayStr *myintarraystr = create_int_array_str(2);
|
struct StringArray *mystringarraystr = SA_create(2);
|
||||||
|
|
||||||
int input;
|
char input[50];
|
||||||
size_t length = 10;
|
do{
|
||||||
|
fgets(input, sizeof(input), stdin); //get input from stdin of size input[] and put it in input
|
||||||
while (input != -1) {
|
SA_push(input, mystringarraystr); //push the value of input into mystr
|
||||||
scanf("%d", &input);
|
} while (*input != '-');
|
||||||
push_int_array_str(input, myintarraystr);
|
|
||||||
}
|
|
||||||
|
|
||||||
read_int_array_str(myintarraystr);
|
print_SA(mystringarraystr);
|
||||||
|
|
||||||
free(myintarraystr->data);
|
SA_free(mystringarraystr);
|
||||||
free(myintarraystr);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user