BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Return Stucture type in a function - don't know how
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Liz Albin
Guest





PostPosted: Wed Mar 02, 2005 3:05 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote



On Tue, 1 Mar 2005 19:05:59 -0800, Maurice Anderson wrote:

Quote:
Hello,
I have a structure type defined as:

typedef struct APCI_Info{
Word Vendor_ID;
String Vendor_Name;
Word Device_ID;
String Device_Name;
Byte Rev;
Byte Bus;
Byte Dev;
Byte Fun;
Word SMB_Address;
} TPCI_Info;

I want to be able to populate a record of this type and return it as a
variable in a function. Just like you can return an Ansistring variable for
example. Something like:

PCI_Info* __fastcall Scan_PCI(void);

PCI_Info* __fastcall TWinbound::Scan_PCI(void)
{
TPCI_Info *PCI_Structure1 = new TPCI_Info;
PCI_Structure1->Vendor_Name = "Intel";
.
.
PCI_Structure1->SMB_Address = '0x500';
return PCI_Structure1;
}

The only problem is that it doesn't work. I get a compiler error that says
"Declaration syntax error".

Thanks,

-M-

First, you don't need the typedef

this will be fine:

struct TPCI_Info{
Word Vendor_ID;
String Vendor_Name;
Word Device_ID;
String Device_Name;
Byte Rev;
Byte Bus;
Byte Dev;
Byte Fun;
Word SMB_Address;
} ;

Next define your function

TCPI_Info foo()
{
TCPI_Info ATCPI_Info;

// do stuff

return ATCPI_Info;
}


void poo()
{
TCPI_Info Thing = foo();
}
--
Good luck,

liz

Back to top
Maurice Anderson
Guest





PostPosted: Wed Mar 02, 2005 3:05 am    Post subject: Return Stucture type in a function - don't know how Reply with quote



Hello,
I have a structure type defined as:

typedef struct APCI_Info{
Word Vendor_ID;
String Vendor_Name;
Word Device_ID;
String Device_Name;
Byte Rev;
Byte Bus;
Byte Dev;
Byte Fun;
Word SMB_Address;
} TPCI_Info;

I want to be able to populate a record of this type and return it as a
variable in a function. Just like you can return an Ansistring variable for
example. Something like:

PCI_Info* __fastcall Scan_PCI(void);

PCI_Info* __fastcall TWinbound::Scan_PCI(void)
{
TPCI_Info *PCI_Structure1 = new TPCI_Info;
PCI_Structure1->Vendor_Name = "Intel";
..
..
PCI_Structure1->SMB_Address = '0x500';
return PCI_Structure1;
}

The only problem is that it doesn't work. I get a compiler error that says
"Declaration syntax error".

Thanks,

-M-


Back to top
Liz Albin
Guest





PostPosted: Wed Mar 02, 2005 3:06 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote



On Tue, 1 Mar 2005 19:05:59 -0800, Maurice Anderson wrote:

Quote:
PCI_Info* __fastcall Scan_PCI(void);

You haven't defined any type PCI_Info
--
Good luck,

liz

Back to top
Maurice Anderson
Guest





PostPosted: Wed Mar 02, 2005 3:22 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Quote:
First, you don't need the typedef

Thanks, what if I wanted to do it with the typedef though?



Back to top
Liz Albin
Guest





PostPosted: Wed Mar 02, 2005 5:44 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

On Tue, 1 Mar 2005 19:22:39 -0800, Maurice Anderson wrote:

Quote:
Thanks, what if I wanted to do it with the typedef though?

It's pointless, and C rather than C++.
--
Good luck,

liz

Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Wed Mar 02, 2005 3:20 pm    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> writes:

Quote:
First, you don't need the typedef

Thanks, what if I wanted to do it with the typedef though?

There is no benefit, and it's archaic. You'd only use the typedef if
you were coding C. But since you are using the "new" operator, you're
in C++ and don't need to use it.

For what reason would you *want* to use a typedef instead of the
struct's type itself?

--
Chris (TeamB);

Back to top
Ed Mulroy [TeamB]
Guest





PostPosted: Wed Mar 02, 2005 4:57 pm    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Quote:
Thanks, what if I wanted to do it with the typedef though?

Overview of why that would work but you really do not want to do it.

In old-timey C (before the current official language level), there was
a problem with structure type names and variable names sharing name
tables (the tables of names in the source code built up by the
compiler as it parsed and translated your program). Because of that
you needed to tag all structure names with the keyword 'struct' when
declaring or casting. The keyword 'typedef' was a way to bypass that
requirement. It caused the name table entry to be unique and handled
as a type.

From almost its beginning C++ was defined as performing the same act
on a name when the name was a structure type as if a typedef had been
used. In C++ these two are functionally identical:

------------------
typedef struct name_1
{
---list of contents---
} name_2;

------------------

and


------------------
struct name_1
{
---list of contents---
};

typedef name_1 name_2;
------------------

Yes you can still say 'typedef'. However it has no function other
than to imply that you, the author of the code, have not yet
progressed into the level of C++ which prevailed in 1989. Surely you
would like your code to reflect current expertise and knowledge and
not that of 16 years ago.

.. Ed

Quote:
Maurice Anderson wrote in message
news:42252fcf$1 (AT) newsgroups (DOT) borland.com...

First, you don't need the typedef

Thanks, what if I wanted to do it with the typedef though?



Back to top
Alisdair Meredith [TeamB]
Guest





PostPosted: Wed Mar 02, 2005 6:09 pm    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Ed Mulroy [TeamB] wrote:

