Thread: strange * on structure

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    strange * on structure

    ethdr = (ETHERNET_FRAME*)&tmp[0];

    I thought pointer (whether declearing or referencing it), * should be on left side of the word.

    One of the code I was studying, I saw code like above where * shows up on right side of struct.

    Is this not a pointer ? what is this?
    Can someone explain please?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's a "cast". It means the variable is to be converted to this type -- in this case, an ETHERNET_FRAME pointer. This makes sense, since &tmp[0] would be the address of (&) some variable.

    And as we know, pointers store the address of some variable of a particular type. Although void pointers can be used to refer to ANY type.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Hi, thanks for replying on this issue.

    I guess I have a problem understanding the syntax of it.
    Wouldn't Ethernet_Frame pointer be --> *Ethernet_Frame ?

    but instead, I have Ethernet_Frame* ........ ??

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it shouldn't really matter where it goes, it's correct either way. HOWEVER, logically, IMO it should go with the type rather than the variable because the symbol in that case is identifying type.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by convenientstore View Post
    Hi, thanks for replying on this issue.

    I guess I have a problem understanding the syntax of it.
    Wouldn't Ethernet_Frame pointer be --> *Ethernet_Frame ?
    Not in the slightest. That would be multiplying by Ethernet_Frame (if Ethernet_Frame was a variable and not a type), or dereferencing Ethernet_Frame (ditto). Even when you declare the variable, that's what you use:
    Code:
    Ethernet_Frame* this_is_a_pointer_variable;

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by convenientstore View Post
    Hi, thanks for replying on this issue.

    I guess I have a problem understanding the syntax of it.
    Wouldn't Ethernet_Frame pointer be --> *Ethernet_Frame ?

    but instead, I have Ethernet_Frame* ........ ??
    When you declare a pointer you do
    Code:
    type * name; //int * myInt
    So when you want to declare that a variable is a pointer you put the type first and the symbol * after that.
    ETHERNET_FRAME is a type, not a variable...

    EDIT: Oops too slow

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by C_ntua View Post
    When you declare a pointer you do
    Code:
    type * name; //int * myInt
    So when you want to declare that a variable is a pointer you put the type first and the symbol * after that.
    ETHERNET_FRAME is a type, not a variable...

    EDIT: Oops too slow
    Nevermind, you gave a very clear description that I'm sure hammers the point home.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Talking of hammers, think of it this way

    RoundHole = (hammer*)SquarePeg;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Strange structure array issue...
    By IndioDoido in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 06:29 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM