Using C in Linux
Hi:
I'm trying to read a list of file names into an array of strings.
Yes...i know 'C' doesn't have strings, so its a 2 dimensional
array of characters.
The issue is that I pass the array (its a pointer after all)
to a function that lists the directory and copies the file names
into the array.
The problem is that i can't address the different array elements.
If I put the code in the main() function, it works fine, but
in the directory reading function, it doesn't let me address
the array elements using the subscript notation like array[4]
I get the error msg arithmetic on a pointer to an incomplete type
So my question is how do I address the array so that I can copy
the filenames in?
variable declarations
char path[100];
char names[100][100];
function call: readindir(path, names);
My function:
void readindir(char *path, char names[][]){
DIR *dir;
struct dirent *mydirent;
int i = 0;
dir = opendir(path);
while((mydirent = readdir(dir)) != NULL){
strcpy(names, mydirent->d_name);
printf("%3d : %s\n", i++, mydirent->d_name);
printf(" %s", names);
}
close(dir);
}
Hi:
I'm trying to read a list of file names into an array of strings.
Yes...i know 'C' doesn't have strings, so its a 2 dimensional
array of characters.
The issue is that I pass the array (its a pointer after all)
to a function that lists the directory and copies the file names
into the array.
The problem is that i can't address the different array elements.
If I put the code in the main() function, it works fine, but
in the directory reading function, it doesn't let me address
the array elements using the subscript notation like array[4]
I get the error msg arithmetic on a pointer to an incomplete type
So my question is how do I address the array so that I can copy
the filenames in?
variable declarations
char path[100];
char names[100][100];
function call: readindir(path, names);
My function:
void readindir(char *path, char names[][]){
DIR *dir;
struct dirent *mydirent;
int i = 0;
dir = opendir(path);
while((mydirent = readdir(dir)) != NULL){
strcpy(names, mydirent->d_name);
printf("%3d : %s\n", i++, mydirent->d_name);
printf(" %s", names);
}
close(dir);
}