fixedynarr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. /*
  7. todo
  8. change intarray into str array
  9. make push array malloc for new str
  10. make input strings instead of int (should use fgets?)
  11. */
  12. struct StringArray{
  13. char **data;
  14. size_t length;
  15. size_t capacity;
  16. };
  17. void print_SA(struct StringArray *in_str) {
  18. for (size_t i = 0; i < in_str->length; i++) {
  19. printf("word:%zu = %s\n", i, in_str->data[i]);
  20. }
  21. }
  22. struct StringArray *SA_create(size_t in_capacity) {
  23. struct StringArray *mystr = malloc(sizeof(struct StringArray)); //allocate memory for struct
  24. mystr->data = malloc(sizeof(char*) * in_capacity); //make sure the pointer in the struct points to the array
  25. mystr->length = 0; //forgot to actually assign a value to the struct
  26. mystr->capacity = in_capacity;
  27. if (mystr->data == NULL) {
  28. exit(1);
  29. }
  30. return mystr;
  31. }
  32. int get_string_len(char *in_string){
  33. int length = 0;
  34. char currchar;
  35. do {
  36. currchar = in_string[length];
  37. length++;
  38. }
  39. while (currchar != '\0');
  40. 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.
  41. return length;
  42. }
  43. /* push value to struct, also handle realloc */
  44. void SA_push(char *in_string, struct StringArray *in_str){
  45. if (in_str->length == in_str->capacity) { //check if already full
  46. in_str->capacity*=2; //if so we should double
  47. printf("array capacity is now:%zu\n", in_str->capacity);
  48. char **temp; //create a temporary pointer
  49. temp = realloc(in_str->data, sizeof(char*)*in_str->capacity); //realloc the data into a new bucket
  50. if(temp == NULL){exit(0);}; //check if actually valid
  51. in_str->data = temp; //if valid, make sure the pointer in the struct points to the realloced block
  52. }
  53. 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
  54. strcpy(newword, in_string); //copy word into memory
  55. in_str->data[in_str->length] = newword;
  56. printf("word:%zu = %s\n", in_str->length, in_str->data[in_str->length]);
  57. in_str->length++; //didn't read the flow when transferring and accidentally put this in the if block
  58. printf("array length is now:%zu\n", in_str->length);
  59. //also had an off-by-one error because I increased length before doing anything
  60. }
  61. void SA_free(struct StringArray *inp_str) {
  62. for (size_t i = 0; i < inp_str->length; i++) { //for each pointer in array
  63. free(inp_str->data[i]); //free at pointer
  64. }
  65. free(inp_str->data); //free pointer to pointers
  66. free(inp_str); //free the struct
  67. }
  68. int main() {
  69. srand(time(NULL));
  70. printf("input words:\n");
  71. struct StringArray *mystringarraystr = SA_create(2);
  72. char input[50];
  73. do{
  74. fgets(input, sizeof(input), stdin); //get input from stdin of size input[] and put it in input
  75. SA_push(input, mystringarraystr); //push the value of input into mystr
  76. } while (*input != '-');
  77. print_SA(mystringarraystr);
  78. SA_free(mystringarraystr);
  79. return 0;
  80. }