What an expression ended by & means ?

Hi all! In line 36 I want to display the value of x|0]. I call for that function takeStruct, but my syntax is incorrect. All that because I don't understand what "myBigStruct&" means. Look at line 12. Here is the code:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*es.cpp*/
#include<iostream>
#include<list>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
struct myBigStruct
{
        int x[100];//big struct with lots of memory
};
void takeStruct (myBigStruct& my_struct)
{
        my_struct.x[0]=23;
        cout <<my_struct.x[0]<<endl;//added
}
int main()
{
        cout << "POINTER TO AN INTEGER\n";
        int m=10;
        cout <<m<<' '<< & m<<endl;//10 0xbf8c1cf4
        int *p_m=& m;
        cout <<p_m<<endl;//0xbf8c1cf4
        *p_m=20;
        cout <<m<<endl;//20
        cout << "REFERENCE\n";
        int x=5;
        //int ref=10;//incorrect
        int &ref=x;
        cout <<ref<<endl;//5
        cout << "MYBIGSTRUCT\n";
        //takeStruct(myBigStruct& x[0]);// expected primary-expression before ‘&’ token
        //takeStruct(int x[O]);// error: expected primary-expression before ‘int’
        //takeStruct(myBigStruct x[O]);//: expected primary-expression before ‘x’
        //takeStruct(my_struct x[O]);//invalid initialization of reference of type ‘myBigStruct&’ from expression of type ‘int’

        /*  
           int k=2;
           const Entry& e = *k;    // reference used as shorthand 

           phone_book.push_back(Entry("DUMAS",111));
           cout << e.name << ' ' << e.number << "\n";


           using namespace std;
           struct Entry {
           string name;
           int number;
           Entry(const string& n, int i) :name(n), number(i) { }
           };
           list<Entry> phone_book;
           void print_entries()
           {
           typedef list<Entry>::const_iterator LI;

           for (int i = phone_book.begin(); i != phone_book.end(); ++i) {
           const Entry& e = *i;    // reference used as shorthand 
           cout << '{' << e.name << ' ' << e.number << "}\n";
           } 
           cout <<endl; 
           }     

           void print_entry(const string& s)
           { 

           for (int i = phone_book.begin(); i != phone_book.end(); ++i) {
           const Entry& e = *i;    // reference used as shorthand 
           if (s == e.name) { 
           cout << e.name << ' ' << e.number << '\n'; 
           return;
           } 
           }
           int main()

           system("clear");
           system("date");
           cout<< "Entrez mot de une ou plusieurs lettres\n";
           cout <<"Si mot de plusieurs lettres, ne prend que la première lettre\n";
           char a_c
           a_char=getchar();
           cout<<a_char<<endl;
           cout<<a_char<<endl;
           int x = 5;
           cout << &x<<endl;//adresse de x
           int &ref=x;
           cout << &ref<<endl;//adresse de ref, même que adresse de x
           cout << ref<<' '<<x<<endl;//affiche 2 fois 5
           cout << "salut\n";
         */
}
                                                                                                                                                                  
                                                                                                                                                                  
See Arguments passed by value and by reference
in the function tutorial:
http://www.cplusplus.com/doc/tutorial/functions/
Also, you don't put the type of an argument in a function call, just the arguments themselves.

Calling functions is absolutely fundamental to C and C++ programming, so I strongly recommend you review your textbook to become clear on how to do it.
To Chervil. This new file works well, but I should like you look at it and tell me if you find something that is not fine. The last line doesn't work. Here is the code:
[CODE]/*es.cpp*/
#include<iostream>
#include<list>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
string str_to_show = "there is one x in this string\n";//global variable
void printString (string& str)
{ cout << str_to_show<<"\n"; }
void duplicate(int& a, int& b)
{
a*=2;
b*=2;
}
string concatenate (string aa, string bb)
{ return aa+bb;}
struct myBigStruct
{
int x[100];//big struct with lots of memory
};
void takeStruct (myBigStruct& my_struct)
{ my_struct.x[0]=23;}
struct Entry {
string name;
int number;
Entry(const string& n, int i) :name(n), number(i) { }
};
list <Entry> phone_book;
int main()
{
cout << "PASSING BY REFERENCE\n";
string str_to_show = "there is one x in this string\n";
printString( str_to_show );
cout << "POINTER TO AN INTEGER\n";
int m=10;
cout <<m<<' '<< & m<<endl;//10 0xbf8c1cf4
int *p_m=& m;
cout <<p_m<<endl;//0xbf8c1cf4
*p_m=20;
cout <<m<<endl;//20dl;
cout << "REFERENCE\n";
int x=5;
int &ref1=x;
cout <<ref1<<endl;//5
string aa="papa";
string &ref=aa;
cout <<ref<<endl;//papa
cout <<"DUPLICATE\n";
int p=1,q=3;
duplicate(p,q);
cout <<"p= "<<p<<" q= "<<q<<endl;//p= 2 q= 6
cout << "CONCATENATION\n";
cout << "enter your first name, then your last name\n";
string first_name,last_name;
cin >>first_name>>last_name;
cout << concatenate(first_name,last_name);//sylvainaubertin
cout << "\nMYBIGSTRUCT\n";
//cout << my_struct.x[0];//not declared
}

[\CODE]
When I compiled that code, it gives a warning message for function printString() :
[Warning] unused parameter 'str' [-Wunused-parameter]



That is important.

Also, on reading the code, there are several global variables:
1
2
string str_to_show = "there is one x in this string\n"; // global variable
list <Entry> phone_book;

The first one has the effect of hiding or disguising the logic error in function printString(). The second isn't used. I'd remove both those lines.

The corrected function should be:
1
2
3
4
void printString (const string& str)
{ 
    cout << str << "\n";
}

Note the use of const, it's a good idea to use this when passing by reference, where the parameter should not be modified by the function.

The last line:
 
//cout << my_struct.x[0];//not declared 

you need to declare an object of the required type:
1
2
    myBigStruct my_struct;
    cout << my_struct.x[0];



The function concatenate() is ok, though you might consider passing the parameters by const reference, this is more efficient:
1
2
3
4
string concatenate (const string& aa, const string& bb) 
{ 
    return aa+bb;
}


One last thing.
When posting your program code, the closing tag [\CODE] should have a forward slash '/', not a backslash '\'.
Last edited on
I thank you very much. Now the file looks better. The only thing strange happens at the last line. it returns the number -1 079 057 422 but maybe this is equal to 23 !
That value is simply 'garbage', the array within the struct is not properly initialised, so it contains whatever value happens to reside in that particular block of memory at the time.

To get the value 23, add the function call:

1
2
3
    myBigStruct my_struct;
    takeStruct(my_struct);
    cout << my_struct.x[0];


Also, you can set the entire array to all zeros like this:
1
2
3
4
struct myBigStruct
{
    int x[100] = { }; // big struct with lots of memory
};
(needs a recent compiler compatible with C++11 or later).
Last edited on
Topic archived. No new replies allowed.