Hi,
So I am working on a FUSE filesystem and I currently have the need to load the contents of a text file into an array. My array initialization looks like: char array[max_entries][PATH_MAX] = {NULL}
The reason I want to pass it by reference is that I want my function to return two values essentially. One a specific char* and the other an array like I initialized. My function proto type looks like:
char* load_meta(char* list[max_entries][PATH_MAX], char* path, int mode);
How I'm trying to call the function: someChar = load_meta(&array, path_name, 1);
Within the function I try to edit the array by deferenceing it once, like this: strcpy(*list[i], file_entry); // This seg faults
What am I doing wrong? Is there a better approach to this problem?
Arrays are always passed by reference. The address of the first element is passed.
But why are you using fixed length arrays? Surely this sort of thing ought to be inside an object and probably dynamically allocated to avoid a mostly empty largish 2D array.
Thanks for the input guys. I ended up using catfish and vlad's solution.
@catfish, the reason for only using one of the dimensions is that I'm trying to copy a char* (string) at a time. If I were to use both indices, [i][j], I would be copying a char at a time.
@ cire If I shouldn't be asking 'c' questions here, can you refer me to a good forum? This is the only site I get quick and knowledgeable responses. I usually use c++ my teacher is just forcing me to use c.
@ kbw, could you elaborate a little bit on an alternative way to do this, to avoid using fixed size arrays. I'm used to being able to organize everything into classes and have string as a data-type. These subtle 'c' differences are really throwing me off.
But why are you using fixed length arrays? Surely this sort of thing ought to be inside an object and probably dynamically allocated to avoid a mostly empty largish 2D array.
ssteuteville wrote:
@ kbw, could you elaborate a little bit on an alternative way to do this, to avoid using fixed size arrays. I'm used to being able to organize everything into classes and have string as a data-type. These subtle 'c' differences are really throwing me off.