Reputation: 73
Hi I am studying about sorting algorithms, for that I want to make a simple program for getting an array of integers from a text file. While doing that I'm having some trouble and questions regarding arrays and functions that take them as parameters . Here is what I do:
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 64
#define MAX_INT_SIZE 10000
void itobuff(const char* istring,const int** sint);
int getistring(FILE* file,char strbuffer[][MAX_SIZE],int max_int,int max);
int main(int argc,char*argv[]){
char buffer[MAX_INT_SIZE][MAX_SIZE];
int int_counter=0;
int int_buffer[MAX_INT_SIZE];
FILE *file = fopen("MYFILE.txt","r");
getistring(file,buffer,MAX_INT_SIZE,MAX_SIZE);
return 0;
}
My doubt is about the definition of the function
int getistring(FILE* file,char strbuffer[][MAX_SIZE],int max_int,int max);
I want to write a function which allows to use any size array. I know this is wrong, but logically it is what I want to achieve but can't figure out how.
int getistring(FILE* file,char strbuffer[][],int max_int,int max);
I know that what I want to do may be done in some other way but I want to know how to do it. For example if I want to write a function that gets an array and returns the determinant of that array, you shouldn't be forced to restrict yourself to a k size array. Or more generally to a i-columns,j-rows array for any other array operation.
Upvotes: 2
Views: 181
Reputation: 63481
Two-dimensional arrays are actually laid out in one dimension. The reason you need to provide the step-size is so the compiler knows how to multiply indices to make a linear index. If you want to use arbitrary sized arrays, use a 1-D array and provide a step size.
Consider this:
char strbuffer[][MAX_SIZE];
The compiler knows that to get an element at strbuffer[i][j]
, it must use:
strbuffer + i * MAX_SIZE + j;
Notice that I've used strbuffer
as if it was a pointer. You're allowed to do this - the compiler will convert an array into a pointer as a convenience.
So, redefine your function to take a 1D array, and use the computation above. If you're not already passing the step size (or width, or whatever you call it), then add that as a paramter:
int getistring(FILE* file, char strbuffer[], int max_int, int max);
Upvotes: 0
Reputation: 223493
If you are using a C implementation that supports C 1999, then it supports variable-length arrays.
Declare a function that takes a variable-length array parameter like this:
int getistring(FILE *file, size_t Rows, size_t Columns, char buffer[][Columns]);
Call the function like this:
result = getistring(file, Rows, Columns, buffer);
Create the buffer array like this:
size_t Rows = some calculation for number of rows;
size_t Columns = some calculation for number of columns;
char (*buffer)[Columns] = malloc(Rows * sizeof *buffer);
if (!buffer)
Handle error.
When done, free the buffer array like this:
free(buffer);
If the numbers of rows and columns are small, you can define the buffer array with automatic storage instead of using malloc
and free
, like this:
char buffer[Rows][Columns];
Upvotes: 3