Reputation: 33
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.
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.
Upvotes: 1
Views: 109
Reputation: 13580
You would have to define read_files
as this:
void read_files(char (*names)[25]) {
char newName1[5] = "mary";
char newName2[5] = "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];
}
}
Because you need to pass a pointer to an array of char[25]
. A two
dimensional array cannot be converted into a double-pointer or a single pointer.
Note also that newName1
and newName2
must be arrays that can hold 5 elements. In C a
string is a sequence of characters that ends with the '\0'
-terminating byte.
That means that for a string of length n
, you need a char
array of length
n+1
, because you need the extra byte for the '\0'
-terminating byte.
Also the better way of copying strings is strcpy
or strncpy
. strncpy
is
more safe than strcpy
because you limit the amount of bytes to be copied, thus
avoiding a buffer overflow. But strncpy
might not write the '\0'
-terminating byte
if there is not enough room.
Instead of:
for(int i = 0; i < 5; i++){
names[0][i] = newName1[i];
}
use this:
strcpy(names[0], newName1);
Or the strncpy
way:
strncpy(names[0], newName1, sizeof names[0]);
names[0][sizeof(names[0]) - 1] = 0; // making sure to add the \0 byte
Also bear in mind, that the correct definition of the main
function can be
only one of these:
int main(void);
int main(int argc, char **argv);
int main(int argc, char *argv[]);
Upvotes: 1
Reputation: 3418
#include <stdio.h>
void read_files(char* names){
char newName1[4] = "mary";
char newName2[4] = "anna";
for(int i = 0; i < 5; i++){
names[i] = newName1[i];
}
int offset = 25;
for(int i = 0; i < 5; i++){
names[i + offset] = newName2[i];
}
}
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[0]);
printf("%s %s", names[0], names[1]);
}
In memory a 2d array is still a 1d block with fancy addressing. So you could have an offset variable and just add it on to every name like I have done here
Upvotes: 0