473,769 Members | 6,538 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
15 2681
* 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.now rote:
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<iostre am>

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
address* pjd = &jd;
fill_addr(pjd);
print_addr(pjd) ;

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

}

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-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...@gm ail.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 "declaratio n" and "definition " i changed the:

void print_addr(addr ess*)

to

void print_addr(cons t 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...@gm ail.comwrote:
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 ?
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 "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.
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*********@gm ail.comwrote in message ...
>
OK, i have tried ito understood the "const" phenomenon and this what i
have come with :
in both "declaratio n" and "definition " i changed the:

void print_addr(addr ess*)
to
void print_addr(cons t 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.ne twrote:
arnuld <geek.arn...@gm ail.comwrote in message ...
OK, i have tried ito understood the "const" phenomenon and this what i
have come with :
in both "declaratio n" and "definition " i changed the:
void print_addr(addr ess*)
to
void print_addr(cons t 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
2403
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
14506
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
2745
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
28469
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
40471
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
9589
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
9423
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,...
1
9998
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
9865
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
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
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
3967
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
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.