Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   What is the proper Syntax for this Pointer? (http://www.velocityreviews.com/forums/t555950-what-is-the-proper-syntax-for-this-pointer.html)

Neil 11-29-2007 08:42 AM

What is the proper Syntax for this Pointer?
 
What I am doing wrong

This works

batPointer = adaptors[position].adaptor[channel]->batData;
adaptors[position].batteries[channel] = batPointer->battery;

where:
batData is a pointer to a struct
batPointer is a pointer to a different kind of struct

I want one line, but I get compiler errors, what is the correct syntax?

santosh 11-29-2007 08:51 AM

Re: What is the proper Syntax for this Pointer?
 
Neil wrote:

> What I am doing wrong
>
> This works
>
> batPointer = adaptors[position].adaptor[channel]->batData;
> adaptors[position].batteries[channel] = batPointer->battery;
>
> where:
> batData is a pointer to a struct
> batPointer is a pointer to a different kind of struct
>
> I want one line, but I get compiler errors, what is the correct
> syntax?


Please post a complete, minimal program that exhibits the concerned
error. Without precise definitions
of 'batPointer', 'adaptors', 'position', 'channel', 'batData',
and 'battery', we cannot say what the problem could be.


Tomás Ó hÉilidhe 11-29-2007 09:21 AM

Re: What is the proper Syntax for this Pointer?
 
Neil:


> batPointer = adaptors[position].adaptor[channel]->batData;
> adaptors[position].batteries[channel] = batPointer->battery;
>
> I want one line, but I get compiler errors, what is the correct syntax?



When faced with this situation, there's a pretty simple routine:

Take your mouse, go to the first line and highlight what gets assigned to
batPointer, hit Ctrl+C.

Go to the second line, put parentheses around batPointer, highlight
batPointer, and hit Ctrl+V.

The parentheses are needed in order to ensure that operator precedence
doesn't get messed up. In your example though, there's no need for the
parentheses.

adaptors[position].batteries[channel] = adaptors[position].adaptor
[channel]->batData->battery;

--
Tomás Ó hÉilidhe

Chris Dollin 11-29-2007 10:24 AM

Re: What is the proper Syntax for this Pointer?
 
Neil wrote:

> What I am doing wrong
>
> This works
>
> batPointer = adaptors[position].adaptor[channel]->batData;
> adaptors[position].batteries[channel] = batPointer->battery;
>
> where:
> batData is a pointer to a struct
> batPointer is a pointer to a different kind of struct
>
> I want one line, but I get compiler errors, what is the correct syntax?


What was your one line, what were the declarations of the variables, and
what were the compiler errors?

[I'd also be likely to introduce a name for `adaptors[position]`, since it's
duplicated. And without seeing the rest of your code, I can't tell if your
assignment above should obviously be extracted into a well-named function
of its own.]

--
Chris "in some contexts, context is everything" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


James Kuyper 11-29-2007 12:39 PM

Re: What is the proper Syntax for this Pointer?
 
Neil wrote:
> What I am doing wrong
>
> This works
>
> batPointer = adaptors[position].adaptor[channel]->batData;
> adaptors[position].batteries[channel] = batPointer->battery;
>
> where:
> batData is a pointer to a struct
> batPointer is a pointer to a different kind of struct


If that's the case, and this is your actual code, you should be getting
a diagnostic message; there is no implicit conversion between pointers
to different kinds of struct; you should have to use an explicit conversion.

Why are you storing a pointer to one kind of struct into a pointer to a
different kind of struct? Why do you expect to get any useful results
from doing so? There are some special circumstances where it will work,
but it's not clear to me that you know what they are, and it's not
possible for me to figure out from the information you've given us
whether this is one of those special circumstances.

> I want one line, but I get compiler errors, what is the correct syntax?


If you get compiler errors when you make this one, you should tell use
what that one line is, and what the error messages are.

Neil 11-30-2007 07:53 AM

Re: What is the proper Syntax for this Pointer?
 
James Kuyper wrote:
> Neil wrote:
>> What I am doing wrong
>>
>> This works
>>
>> batPointer = adaptors[position].adaptor[channel]->batData;
>> adaptors[position].batteries[channel] = batPointer->battery;
>> where:
>> batData is a pointer to a struct
>> batPointer is a pointer to a different kind of struct

>
> If that's the case, and this is your actual code, you should be getting
> a diagnostic message; there is no implicit conversion between pointers
> to different kinds of struct; you should have to use an explicit
> conversion.
>
> Why are you storing a pointer to one kind of struct into a pointer to a
> different kind of struct? Why do you expect to get any useful results
> from doing so? There are some special circumstances where it will work,
> but it's not clear to me that you know what they are, and it's not
> possible for me to figure out from the information you've given us
> whether this is one of those special circumstances.
>
>> I want one line, but I get compiler errors, what is the correct syntax?

>
> If you get compiler errors when you make this one, you should tell use
> what that one line is, and what the error messages are.


Yes it works. The structures are elaborate ( Not mine ) but they do
want the are supposed to. Same the adapter, and the battery from the
linked list.

The structs ( minus the uneeded stuff)are

struct adaptorBatteryData
{
struct batteryData const *battery;
};

struct adaptorData
{
struct adaptorBatteryData const *batData;
};

struct batteryData
{

};

struct adaptorBatteryData const *batPointer;


adaptors[position].batteries[channel] =
adaptors[position].adaptor[channel].batData->battery;
Gives the error "structure required on left side of . or *."


Neil 11-30-2007 07:56 AM

Re: What is the proper Syntax for this Pointer?
 
Tomás Ó hÉilidhe wrote:
> Neil:
>
>
>> batPointer = adaptors[position].adaptor[channel]->batData;
>> adaptors[position].batteries[channel] = batPointer->battery;
>>
>> I want one line, but I get compiler errors, what is the correct syntax?

>
>
> When faced with this situation, there's a pretty simple routine:
>
> Take your mouse, go to the first line and highlight what gets assigned to
> batPointer, hit Ctrl+C.
>
> Go to the second line, put parentheses around batPointer, highlight
> batPointer, and hit Ctrl+V.
>
> The parentheses are needed in order to ensure that operator precedence
> doesn't get messed up. In your example though, there's no need for the
> parentheses.
>
> adaptors[position].batteries[channel] = adaptors[position].adaptor
> [channel]->batData->battery;
>

I thought that, but then though it wrong. Thanks for the answer. And
the tip.

I hated to leave the two line solution.

James Kuyper 11-30-2007 01:06 PM

Re: What is the proper Syntax for this Pointer?
 
Neil wrote:
....
> The structs ( minus the uneeded stuff)are
>
> struct adaptorBatteryData
> {
> struct batteryData const *battery;
> };
>
> struct adaptorData
> {
> struct adaptorBatteryData const *batData;
> };
>
> struct batteryData
> {
>
> };
>
> struct adaptorBatteryData const *batPointer;
>
>
> adaptors[position].batteries[channel] =
> adaptors[position].adaptor[channel].batData->battery;
> Gives the error "structure required on left side of . or *."


What is missing from what you just gave us is the data type for
adaptors[position] and adaptors[position].adaptor[channel]. As written,
your code requires that they both be structures. The message that was
generated implies that at least one of those expressions is a pointer to
a structure. If that is the case, then the your problem is due to the
fact that you've used "." rather than "->".

Keith Thompson 11-30-2007 07:24 PM

Re: What is the proper Syntax for this Pointer?
 
Neil <NeilKurzm@worldnet.att.net> writes:
> Tomás Ó hÉilidhe wrote:
>> Neil:
>>> batPointer = adaptors[position].adaptor[channel]->batData;
>>> adaptors[position].batteries[channel] = batPointer->battery;
>>>
>>> I want one line, but I get compiler errors, what is the correct syntax?

>>

[...]
>>
>> adaptors[position].batteries[channel] = adaptors[position].adaptor
>> [channel]->batData->battery;
>>

> I thought that, but then though it wrong. Thanks for the answer. And
> the tip.
>
> I hated to leave the two line solution.


Why? Is there some virtue in putting as much stuff as possible on one
line?

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Barry Schwarz 12-01-2007 05:15 AM

Re: What is the proper Syntax for this Pointer?
 
On Thu, 29 Nov 2007 03:42:51 -0500, Neil <NeilKurzm@worldnet.att.net>
wrote:

>What I am doing wrong


What is your end objective? What are really trying to accomplish?
>
>This works


How can it work if you get compiler errors?

>
> batPointer = adaptors[position].adaptor[channel]->batData;
> adaptors[position].batteries[channel] = batPointer->battery;
>
>where:
>batData is a pointer to a struct
>batPointer is a pointer to a different kind of struct


If batPointer is of type struct x* and batData is of type struct y*,
they are incompatible and there is no implicit conversion between
them. The only way to assign a value of one type to an object of
incompatible type is with a cast. However, if the converted pointer
value does not have the proper alignment you have invoked undefined
behavior.

Why do you want a pointer to some object to hold the address of an
object of different type?

>
>I want one line, but I get compiler errors, what is the correct syntax?


One line of what? You have two independent assignment statements. Do
you intend to eliminate one?



Remove del for email


All times are GMT. The time now is 12:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.