Passing an array of char[][] by reference (C not CPP)

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.
In the operator precedence table below, operator * has lower precedence than operator [].
http://en.cppreference.com/w/c/language/operator_precedence

So maybe what you want to do is... but it still doesn't make sense: where is the second index? Two indices are needed.
strcpy((*list)[i], file_entry);

You might want to read this.
http://c-faq.com/aryptr/aryvsadr.html
You cannot pass by reference in C.

load_meta takes a (pointer to a) two-dimensional array of pointers to char. The array you declared is a two-dimensional array of char.

Presumably your compiler generates a warning for this and you should pay attention to it. If it doesn't, turn up your warning levels.

Perhaps the following will help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string.h>
#include <stdio.h>

#define MAX_ENTRIES 256
#define PATH_MAX 256

typedef char list_type[MAX_ENTRIES][PATH_MAX] ;

char* load_meta( list_type list, char* path, int mode ) ;

int main()
{
    char path_name[] = "/path_name" ;
    list_type array = { 0 } ;

    printf( "%s\n", load_meta(array, path_name, 1)) ;
}

char* load_meta(list_type list, char* path, int)
{
    return strcpy(list[0], path) ;
}


Please note that this is not a C programming site and you'll probably get better results on a forum where this is on topic.
this is not a C programming site
We do cover C here.

You cannot pass by reference in C.
Why do you think he's not using C++.

You can pass by reference in C, but you have to pass the address yourself (by value).
The correct syntax looks the following way

char* load_meta( char ( * list )[max_entries][PATH_MAX], char* path, int mode);

Inside the function expression *list will give you the original array.

strcpy( ( *list )[i], file_entry);
We do cover C here.

Says whom? Do you see a C programming forum?


Why do you think he's not using C++.

See the subject of this thread.


You can pass by reference in C, but you have to pass the address yourself (by value).

C doesn't have references. Therefore you cannot pass by reference in C. You may certainly pass a pointer by value as you describe.
Last edited on
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.

Thanks again!
kbw wrote:
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.


I'm not kbw, but I think he means using malloc() and free().
http://cplusplus.com/reference/cstdlib/malloc/
http://cplusplus.com/reference/cstdlib/free/

http://c-faq.com/aryptr/dynmuldimary.html

I have a different suggestion. If you write pure C and you use a compiler that supports C99 features (such as GCC), consider using VLAs (variable-length arrays) instead of dynamically allocated arrays -- for simplicity.
http://stackoverflow.com/questions/3082126/c99-faq-and-variable-length-arrays
Topic archived. No new replies allowed.