-8

From what I've seen, the * symbol usually appears before a variable of basic types (eg. int ). However, I've come across a line of code that is as follows:

insert(int key, struct node **leaf)
{
    if( *leaf == 0 )
        {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        (*leaf)->key_value = key;
        /* initialize the children to null */
        (*leaf)->left = 0;    
        (*leaf)->right = 0;  
    }
    else if(key < (*leaf)->key_value)
    {
        insert( key, &(*leaf)->left );
    }
    else if(key > (*leaf)->key_value)
    {
        insert( key, &(*leaf)->right );
    }
}

How does the * symbol work when it comes before a structure (eg. struct node*)?

Thanks.

riceman89
  • 179
  • 1
  • 13
  • 4
    I recently saw the * symbol in this code: `return 3 * 5;`. I really wonder how *that* worked; as far as I can tell, 5 isn't a pointer. – Kerrek SB Jan 18 '16 at 18:31
  • 1
    Perhaps read any book on c rather than waiting for somebody to type something similar but not so in depth – Ed Heal Jan 18 '16 at 18:32
  • I've been searching around the internet (can't afford a textbook right now) for answers. I deeply apologize for wasting your time otherwise. – riceman89 Jan 18 '16 at 18:41
  • I left an answer about what is happening here exactly, but you need to learn and understand the basics of C pointers (declaration, allocation, address of operator &, dereference operator *, etc...) – ForeverStudent Jan 18 '16 at 18:56
  • Do you not have a library? Or second hand bookshop at the college – Ed Heal Jan 18 '16 at 19:25
  • I'll create questions lists and make trips to the campus library and see if I can find information next time. Once again, I'm sorry for wasting your time. – riceman89 Jan 18 '16 at 19:35
  • Maybe offtopic, but important: [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jan 18 '16 at 19:39
  • You are not wasting my time. You get a better understanding by reading a book and trying things out. Can you not borrow the book for a day? – Ed Heal Jan 18 '16 at 19:39

2 Answers2

1

leaf as a pointer to pointer. It means it points to a pointer in memory. And * operator dereferences its operand. So *leaf means the value of pointer that leaf is pointing to. Actually as I can see this structure is relating to a tree data structure. This code actually allocates memory for where ( a place in memory ) leaf is pointing to :

*leaf = (struct node*) malloc( sizeof( struct node ) );

struct node is a user-defined type and struct nod * means the type of a pointer pointing to a variable of type struct node.

  • "*`leaf` as a pointer to pointer.*" how do you come to know this? From the code shown is just a pointer, pointer to whatever, the OP does not tell to which type it is pointing. – alk Jan 18 '16 at 19:04
  • It casts the value of where `leaf` pointing to `struct node * ` so `leaf` is `struct node **` –  Jan 18 '16 at 19:06
  • This is just your assumption, a sane one, yes – alk Jan 18 '16 at 19:08
  • Because we do not see the relevant code. This might be the reason why the question received so many downvotes. – alk Jan 18 '16 at 19:10
  • Yes , but from source he has seen most probably it has been `struct node **` –  Jan 18 '16 at 19:11
  • "*most probably*" is not necessarily the truth. – alk Jan 18 '16 at 19:14
  • My apologies - yes, the source was a double pointer. I'll edit the OP to include more context. – riceman89 Jan 18 '16 at 19:17
1

* is both a binary and a unary operator in C and it means different things in different context.

Based on the code you have provided:

*leaf = (struct node*) malloc( sizeof( struct node ) );

Here the void * (void pointer) that malloc returns is being casted to a pointer to struct node, I don't recommend this, for more information read this

I can guess if you see the declaration of leaf it will be like this:

struct node ** leaf; //declares a pointer to a pointer of struct node

leaf = malloc(sizeof(struct node *) ); //allocate enough memory for pointer
//remember to not cast malloc in C

At this point *leaf is the pointer to struct node where the * is acting as a dereference operator.

Community
  • 1
  • 1
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
  • Thanks for the detailed answer. I didn't see that as a type cast, but its very clear that it is now. Cheers for the very helpful explanation! – riceman89 Jan 18 '16 at 19:05