0

So for my school project, a large CSV file will be entered through stdin and we will have to sort it based on column and print it out as a sorted csv file.

The step I am on right now is figuring out how to keep reallocing a struct of arrays so that it will grow if there is not big enough to hold the data coming in from stdin. We don't know the exact amount of rows that will be inputted in the CSV file. Right now we just used a static amount to test and see if the values are assigned to the structs.

I am still a beginner at C so I do not clearly know how I would iterate through a pointer like I would iterate through an array. Since we are using a static amount of structs in the array, we can just iterate using array[i] like in Java but how would you iterate through something like *array?

I do not know where to start for creating this dynamic array. I tried

struct array* testArray = (array*)malloc(sizeof(testArray));

but I have no idea how to iterate through it like I did with the static array by using array[i].

Any help would be greatly appreciated, sorry for the wall of text...

J...S
  • 5,079
  • 1
  • 20
  • 35
codemonkey
  • 58
  • 1
  • 8

2 Answers2

0
struct array* testArray = (array*)malloc(sizeof(testArray));

is a little wrong as you only allocate 1 element of testArray.

It is more like:

struct A
{
    int a;
    int b;
    ....
};

struct A* arr = malloc( N * sizeof(struct A) );
                       ^^^
                       N element of struct A

int j;
for (j=0; j<N; ++j)  // Iterate it like any other array
{
    arr[j].a = 5;
    arr[j].b = 42;
    ....
}

Use realloc when you need the array to grow.

When reading from a file/stdin it could look like (based on comment from David C. Rankin):

int n=0; // Count of the number of structs read from the file

struct A* arr = malloc( N * sizeof(struct A) );
while (read line from file)
{
    arr[n].a = val1; 
    arr[n].b = val2; 
    ++n;              // Increment count
    if (n == N)       // Check if current array is full, i.e. realloc needed
    { 
       // realloc array to 2 * N; N = N * 2 
    }
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

You can navigate through a malloced space the same way as with an array (using indicies), but it seems that your main issue lies in your use of malloc. Malloc's argument is the size in number of bytes that you want to allocate. So if you want to have an array of structs, you would first need to find out how many bytes one struct contains using sizeof(struct array), and then determine how large of an array you want, let's say N. So that line of code should look more like struct array* testArray = malloc(N * sizeof(struct array));. The return value of malloc will be a void pointer containing the memory address of the first byte of allocated space. Upon assigning this value to testArray, it will be type-casted to the assigned variable type (struct array *). Now you can use pointer arithmetic to access a specific index i with *(testArray + i), or simply testArray[i]. If you find that N was not a sufficient size, you can use realloc to increase the array size to 2N, or whatever size deemed necessary.

A.A.O.
  • 26
  • 2