I am confused as to when to use . vs. ->.
I have seen both being used when referencing an item in a structure.
Here's an example:
typedef struct stack{
int top;
double numbers[100];
}stack;
stack the_stack;
the_stack.top = 0;
Would you use -> if this was the following was the case:
typedef struct stack{
int top;
double numbers[100];
} *stack;
stack the_stack;
the_stack->top = 0;
If not, when is . used in C?
The . operator is the value membership operator. This means that it is used when you are accessing the object's value.
The -> operator is the pointer membership operator. The object before it is a pointer to a structure, and this goes into the pointer to access the object's members.
To use your example:
typedef struct stack {
int top;
double numbers[100];
} stack;
Then you could do any of these:
struct stack my_stack, *my_stack_pointer;
my_stack.top = 3; // Change the value of stack.top
// From whatever my_stack_pointer points to, take the value of top, and
// assign 3 to it
(*my_stack_pointer).top = 3;
// Same idea as the previous, only abbreviated
my_stack_pointer->numbers[0] = 3
Your examples are both correct. . is used for a structure element while -> is used for dereferencing a pointer to a structure.
In genera, x->y is the same as (*x).y. (The latter construct is generally discouraged.
It's simple, really. a->b is the same as (*a).b, that is, first dereference the pointer a, then access member b.
In C++, it is in theory possible to do different things with the two operators, but it's bad practice to do so (since it contrasts with everything people expect from the rest of C or C++).
the_stack->top is only shorthand for (*the_stack).top.
whenever you have a pointer to a structure, you must dereference the pointer then access the elements in the structure, the arrow is a shortcut for doing (*the_stack).top replace this with the arrow now its much cleaner and nicer the_stack->top
When u use pointer to a structure , '->' is used '.' otherwise.
typedef struct stack{
int top;
double numbers[100];
} *stack;
stack the_stack;
the_stack->top = 0; // equivalent to (*the_stack).top=0
We use . for object.attribute.
And -> is just syntactic sugar for, (*a).attribute = a->atribute
This -> is used mainly so the programmer by fault doesn't writes *a.attribute.
. operator is used to when you have an object to the structure.
-> is used when you have a pointer to the structure, you de-reference it and access the memory location.
But keep in mind you need to allocate the memory first to de-reference your pointer, in case of object, you don't need to.
Related
here I use n node inside of it as "struct n *next" how is this possible or what is the meaning of it ?
struct n {
int data;
struct n *next ;
};
TLDR
It's a pointer
There is an entity in C and C++ called pointer. It is a variable contains address in memory where actual value stored. In your case you say to compiler "I want to store address of next node in this variable". struct n just type name, next is variable name and * is mark to compiler that that variable is a pointer.
If you want to learn a little more about pointers I recommend you to read great book called "Using and understanding C pointers".
Given:
typedef struct Person_struct { char name[10]; int age; } Person;
Person bob;
void makePerson(Person *human) {
strcpy((*human).name, "Bob Bee");
(*human).age = 36;
}
Dereferencing then takes the general form:
(*variable).member //variable could be bob, member could be age
I know that the member access operator . has precedence over * so we use parenthesis to dereference the variable first. I don't really understand why this is necessary.
It actually seems more intuitive that *variable.member would first operate on the member access operator to get the pointer-to-struct_type and then .member would add sizeof(member) to get the location to be deferenced.
But the proper form (*variable).member is very nonintuitive. It is not clear what 'value' is actually being returned by (*variable) nor is it clear how the member will be accessed from whatever is returned.
My question is:
What is going on in memory when (*variable).member is used as opposed to *variable.member?
I saw this but it did not answer my question.
The reason you can't use *variable.member is because the variable in this case is a pointer, not a structure, and you can't use the member access operator on a pointer. We have a separate operator -> for doing member access on the dereferenced pointer:
strcpy(human->name, "Bob Bee");
I know that the member access operator . has precedence over * so we use parenthesis to dereference the variable first. I don't really understand why this is necessary.
You just said it: the member access operator . has precedence over te dereference operator *. If you are asking why . has precedence, that's just a design choice.
the proper form (*variable).member is very nonintuitive
I'd say it's annoying rather than nonintuitive. In fact, this is such a common operation that there's an operator for it. You can use the -> operator to access fields of a struct when you have a pointer to it.
human->age = 36
// same as
(*human).age = 36
What is going on in memory when (*variable).member is used as opposed to *variable.member?
Doing (*variable).member is the same as saying "take the address inside variable and dereference it, then take the member field of the struct you just obtained".
Doing *variable.member is the same as saying "take the address inside the member field of the struct variable, then dereference it". If variable is a pointer, that's of course just wrong.
Good question!
It much depends on how your compiler handles the meaning of the symbol.
For example, in C++, the statement (*variable).member, (*variable) can be interpreted as a reference to the actual object.
Person bob;
Person* ptr = &bob;
Person& ref = *ptr;
Reference can be used as the actual object in its context.
But, in the perspective of a compiler, (*variable) has meaning only at the immediate step in the compiling process.
At the final step (binary), (*variable) loses its meaning.
The only thing left is (*variable).member be interpreted as variable->member. That means, take the pointer variable, dereference it using type Person, get the actual attribute from the prototype of Person struct.
Example:
struct Person {
char* name;
int age;
};
Person bob;
printf("%d\n", bob.age);
A possible binary code can be
1. advance stack pointer about `sizeof(Person) = 16 bytes` (determined at compile time for 64bit machine). Top of the stack now contains the value of object bob
2. put the last 8 bytes on top of the stack into `printf` function. (You are gonna learnt more about this later)
Hope it helps
I've got a specific question regarding the arrow vs. dot notation for structs in C. I understand that -> is used for struct pointers, and . is used for objects, however I've been having some trouble parsing some code I found online.
typedef struct node{
int data;
}Node;
typedef struct heap{
int size;
Node *dataArray;
}Heap;
typedef struct plan{
int maxPile;
Heap *heapArray;
}Plan;
Given this code, if I create:
Plan *p
And then I want to access a specific index in the heapArray inside Plan I would do:
p->heapArray[i]
From here, though if I want to access either the size of the dataArray inside a struct heap, would I use '->' or '.'?
So if I wanted to get the first element of the data array of that heap would I do:
p->heapArray[i].dataArray[0]
or
p->heapArray[i]->dataArray[0]
The correct answer is
p->heapArray[i].dataArray[0]
because when you use the subscript on the heapArray pointer, it's like doing pointer arithmetic and then dereferencing the pointer, something like this
(*(p->heapArray + 1)).dataArray[0]
so when you dereference it, the type of it becomes Heap which means it's not a pointer and has to be accessed with a . and not a ->.
p->heapArray[i] is of type Heap, which is a struct, so you'd use ..
for the structure
struct school {
char*name;
int student;
int teacher;
int worker;
};
I have actually two questions..
A)is the difference between x.y and x->y syntax that pointers point to an address in the heap memory between the other syntax deal with the stack
or its not true and pointers can be used in structures to point to an address in the stack too , what is actually the difference between two syntax ?
B)when i try to print the sizeof the variable of the structure it prints 24 not 20 could it be explained ?
A) x.y is used when x is a variable of the structure type. x->y is used when x is a pointer to the structure type. It doesn't have to point to the heap, it can point to a variable. So if you have:
struct school school_var;
struct school *school_ptr = &school_var;
then school_ptr->student is the same as school_var.student.
B) Variables can have extra padding, which is included in sizeof.
What is the difference between self referential pointer in structure and pointer to structure?
struct abc
{
int data;
struct abc *next;
}
struct abc *pt;
What are the differences between *next and *pt??
How they differ in their use??
I am really in doubt between these two
I am a beginner
First example is used mainly for linked list
Are pointer to structure node and self referential pointer the same thing?
please see
see-programming.blogspot.in/2013/05/chain-hashing-separate-chaining-with.html here we have used struct hash *hashTable as an array ..how?? and can we do same with *pt
They are of the same type. They behave in the exact same way.
Some example usage:
// declare 2 structs
struct abc s1;
struct abc s2;
// point pt to s1
pt = &s1;
// point s1.next to s2
s1.next = &s2;
// access pt->data
int a = pt->data;
// access s1.next->data
int a = s1.next->data;
Differences in usage:
There's only one pt variable.
For every struct abc variable, there is a next variable.
In the context of a linked-list, there is only one head pointer, thus pt would be it.
But each node points to the next node, thus next should be used for this.
Using pointers as arrays?
Yes, this can be done with either pt or next.
A pointer just points to an address in memory. There can be any number of structs following on each other at that location.
If you want to use it as an array (or just using pointers in general), you just have to make sure you don't try to access elements that you didn't allocate memory for (with malloc for example) and free the memory after usage (if you used malloc).
Some example usage with array:
// declare a struct
struct abc s1;
// make an array of size 10
struct abc *a1 = malloc(10*sizeof(struct abc));
// give the 4th element a new value
a1[4] = s1;
// free the memory
free(a1);
I hope that helps a bit.
Conceptually, very little difference at all.
next is a member of the same structure it is pointing to. pt is not a member of the structure it is pointing to.
They are used in a similar way, except that to use next you have a have an existing struct abc, and pt can be used directly. Please consider:
myABC.next= &myOtherABC ;
pt= &myOtherABC ;
are pointer to structure node and self referential pointer same thing
They are and they are not. Depends on point of view. They are because they both point to a structure. They are not because a pointer to structure can point to any structure, and as a variable it can be a parameter, a local variable, a member of another struct, etc. But a self referential pointer is necessarily a member of a struct and points to the same struct it is a member of.
The only difference applies to people that write compilers. That is because a self-referential pointer refers to itself before itself is fully declared. So someone writing a compiler has to deal with this special case.
As a programmer (that means you), there is no difference, and the terms do not offer any additional meaning.
they are the same type but they do not contain the same value
from pt you can access next because pt point to a structure that contain next. but from next you can't access pt.
for linked list, you have to understand, that the list contain at the begining 1 element, and this element know how reach the next element of the list, it is the goal of the next pointer
There is no difference between those two pointers.
A possible difference in usage depends on the context but still does not apply to their nature of being both pointer to the same type of structure and therefore undergoing the same rules in terms of assignment, reading and arithmetics.