What is the proper Syntax for this Pointer?

Discussion in 'C Programming' started by Neil, Nov 29, 2007.

  1. Neil

    Neil Guest

    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?
     
    Neil, Nov 29, 2007
    #1
    1. Advertising

  2. Neil

    santosh Guest

    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.
     
    santosh, Nov 29, 2007
    #2
    1. Advertising

  3. 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
     
    Tomás Ó hÉilidhe, Nov 29, 2007
    #3
  4. Neil

    Chris Dollin Guest

    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
     
    Chris Dollin, Nov 29, 2007
    #4
  5. Neil

    James Kuyper Guest

    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.
     
    James Kuyper, Nov 29, 2007
    #5
  6. Neil

    Neil Guest

    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, Nov 30, 2007
    #6
  7. Neil

    Neil Guest

    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.
     
    Neil, Nov 30, 2007
    #7
  8. Neil

    James Kuyper Guest

    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 "->".
     
    James Kuyper, Nov 30, 2007
    #8
  9. Neil <> 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) <>
    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"
     
    Keith Thompson, Nov 30, 2007
    #9
  10. On Thu, 29 Nov 2007 03:42:51 -0500, Neil <>
    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
     
    Barry Schwarz, Dec 1, 2007
    #10
  11. On Fri, 30 Nov 2007 02:53:53 -0500, Neil <>
    wrote:


    snip

    >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


    Unfortunately, also minus some needed stuff.

    >
    >struct adaptorBatteryData
    >{
    > struct batteryData const *battery;
    >};
    >
    >struct adaptorData
    >{
    > struct adaptorBatteryData const *batData;
    >};
    >
    >struct batteryData
    >{
    >
    >};
    >
    >struct adaptorBatteryData const *batPointer;
    >
    >
    >adaptors[position].batteries[channel] =


    You have not told us what type of struct the object adaptors is a
    pointer to or an array of. The underlying structure apparently has a
    member named batteries which does not match any of the three
    structures you listed.

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


    It apparently also has a member named adaptor which also doesn't match
    any of the three structures you listed. But adaptor appears to be an
    array of or a pointer to struct adaptorData since it has a member
    named batData.

    batData is a pointer to struct adaptorBatteryData which does have a
    member named battery. This member is a pointer to struct
    batteryData.

    So the first question: Is the undefined member batteries an object of
    a suitable type so batteries can receive a value of type pointer to
    struct batteryData?

    >Gives the error "structure required on left side of . or *."


    C doesn't have an operator "*.". It does have two operators "*" but
    neither one is applied to structures.

    So the second question: What is the correct text of the error message?
    Use cut and paste; don't retype.

    And the third question: Which of the objects adaptors. or
    adaptor is not a structure?


    Remove del for email
     
    Barry Schwarz, Dec 2, 2007
    #11
  12. Neil

    Neil Guest

    Barry Schwarz wrote:
    > On Thu, 29 Nov 2007 03:42:51 -0500, Neil <>
    > wrote:
    >
    >> What I am doing wrong

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

    The pointer was use to search a linked list, If it fails I need the
    first item as a place holder.
    >> This works

    >
    > How can it work if you get compiler errors?

    The two lines worked fine, When I tried to combine them I got the 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.

    struct x* contains a pointer to a linked list of struct y* (struct x* is
    also a linked list)
    ->a->b->c->d->NULL
    | | | |
    1 1 1 1
    | | |
    2 2 2
    | |
    3 3

    >
    > 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?

    No combine
    >
    >
    >
    > Remove del for email


    The earlier suggestion worked fine. Thanks to all.
    Besides learning the pointer, I learn that I need to include more in a post.
     
    Neil, Dec 5, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joshua Beall

    Proper syntax for background: url();

    Joshua Beall, Apr 7, 2004, in forum: HTML
    Replies:
    3
    Views:
    1,884
    brucie
    Apr 7, 2004
  2. Shea Martin
    Replies:
    2
    Views:
    491
    Gianni Mariani
    Nov 18, 2003
  3. Replies:
    10
    Views:
    740
    Chris Torek
    Feb 4, 2005
  4. Ray Dillinger

    syntax error with pointer to pointer

    Ray Dillinger, Apr 28, 2007, in forum: C Programming
    Replies:
    7
    Views:
    419
    Old Wolf
    Apr 30, 2007
  5. Replies:
    11
    Views:
    482
    Ben Bacarisse
    Feb 9, 2008
Loading...

Share This Page