By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,314 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,314 IT Pros & Developers. It's quick & easy.

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

P: n/a
-------- 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 :-(

Mar 29 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
* 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, "&".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '07 #2

P: n/a
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;
}

Mar 29 '07 #3

P: n/a
On 29 Mar, 11:03, "neurorebel" <neurore...@gmail.comwrote:
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

Mar 29 '07 #4

P: n/a
On Mar 29, 3:31 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
void print_addr(const address p) // print_addr doesn't need to change
the contents of p
good idea :-)

{
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) { ... }
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]$
Mar 29 '07 #5

P: n/a
On Mar 29, 3:03 pm, "neurorebel" <neurore...@gmail.comwrote:
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

Mar 29 '07 #6

P: n/a
On 29 Mar, 11:45, "arnuld" <geek.arn...@gmail.comwrote:
------------ 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

Mar 29 '07 #7

P: n/a
On Mar 29, 4:37 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:

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 ?
Mar 29 '07 #8

P: n/a
On 29 Mar, 13:20, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 4:37 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
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 ?
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

Mar 29 '07 #9

P: n/a
On Mar 29, 5:38 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:

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
:-)

Mar 29 '07 #10

P: n/a
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '07 #11

P: n/a
On Mar 29, 7:23 pm, "Alf P. Steinbach" <a...@start.nowrote:
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.
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]$

Mar 29 '07 #12

P: n/a
On Mar 29, 7:38 pm, "arnuld" <geek.arn...@gmail.comwrote:
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 ?

Mar 29 '07 #13

P: n/a
On 29 Mar, 15:16, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 5:38 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
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 ?
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.

<snip>
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

Mar 29 '07 #14

P: n/a

arnuld <ge*********@gmail.comwrote in message ...
>
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 ?
Alf P. Steinbach's "Pointers" document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

--
Bob R
POVrookie
Mar 30 '07 #15

P: n/a
On Mar 30, 5:38 am, "BobR" <removeBadB...@worldnet.att.netwrote:
arnuld <geek.arn...@gmail.comwrote in message ...
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 ?

Alf P. Steinbach's "Pointers" document:http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf
:-)

Mar 30 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.