Passing character arrays

Hi, I'm getting an error on complile with the following code. I have tried a few things and it always gives me a similar error. The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

const int MAXLEN = 100;

struct person
{
    char fname[MAXLEN];
    char sname[MAXLEN];
    int age;
    double height;
    double weight;
}nameof, personcpy;


person fillperson(struct person&, const char fname[], const char sname[], int age, double height, double weight);
person makecopy(struct person&, struct person&);
int printperson(struct person);

int main()
{
    struct person fred, fredcpy;
    fillperson(fred, "fred", "murphy", 22, 180.0, 83.2);
    makecopy(fred, fredcpy);
    printperson(fredcpy);

    return 0;
}

person fillperson(struct person &nameof, const char &fname, const char &sname, int age, double height, double weight)
{
    nameof.fname = fname;
    nameof.sname = sname;
    nameof.age = age;
    nameof.height = height;
    nameof.weight = weight;

    return nameof;
}

person makecopy(struct person &nameof, struct person &personcpy)
{
    //Copy the 'person' struct items to 'personcpy'
    strcpy(personcpy.fname,nameof.fname);
    strcpy(personcpy.sname,nameof.sname);
    personcpy.age=nameof.age;
    personcpy.height=nameof.height;
    personcpy.weight=nameof.weight;

    return personcpy;
}

int printperson(struct person &personcpy)
{
    //Should output the copied content to the screen
    cout << "First Name: " <<personcpy.fname<< endl;
    cout << "Surname: " <<personcpy.sname <<endl;
    cout << "Age: " <<personcpy.age << endl;
    cout << "Height: " <<personcpy.height << endl;
    cout << "Weight: " <<personcpy.weight <<endl;
    return 0;
}


The error I get, for both line 34 and 35 is:
error: incompatible types in assignment of ‘const char’ to ‘char [100]’
I presume I am passing the charachter array in to the function incorrectly. I tried
person fillperson(struct person &nameof, const char fname[], const char sname[], int age, double height, double weight
as well, also using pointers, and changing the value inside. I am supposed to do it this way, but I am stumped on this one! If anyone could help it'd be greatly appreciated!

Thanks
const char & is a const reference to a single character I think you want const char * which is a pointer to a const cstring.

You'll also most likely want to use strcpy instead of simple assignment in the fillperson function
Last edited on
Thank you! I had used the pointer method before as you suggested, before I posted on here, but it still gave errors. Your suggestion to use strcpy worked. I wouldn't have got that. That's a new technique to me. So thanks again.
Topic archived. No new replies allowed.