473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

-------- 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
15 2633
* 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
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
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
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
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
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
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
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
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
* 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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
4
by: Yair | last post by:
Hi, I need to shift the bits of a float type. In order to do so, I declared the following struct: private struct unionIntFloatType { public float m_asFloat;
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
10
by: Pantokrator | last post by:
Hi, Is there any way to "overload" a struct? e.g. having already struct stA1 { int i_ID; int i_Type; };
4
by: Daniel Mark | last post by:
Hello all: I have found a useful module in IPython, named 'from IPython.ipstruct import Struct". So I can use it as follows: #################################### from IPython.ipstruct...
3
by: angshuman.agarwal | last post by:
Structure in C DLL ---------------------------- typedef struct IrData { unsigned short uiFormat; unsigned short uiLength; unsigned char* pchData; } tagIrData; Function in C DLL
8
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
0
by: Keith | last post by:
Hello, I am trying to create exectuables on inux using "pyinstaller". I am using pyinstaller-1.3, RHEL 4.4, Python 2.5. The executables fail to run. The problem returned is pertaining to...
8
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.