-3

Possible Duplicate:
What are the barriers to understanding pointers and what can be done to overcome them?

i am really not familiar to c and pointers, I want to understand what is going on here :

typedef struct {
        int q[QUEUESIZE+1];
        int first;
        int last;
        int count;
} queue;
init_queue(queue *q)
{
        q->first = 0;
        q->last = QUEUESIZE-1;
        q->count = 0;
}

Is that correct to think that : q->first = 0 implies that if one assign to the '0' address some value 'val', then *(q->first) will return 'val' ?

Community
  • 1
  • 1
user1611830
  • 4,749
  • 10
  • 52
  • 89
  • 1
    `*(q->first)` isn't even valid code. Can you explain your question better? – Carl Norum Jan 27 '13 at 20:52
  • 1
    What's stopping you from getting a text book on C and spending some time learning the basics. I can't imagine that this wouldn't be spelled out in great detail anywhere. – Kerrek SB Jan 27 '13 at 20:54

2 Answers2

3

No. q->first = 0 is assigning 0 to the attribute first of queue. q is a pointer but q->first is an int.

Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
1

q->first is a short hand for (*q).first
The parenthesis are necessary because . would be evaluated before the dereference * and since q is a pointer well q.first == NOT A VALID THING


queue aQ;
init_queue(&aQ);

the function init_queue take a pointer to a queue not a pointer to an int.
The role of this function is to initialize all the field of the structure to be usable by other function at a latter time.

Vincent Bernier
  • 8,674
  • 4
  • 35
  • 40
  • Sorry but what I don't understand is if a take an 'int' i, and I pass it in init_queue(&i), (*i).first would be equivalent as setting the value of a 'typedef struct' and assign i.first a value, no ? – user1611830 Jan 27 '13 at 21:06