Quote:
From almost its beginning C++ was defined as performing the same act
on a name when the name was a structure type as if a typedef had been
used. In C++ these two are functionally identical:

------------------
typedef struct name_1
{
---list of contents---
} name_2;

------------------

and


------------------
struct name_1
{
---list of contents---
};

typedef name_1 name_2;
------------------

Actually I believe there really is a slight difference between these
two, that means the second should always be preferred.

The first syntax declares an anonymous struct type, and creates a
typedef alias for that type. However, this is only an alias, and not
the true name of the type.

The second declares a type called name_1, and an alias for it called
name_2.

The difference rarely matters, but (for example) shows up where a
type-name must be deduced, such as in function templates. In these
cases, you really do want the second form, as it is far simpler dealing
with named types when you start trying to debug ;?)

AlisdairM(TeamB)

Back to top
Ed Mulroy [TeamB]
Guest





PostPosted: Wed Mar 02, 2005 10:31 pm    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

I am confused.

Quote:
The first syntax declares an anonymous struct type, and creates a
typedef alias for that type. However, this is only an alias, and
not ...

typedef struct name_1
{
---list of contents---
} name_2;

I do not understand how this. Where is the anonymous structure
located, how did come to be an anonymous structure and what is an
anonymous structure in this context actually?

Quote:
The difference rarely matters, but (for example) shows up
where a type-name must be deduced, such as in function
templates. ...

If I have

template <class T> --the rest goes here---

and it is used with <name_1> or is used with <name_2> or called where
the calling parameter is type T in the function/class header and
name_1 or name_2 in the code where is the problem in deducing?

.. Ed

Quote:
Alisdair Meredith wrote in message
news:42260160 (AT) newsgroups (DOT) borland.com...

From almost its beginning C++ was defined as performing the same
act
on a name when the name was a structure type as if a typedef had
been
used. In C++ these two are functionally identical:

------------------
typedef struct name_1
{
---list of contents---
} name_2;

------------------

and


------------------
struct name_1
{
---list of contents---
};

typedef name_1 name_2;
------------------

Actually I believe there really is a slight difference between these
two, that means the second should always be preferred.

The first syntax declares an anonymous struct type, and creates a
typedef alias for that type. However, this is only an alias, and
not
the true name of the type.

The second declares a type called name_1, and an alias for it called
name_2.

The difference rarely matters, but (for example) shows up where a
type-name must be deduced, such as in function templates. In these
cases, you really do want the second form, as it is far simpler
dealing
with named types when you start trying to debug ;?)



Back to top
Maurice Anderson
Guest





PostPosted: Thu Mar 03, 2005 5:14 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Ok I did this:

---H file:
struct TPCI_Info{
Word Vendor_ID;
String Vendor_Name;
Word Device_ID;
String Device_Name;
Byte Rev;
Byte Bus;
Byte Dev;
Byte Fun;
Word SMB_Address;
} ;
TPCI_Info __fastcall Scan_PCI(void);

---CPP File:
TPCI_Info __fastcall TWinbound::Scan_PCI(void)
{
int Bus, Dev, Fun;
TPCI_Info Info;// = new TPCI_Info;
unsigned long Data;
..
return Info;
}

But I still get a Declaration syntax error...help.


Back to top
Alisdair Meredith
Guest





PostPosted: Thu Mar 03, 2005 6:54 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Ed Mulroy [TeamB] wrote:

Quote:
I am confused.

typedef struct name_1
{
---list of contents---
} name_2;

I do not understand how this. Where is the anonymous structure
located, how did come to be an anonymous structure and what is an
anonymous structure in this context actually?

No, you are not confused, you are in fact correct!

Sorry, my mistake, I was looking at name_2 and forgot about name_1,
which is indeed the name for this struct type.

AlisdairM(TeamB)

Back to top
Liz Albin
Guest





PostPosted: Thu Mar 03, 2005 7:33 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

On Wed, 2 Mar 2005 21:14:01 -0800, Maurice Anderson wrote:

Quote:
But I still get a Declaration syntax error...help.

where is the error? and why are you using the (void) ?
--
Good luck,

liz

Back to top
Ed Mulroy [TeamB]
Guest





PostPosted: Thu Mar 03, 2005 12:16 pm    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Quote:
I am confused ... I do not understand ...
No, you are not confused, you are in fact correct! ...

Thank goodness for that. I was beginning to fear that I'd missed an
underlying principle and now needed to go back to the very basics.

.. Ed

Quote:
Alisdair Meredith wrote in message
news:4226b48a$1 (AT) newsgroups (DOT) borland.com...

I am confused.

typedef struct name_1
{
---list of contents---
} name_2;

I do not understand how this. Where is the anonymous structure
located, how did come to be an anonymous structure and what is an
anonymous structure in this context actually?

No, you are not confused, you are in fact correct!

Sorry, my mistake, I was looking at name_2 and forgot about name_1,
which is indeed the name for this struct type.



Back to top
Maurice Anderson
Guest





PostPosted: Tue Mar 08, 2005 5:43 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Quote:
where is the error?
It says Declaration syntax" and it highlights this line:


TPCI_Info __fastcall TWinbound::Scan_PCI(void)

Quote:
and why are you using the (void) ?

I use the void because I am not passing any arguments to the function. Any
ideas?

-M-



Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Tue Mar 08, 2005 9:07 am    Post subject: Re: Return Stucture type in a function - don't know how Reply with quote

Maurice Anderson wrote:

Quote:
and why are you using the (void) ?

I use the void because I am not passing any arguments to the
function. Any ideas?

You don't need it.

int wibble();

Is fine.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.