Passing a char array to a function

How to pass a char array to a function.

ads

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 the error "main.cpp", line 86: Error: formal argument 1 of type char in the call to Mail :: putAddress (char, char, char, char)) passed char *.

+8
c ++ function arrays char
source share
9 answers

Your function should be:

 void putAddress(char *,char *,char *,char *); 
+10
source share

You need to pass pointers to char

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

Then you need to be careful to find out the size of each array so that you do not index it, in your case all of them are 64.

+8
source share
 void putAddress(char* array){ //use array as usual } 
+4
source share

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; } 

Since arrays automatically break into pointers when passing a function, all you have to do is pass an array of characters, and it works. So in your example:

 void putAddress(char*, char*, char*, char*); 
+4
source share

The compiler tells you right there ... It is passed as char* . Therefore use either char* or char ar[] .

+4
source share

You can pass an array in two ways:

(1) Regular style C:
Here you go to the address and get using the pointer

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

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

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

This will help you get the size of the array right away (pointer not allowed). This can be made more complex using template .

You can also iterate over the use case of std::string , which will make a copy of the entire array and manage it as an automatic variable.

+3
source share

To fix your code:

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

but it is still wrong. Arrays decay into pointers, so it will compile, but will fail if the arguments do not end with zero. You should also pass size if you choose this approach.

However , since this is C ++, not C, I suggest using std::string instead:

 void putAddress(const std::string&,const std::string&,const std::string&,const std::string&); 
+2
source share
 void putAddress(char[],char[],char[],char[]); function void putAddress(char fName[],char fStreet[],char fSuburb[],char fCountry[]){ return; } 

You forgot to put paranthesis, put them as in the above code.

0
source share

A compiler error, because fromName is really a pointer to an array (the first element) from the arrayName. This is just the syntax of C ++ (and simple C).

To pass a char array to a function, you must do what you are doing now, that is, pass a pointer to the array (the first element).

So all you have to do is change

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

to

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

PS: Your next problem is to know (what makes putAddress aware) about each length of the array. If they are fixed, you have no problem.

0
source share

All Articles