problem with accessing a "struct" using "->"

A

arnuld

-------- PROGRAMME -----------
/* Stroustrup, 5.6 Structures

STATEMENT:
this programmes *tries* to do do this in 3 parts:

1.) it creates a "struct", named "jd", of type "address".
2. it then adds values to "jd"
3.) in the end it prints values of "jd".

*/

#include<iostream>
#include<vector>

struct address;
void fill_addr(address); // assigns values to a struct of type
"address"
void print_addr(address*); // prints an address struct

struct address {
char* name;
char* country;
};



int main()
{
address jd; // an "address" struct
fill_addr(jd);
print_addr(jd);

return 0;
}


struct* fill_addr(address* jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerland";

return jd;
}

void print_addr(address* p)
{
using std::cout;
using std::endl;
cout << p->name
<< '\n'
<< p->country
<< endl;
}

----------- OUTPUT --------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
5.6_structures.cpp
5.6_structures.cpp: In function 'int main()':
5.6_structures.cpp:30: error: cannot convert 'address' to 'address*'
for argument '1' to 'void print_addr(address*)'
5.6_structures.cpp: At global scope:
5.6_structures.cpp:36: error: expected identifier before '*' token
5.6_structures.cpp: In function 'int* fill_addr(address*)':
5.6_structures.cpp:38: error: request for member 'name' in 'jd', which
is of non-class type 'address*'
5.6_structures.cpp:39: error: request for member 'country' in 'jd',
which is of non-class type 'address*'
5.6_structures.cpp:41: error: cannot convert 'address*' to 'int*' in
return
[arch@voodo tc++pl]$


