473,758 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#include<vector >

struct address;
void fill_addr(addre ss); // assigns values to a struct of type
"address"
void print_addr(addr ess*); // 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(addre ss* jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerlan d";

return jd;
}

void print_addr(addr ess* 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(addr ess*)'
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(addre ss*)':
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 2679
* 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(addre ss& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerlan d";
}
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(cons t 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...@gma il.comwrote:
void print_addr(cons t 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(cons t address& p) { ... }

Gavin Deane

Mar 29 '07 #4
On Mar 29, 3:31 pm, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
void print_addr(cons t 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(cons t 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<iostre am>
#include<vector >

struct address;
void fill_addr(addre ss&); // assigns values to a struct of type
"address"
void print_addr(cons t 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_j d);
print_addr(ref_ jd);

return 0;
}
void fill_addr(addre ss& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerlan d";

}

void print_addr(cons t 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...@gma il.comwrote:
I guess your fill_addr function should be something like this

void fill_addr(addre ss& jd)
{
jd.name = "Niklaus Wirth";
jd.country = "Switzerlan d";

}

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(cons t 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...@gm ail.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<iostre am>
#include<vector >

struct address;
void fill_addr(addre ss&); // assigns values to a struct of type
"address"
void print_addr(cons t 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_j d);
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...@ho tmail.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...@gm ail.comwrote:
On Mar 29, 4:37 pm, "Gavin Deane" <deane_ga...@ho tmail.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_j d); // <<-- 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...@ho tmail.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_j d); // <<-- 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 "meaningles s" 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2401
by: XiongBin | last post by:
anybody who tell me: what is the difference between "struct" and "class"? :-)
4
5984
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
14505
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 program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
10
2743
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
4410
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 import Struct
3
4357
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
28468
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
40470
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
0
1709
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 "struct.py" not being able to find the module "_struct". struct.py is located under /usr/local/lib/python-2.5/, and there is a _struct.o (no _struct.py anywhere) located under /usr/local/lib/ python-2.5/lib-dynload.
8
2170
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 (where there is an
0
9492
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9299
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9908
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9740
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3832
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2702
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.