12

How do you pass a char array into a function.

declarations

char fromName[64];
char fromStreet[64];
char fromSuburb[64];
char fromCountry[64];

function call

    Trans[i]->putAddress(fromName, fromStreet, fromSuburb, fromCountry);

prototype

void putAddress(char,char,char,char);

function    
void putAddress(char fName,char fStreet,char fSuburb,char fCountry){

        return;
}

and Error "main.cpp", line 86: Error: Formal argument 1 of type char in call to Mail::putAddress(char, char, char, char) is being passed char*.

0

9 Answers 9

9

Your function should be:

void putAddress(char *,char *,char *,char *);
Sign up to request clarification or add additional context in comments.

2 Comments

awesome but now its saying that putAddress needs a prototype
You need to prototype your function. void putAddress(char *,char *,char *,char *); put this line after using namespace std
9

You need to pass pointers to char

void putAddress(char* fName,char* fStreet,char* fSuburb,char* fCountry);

You then need to be careful you know the size of each array so you don't index off the end, in your case all of them are 64.

Comments

9

You can pass an array in 2 ways:

(1) Conventional C-style:
Here you pass by address and receive using a pointer

void putAddress(char *,char *,char *,char *);

(2) C++ pass by reference:
You pass the array by reference with size specification:

 void putAddress(char (&a1)[64], char (&a2)[64],char (&a3)[64], char (&a4)[64]);

This helps you getting the array-size straight away correct (pointer is not allowed). This can be made more sophisticated using template also.

You can also iterate the option of using std::string, which will make a copy of the whole array and manage it as an automatic variable.

Comments

5

You pass strings (arrays of characters) as a pointer to the first character of the array:

void something(char *str) { /* ... */ }

int main(int argc, char **argv) {
    char somestring[] = "Hell World!\n";

    something(somestring);

    return 0;
}

Because arrays automatically decay to pointers when passed to a function all you have to do is pass the character array and it works. So in your example:

void putAddress(char*, char*, char*, char*);

Comments

4
void putAddress(char* array){
    //use array as usual
}

Comments

4

The compiler is telling you right there... Its being passed as char*. So use either char* or char ar[].

Comments

2

To correct your code:

void putAddress(char*,char*,char*,char*);

but it's still wrong. Arrays decay to pointers, that's why it will compile, but will result in an error if the arguments are not null-terminated. You should also pass in the size if you choose this approach.

However, since this is C++ and not C, I suggest you use std::string instead:

void putAddress(const std::string&,const std::string&,const std::string&,const std::string&);

Comments

0

The compiler's error makes sence as fromName is indeed a pointer to the (first element of the) fromName array. This is just C++ (and plain C) syntax.

In order to pass a char array to a function you should do what you are currently doing, that is, pass a pointer to the (first element of the) array.

So all you need to do is change

    void putAddress(char,char,char,char);

to

    void putAddress(char *, char *, char *, char *);

PS: Your next problem is knowing (making putAddress aware of) each array's length. If these are fixed though, you have no problem.

1 Comment

this is wrong. if you actually do this and try to interact with said char array within the function, you will likely get an 'excess elements in scalar initializer' error (when using functions like strcmp)
0
void putAddress(char[],char[],char[],char[]);

function    
void putAddress(char fName[],char fStreet[],char fSuburb[],char fCountry[]){

        return;
}

You have forgotten to put the paranthesis, Put them as in the above code.

Comments

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.