i know the error at "line 30" means but i am not able to correct it. i
tried with different ways of using "address" and "address*" but it
does not work :-(
 
A

Alf P. Steinbach

* arnuld:
i know the error at "line 30" means but i am not able to correct it. i
tried with different ways of using "address" and "address*" but it
does not work :-(

Try the address operator, "&".
 
N

neurorebel

I guess your fill_addr function should be something like this

void fill_addr(address& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerland";
}


Prefer reference parameters when you want to modify a variable inside
a function. And by the way you must initialize the strings in your
struct. I recommend you to use c++ strings and your struct would look
better this way

struct address {
string name;
string country;

};

And let's change the print_addr function to be consistent with the
aboce modifications

void print_addr(const address p) // print_addr doesn't need to change
the contents of p
{
using std::cout;
using std::endl;
cout << p.name
<< '\n'
<< p.country
<< endl;
}
 
G

Gavin Deane

void print_addr(const address p) // print_addr doesn't need to change
the contents of p
{
using std::cout;
using std::endl;
cout << p.name
<< '\n'
<< p.country
<< endl;
}

Maybe it's what you meant, but if you go this way I see no reason why
print_addr shouldn't take a const reference
void print_addr(const address& p) { ... }

Gavin Deane
 
A

arnuld

good idea :)

Maybe it's what you meant, but if you go this way I see no reason why
print_addr shouldn't take a const reference
void print_addr(const address& p) { ... }

it takes a /const reference/ , no problem.

my programme works now, fine,no troubles. THANKS:

------------ PROGRAMME -------------
/* Stroustrup, 5.6 Structures

STATEMENT:
this programmes has 3 parts:

1.) it creates a "struct", named "jd", of type "address".
2. it then adds values to "jd"
3.) in the end it prints values of "jd".

*/

#include<iostream>
#include<vector>

struct address;
void fill_addr(address&); // assigns values to a struct of type
"address"
void print_addr(const address&); // prints an address struct

struct address {
char* name;
char* country;
};



int main()
{
address jd; // an "address" struct
address& ref_jd = jd;
fill_addr(ref_jd);
print_addr(ref_jd);

return 0;
}


void fill_addr(address& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerland";

}

void print_addr(const address& p)
{
using std::cout;
using std::endl;
cout << p.name
<< '\n'
<< p.country
<< endl;
}

-------- OUTPUT -------------

[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
5.6_structures.cpp
[arch@voodo tc++pl]$ ./a.out
Niklaus Wirth
Switzerland
[arch@voodo tc++pl]$
 
A

arnuld

I guess your fill_addr function should be something like this

void fill_addr(address& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerland";

}

Prefer reference parameters when you want to modify a variable inside
a function. And by the way you must initialize the strings in your
struct. I recommend you to use c++ strings and your struct would look
better this way

struct address {
string name;
string country;

};

And let's change the print_addr function to be consistent with the
aboce modifications

void print_addr(const address p) // print_addr doesn't need to change
the contents of p
{
using std::cout;
using std::endl;
cout << p.name
<< '\n'
<< p.country
<< endl;

}

thanks, it works
 
G

Gavin Deane

------------ PROGRAMME -------------
/* Stroustrup, 5.6 Structures

STATEMENT:
this programmes has 3 parts:

1.) it creates a "struct", named "jd", of type "address".
2. it then adds values to "jd"
3.) in the end it prints values of "jd".

*/

#include<iostream>
#include<vector>

struct address;
void fill_addr(address&); // assigns values to a struct of type
"address"
void print_addr(const address&); // prints an address struct

struct address {
char* name;
char* country;

};

int main()
{
address jd; // an "address" struct
address& ref_jd = jd;
fill_addr(ref_jd);
print_addr(ref_jd);

return 0;

}

Glad you got it working. You might know this, but note that there is
no need for ref_jd in your main function. This variation has identical
effect:

int main()
{
address jd;
fill_addr(jd);
print_addr(jd);

return 0;
}

Gavin Deane
 
A

arnuld

Glad you got it working.
:)

You might know this, but note that there is
no need for ref_jd in your main function. This variation has identical
effect:

i did not know this.
int main()
{
address jd;
fill_addr(jd);
print_addr(jd);

return 0;

}

yes, it runs.

Gavin, it means, it is an "implicit conversion" to a "reference".
isn't using "explicit references" a good coding practice ?
 
G

Gavin Deane

i did not know this.




yes, it runs.

Gavin, it means, it is an "implicit conversion" to a "reference".
isn't using "explicit references" a good coding practice ?

There is no conversion. References are not objects in their own right.
A reference simply gives you an alias for an existing object. Whenever
you use the name of the reference in code, it is as if you had used
the name of the object referred to. So when you do fill_addr(jd) in
main, the reference-to-address parameter of the fill_addr function is
initialised to refer to the object jd.

In your original code you had this in your main function

address jd; // an "address" struct
address& ref_jd = jd; // <<-- LINE 2
fill_addr(ref_jd); // <<-- LINE 3

On the line I have marked as Line 2, ref_jd is initialised to refer to
the object jd. From then on, wherever you write ref_jd, it is as if
you had written jd. So on the line I have marked as Line 3, what is
the reference-to-address parameter of the fill_addr function
initialised to refer to now? It doesn't refer to ref_jd. That's
meaningless. ref_jd isn't an object - it isn't a thing that can be
referred to. ref_jd is simply an alias for jd. There is no such thing
as a reference to a reference. A reference has to refer to some
object, and the object in question here is jd. So the reference-to-
address parameter of the fill_addr function is initialised to refer to
the object jd. Exactly as before.

ref_jd is entirely redundant and serves only to clutter your code and
potentially confuse a reader.

Gavin Deane
 
A

arnuld

There is no conversion. References are not objects in their own right.
A reference simply gives you an alias for an existing object. Whenever
you use the name of the reference in code, it is as if you had used
the name of the object referred to. So when you do fill_addr(jd) in
main, the reference-to-address parameter of the fill_addr function is
initialised to refer to the object jd.

it is confusing to me *What* exactly a reference is ? it is not an
object (means an area of memory) but it still exists. where does it
exist ?

[FAQ]
i see the FAQ has an article on it: http://www.parashift.com/c++-faq-lite/references.html

it says:

"A reference is the object. It is not a pointer to the object, nor a
copy of the object. It is the object."

if it is the object then why does it takes the address at one time and
increments the value some other time:

swap(int& i, int& j)

gets the address but

i++, increments the value. i am not a C programmer but i did 1st
chapter of K&R2 where i can see that to get address we use "*i" and to
increment value we use "(*i)++".

[/FAQ]

does it mean "references" are implemented as "pointers" in the core
language ?

In your original code you had this in your main function

address jd; // an "address" struct
address& ref_jd = jd; // <<-- LINE 2
fill_addr(ref_jd); // <<-- LINE 3

On the line I have marked as Line 2, ref_jd is initialised to refer to
the object jd. From then on, wherever you write ref_jd, it is as if
you had written jd. So on the line I have marked as Line 3, what is
the reference-to-address parameter of the fill_addr function
initialised to refer to now?

it will refer to "jd", the original object.

It doesn't refer to ref_jd.
yes.



That's meaningless.

i dont get it. i am not saying that it is "meaningless" i am saying,
it is OK if it is meaningless and is this how we use References. if
yes, then it means we will never use pointers in C++, because
References fulfill the job of taking address and dereference
automatically.

ref_jd isn't an object - it isn't a thing that can be
referred to. ref_jd is simply an alias for jd. There is no such thing
as a reference to a reference. A reference has to refer to some
object, and the object in question here is jd. So the reference-to-
address parameter of the fill_addr function is initialised to refer to
the object jd. Exactly as before.

out of my head :-(
ref_jd is entirely redundant and serves only to clutter your code and
potentially confuse a reader.

that i understand pretty well, i will never do that again :)
Gavin Deane

:)
 
