Daniel Del Core
Daniel Del Core

Reputation: 3251

Passing char array into a function

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*.

Upvotes: 12

Views: 81037

Answers (9)

Cratylus
Cratylus

Reputation: 54094

Your function should be:

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

Upvotes: 9

phantasmagoria
phantasmagoria

Reputation: 319

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.

Upvotes: 0

biril
biril

Reputation: 1995

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.

Upvotes: 0

iammilind
iammilind

Reputation: 70094

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.

Upvotes: 9

Luchian Grigore
Luchian Grigore

Reputation: 258688

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&);

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137547

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

Upvotes: 4

EdChum
EdChum

Reputation: 394459

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.

Upvotes: 9

orlp
orlp

Reputation: 118006

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*);

Upvotes: 5

aaronqli
aaronqli

Reputation: 809

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

Upvotes: 4

Related Questions