1

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.

2
  • When you're copying the name into names[x] just use strncpy (assuming the names are null-terminated). You don't have to copy if char by char. Commented Jan 31, 2018 at 0:10
  • void read_files(char** names, int size), using char** means pointer to char pointers, and pass size as 25 Commented Feb 2, 2018 at 2:38

2 Answers 2

1

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[]);
Sign up to request clarification or add additional context in comments.

6 Comments

@chux I missed that, I fixed my answer
I think you mean element. I hope to god strings dont have to be declared in five dimensions
@Mitchel0022 I meant elements, but declaring an array in 5 dimensions... that would be awesome.
good info on the strcpy vs strncpy. Is one better than the other as far as comparison? Because another step is before I copy the name into the name array, I need to make sure it doesn't exist in one of the other elements.
@TannerDanger strcpy will copy the whole string and strncpy will copy only n bytes. Ones not better than the other, they are just used for different things
|
0
#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

1 Comment

> "In memory a 2d array is still a 1d block with fancy addressing." Good info, I completely forgot about that. This is helpful for another part of this assignment, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.