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*.
A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().
C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index.
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*);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With