I'm sure this question has been asked before, but I've been searching pretty constantly for the last 2 days and can't solve this, so I'm hoping someone will help me out.
For my C assignment, we are supposed to receive each function parameter as a pointer. One of the values passed is a 2D char array, and for the last 48+ hours, I haven't been able to find out how to properly do this in my book, notes, instructor slides, etc. and actually make it work.
Simplified code of what I'm trying to accomplish
void main(){
/*
This is the value being passed. The first dimension has a size of 1000,
to account for 1000 different sorted people. Second dimension is an
array that holds the actual name, with a max name length of 25.
*/
char names[1000][25] = {'\0'};
read_files(names);
}
void read_files(char* names){
char newName1[4] = "mary";
char newName2[4] = "anna";
for(int i = 0; i < 5; i++){
names[0][i] = newName1[i];
}
for(int i = 0; i < 5; i++){
names[1][i] = newName2[i];
}
}
Basically, I'm trying to get names[0][x] to have x = "mary", and names[1][x] to have x = "anna".
(Or, more literally, names[0][0]= 'm', names[0][1]= 'a', etc.)
Unfortunately I can't get the passing right. The closest I've gotten is to have it assign one name to names, but not anymore.
Any help with this would be fantastic. There's actually quite a few classmates I'm working with who are all stumped on this. I'm sure its easy for experienced guys, but we just haven't gotten good instruction on how to do it and I can't find many specific examples that address this exact issue.
As I said, our instructor specifically wants function arguments to be pointers.
EDIT
Good info so far, I was very close to a few of these examples. I'll give them a shot and see what works.
names[x]just usestrncpy(assuming the names are null-terminated). You don't have to copy if char by char.void read_files(char** names, int size), using char** means pointer to char pointers, and passsizeas 25