0

I have to clear one doubt which has same concept in c and c++ as well.

Suppose i have a struct like this:

struct Huffman
{
    int value;
    unsigned char sym;                 /* symbol */
    struct Huffman *left,*right;    /* left and right subtrees */
};

typedef struct Huffman Node;
Node * tree;

and now i create tree using tree variable. Then using dot operator and arrow operator both. like this.

Arrorw operator:

 for (i = 0; i < data_size; i++) 
    {
         // the problem is here this tree pointer don't store the values for all alphabets, it just remembers the last executed alphabet after this for loop.
        tree -> left = NULL;
        tree  ->right = NULL;
        tree -> symbol = storesym[i];
        tree  -> freq = storefreq[i];
        tree -> flag = 0;
        tree -> next = i + 1;
        cout<<"check1 : "<<tree -> symbol<<endl;
    } 

Dot Operator:

for (i = 0; i < data_size; i++) 
{
    tree[i].symbol = storesym[i];
    tree[i].freq = storefreq[i];
    tree[i].flag = 0;
    tree[i].left = tree[i].right = tree[i].value = NULL;
    tree[i].next = i + 1;
}

Now i am not able to understand that (1) what is the difference between the two ? (2) How they are allocated in memory ?

Sss
  • 1,519
  • 8
  • 37
  • 67

3 Answers3

4

(1): The -> is just a shortcut for (*). For example:

string s = "abc";
string *p_s = &s;
s.length();
(*p_s).length(); 
p_s->length(); //shortcut
yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • i thanks fot the help but i tried to create tree on this link stackoverflow.com/questions/21910872/… The problem was i was able to do it using dot operator but still unsuccessful to do using arrow operator, could you please help me on the link i gave you ? Actually this problem led me ask this dot operator and arrow operator question – Sss Feb 20 '14 at 22:06
1

When you have a pointer to a structure instance, you use the arrow operator: ->.

When you have a variable or direct instance of a structure, you use the dot operator, .

In these cases, classes would be accessed the same way, provide the members have the correct accessibility.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

The . operator expects its operand to be an expression of type struct ... or union .... The -> operator expects its operand to be an expression of type "pointer to struct ..." or "pointer to union ...".

The expression tree has type "pointer to struct Huffman", so you use the -> operator to access a member.

The expression tree[i] has type "struct Huffman"; the subscript operator implicitly dereferences the pointer (remember that a[i] is evaluated as *(a + i)), so you use the . operator to access a member.

Basically, a->b is the somewhat more readable equivalent of (*a).b.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • You are very close to what i want to know. actually the origin of the problem is question i asked on this link which is still un-answered , Coukd you please help me there , Actually i was creating a tree (first using the dot operator then using -> , so dot operator i done succesfully but i was not able to do equivalent for -> operator, here is the link ) http://stackoverflow.com/questions/21910872/how-to-store-values-in-struct-pointer-variable-tree-for-full-tree-in-c?rq=1 – Sss Feb 20 '14 at 21:34
  • I was also not able to understand How a[i]= *(a+i), could you please explain a bit more. – Sss Feb 20 '14 at 21:53
  • @user234839: Array accesses are defined in terms of pointer arithmetic; when you write `a[i]`, the compiler evaluates that expression as `*(a + i)`; starting with the address specified by `a` (which may either be an array or pointer expression), it computes the address of the `i`'th element following `a` and dereferences the result. – John Bode Feb 21 '14 at 13:16