A

Alf P. Steinbach

* arnuld:
does it mean "references" are implemented as "pointers" in the core
language ?

It means references are /probably/ implemented as pointers (memory
addresses), but you have no way of knowing other than looking at the
generated machine code.

At the C++ level a reference is simply a synonym.

By the way, I think you should write a new version of your "fill in
address" program, this time using pointer arguments. It's better to use
reference arguments for this program, yes without a doubt. But it's
also a Good Idea(TM) to master the basics of pointers.
 
A

arnuld

It means references are /probably/ implemented as pointers (memory
addresses), but you have no way of knowing other than looking at the
generated machine code.

probably, :-(

and no way except of looking at machine code :-(

At the C++ level a reference is simply a synonym.

that is the "synonym" that confuses me.

By the way, I think you should write a new version of your "fill in
address" program, this time using pointer arguments. It's better to use
reference arguments for this program, yes without a doubt. But it's
also a Good Idea(TM) to master the basics of pointers.

Joel agrees with you, completely [1].

BTW, here is my version using Pointers, it runs fine. see if you have
any advice for improvement (except of using "const pointer" which i
never understood).

even then i feel, till yet, "Pointers" are easier to understand than
using "References".

-------------- PROGRAMME ----------
#include<iostream>

struct address;
void fill_addr(address*); // assigns values to a struct of type
"address"
void print_addr(address*); // prints an address struct

struct address {
char* name;
char* country;
};



int main()
{
address jd; // an "address" struct
address* pjd = &jd;
fill_addr(pjd);
print_addr(pjd);

return 0;
}


void fill_addr(address* jd)
{
jd->name = "Niklaus Wirth";
jd->country = "Switzerland";

}

void print_addr(address* p)
{
using std::cout;
using std::endl;
cout << p->name
<< '\n'
<< p->country
<< endl;
}

------------ OUTPUT ------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
5.6_structures-2.cpp
[arch@voodo tc++pl]$ ./a.out
Niklaus Wirth
Switzerland
[arch@voodo tc++pl]$
 
A

arnuld

BTW, here is my version using Pointers, it runs fine. see if you have
any advice for improvement (except of using "const pointer" which i
never understood).

OK, i have tried ito understood the "const" phenomenon and this what i
have come with :

in both "declaration" and "definition" i changed the:

void print_addr(address*)

to

void print_addr(const address*)

this way, we are not allowed to modify the original values the pointer
is pointing to.

right ?
 
G

Gavin Deane

it is confusing to me *What* exactly a reference is ? it is not an
object (means an area of memory) but it still exists. where does it
exist ?

It is a syntax concept. It exists in the code that appears on your
screen in front of your eyes.
[FAQ]
i see the FAQ has an article on it:http://www.parashift.com/c++-faq-lite/references.html

it says:

"A reference is the object. It is not a pointer to the object, nor a
copy of the object. It is the object."

if it is the object then why does it takes the address at one time and
increments the value some other time:

swap(int& i, int& j)

gets the address but

i++, increments the value. i am not a C programmer but i did 1st
chapter of K&R2 where i can see that to get address we use "*i" and to
increment value we use "(*i)++".

[/FAQ]

I'm not sure what you mean here. I don't see how addresses come into
it. You understand that the & symbol has a two completely unrelated
meanings, right?

It can be used to declare a reference:
int i;
int& ri;

and it is also the address-of operator used to, well, take the address
of an object:
int i;
int* pi = &i;

does it mean "references" are implemented as "pointers" in the core
language ?

They might well be. But that's a detail important only to the people
who write the compiler.

i dont get it. i am not saying that it is "meaningless" i am saying,
it is OK if it is meaningless and is this how we use References. if
yes, then it means we will never use pointers in C++, because
References fulfill the job of taking address and dereference
automatically.

In C++, references can be used as function parameters in many of the
situations where you would use a pointer in C. But references are not
a fully equivalent replacement for pointers in function parameter
lists. Pointers can do two things references can't - they can be
changed to point to a different object (assuming they are non-const
pointers) and they can point to nothing - the null pointer. There is
no such thing as a "null reference". So, if it makes sense to
sometimes pass your function a valid object and sometimes pass it
nothing, you can't use a reference as the parameter - you have to use
a pointer (and check inside the function whether the pointer is
null).

That is the basis of "use references when you can and pointers only
when you have to", explained in FAQ 8.6 in the link you posted.

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top