Thread: * Operator in special case

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    * Operator in special case

    Hello,

    i have a question about the "*" Operator.
    I know that this sign has a meaning in the context of pointers. Like this:
    int *number;

    But now i saw something like:

    void addEnd(int num)
    {
    struct Node *temp1, *temp2;

    temp1=(struct Node *)malloc(sizeof(struct Node));
    ....
    ....

    what does this mean??
    (struct Node *)..

    Another example:
    "new=(person*)malloc(sizeof.....) whereas person is a structure.

    What does it mean, if i put a * after a structure?

    Thank you in advance
    Regards
    Last edited by HungryMan; 05-07-2011 at 02:44 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's called casting.
    return value of malloc(void*) is cast to type (struct Node*).
    Which is considered poor practice.
    Question 7.7b

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by HungryMan View Post
    what does this mean??
    (struct Node *)..
    That is a typecast ... a way to make a value appear as a different type.
    As Bayint explained malloc() returns a void* (pointer to nothing) which in your exmple is beaing typecast to a pointer to a Node struct (struct Node*)

    Another example of typecasts are to make a char appear as an int ..
    Code:
    int x;
    char y;
    
    y= 12;
    
    x =  (int) y;
    Mostly C handles this stuff internally but when you are using typedef to create your own variable types C needs a little help so you use typecasts. There should be some pretty good information on this in any c texbook or tutorial... so a little googling and reading would be a good idea.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bayint Naung View Post
    It's called casting.
    return value of malloc(void*) is cast to type (struct Node*).
    Which is considered poor practice.
    Question 7.7b
    Bayint mean WRT to malloc, and not casting in general.

    Whatever it is considered, casting malloc is also a fairly common practice since it is a major factor in getting C source files to compile alongside C++.
    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

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    One way to look at casting is that whenever you cast something you are telling the compiler you are wiser than it in representing the data. 99% of the times, this is not true.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by HungryMan View Post
    What does it mean, if i put a * after a structure?
    The same thing it means when you put it after int. I.e. that you want some piece of memory to be treated as a pointer to certain type.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Special characters
    By nowber in forum C Programming
    Replies: 2
    Last Post: 10-25-2009, 10:05 PM
  2. What's so special about c#?
    By ashinms in forum C# Programming
    Replies: 32
    Last Post: 05-01-2009, 08:13 PM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. special key
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 06-27-2002, 12:53 AM