The (.) dot and The (->) arrow operator.

Hello, can someone explain the difference between the dot and the arrow operator?
when i see it used i see functions and alot of confusing stuff since i have not worked with functions or classes or objects yet. So can someone explain the difference between them and how they are used?
here is an example given by the tutorial i am following but i don't understand anything of it:

1
2
3
4
5
6
  struct Employee {
  char first_name[16];
  int  age;
} emp;

strcpy(emp.first_name, "zara");
Last edited on
object.member
pointer->member

Your 'emp' is a variable that has a member variable (char array) named 'first_name'. You can access that char array with the dot operator.

If uoy had a pointer pointing to the emp, you would have to use the arrow to do the same:
1
2
Employee * pe = &emp;
strcpy( pe->first_name, "zara" );

Therefore, the arrow is same as dereference a pointer and then use the dot.
1
2
Employee * pe = &emp;
strcpy( (*pe).first_name, "zara" );

Line 4: emp is an object. the . operator refers to a member of the object.

1
2
3
  Employee * e;  // Uninitialized pointer to an Employee struct
  e = new Employee;  // Allocate an Employee struct and assign its address to the pointer
  e->age = 15;  // Since emp is a pointer, we have to dereference the pointer using the-> operator 


but what does strcpy and pe mean?
Last edited on
strcpy is a function for copying c-strings.
http://www.cplusplus.com/reference/cstring/strcpy/

pe is a pointer to an Employee object.
I still don't understand it, hehe. Do you think its best if i wait till i get to functions and pointers in my tutorial?
You should definitely understand functions and pointers before you try to understand derefencing a pointer to call a method (object function). You should also learn about classes.
Okay thanks.
Hello. I can explain the difference but as mentioned above, you need to know pointers and structures to understand it, because it IS a pointer to a part of a structure.

This is an example from my textbook.
*cirPtr.radius = 10 ;

^^ This is what would assume to be a pointer to a member of a structure. HOWEVER,
The dot operator has higher precedence than the indirection operator, so the indirection operator tries to dereference cirPtr . radius, nor cirPtr.

So you need to use parenthesis.

(*cirPtr) .radius = 10 ;

Since this is considered "awkward" (as the author of my text says) the -> is a visual alternative. This is only used with structures, so no need to even worry about it unit you get there.

cirPtr->radius = 10; is the same as the above.

Simple answer...... It's a matter of order of operations......
closed account (SECMoG1T)
Both are used to access member(vaiables and functions) of structures and classes but the arrow is more than that;

<DOT> the dot operator is simply used to access the member variable such as setting and retriving their values
e.g
struct employee
{
int pay;
string name;
};
// this structure is a programmer defined type so you can declare an object of the type

employee myemployee; // declare an object of the type employee;

myemployee.pay=1200; myemployee.name="zara"; // setting value to variables

cout<<myemployee.name<<"payment for the job is"<<myemployee.pay<<endl;// acessing // value

<ARROW>
The arrow operator have combined functionality i.e dereferencing functionality and dot operator functionality;
That simply implies it works with such structures and classes that involve dynamic memory and pointers
mostly allocated using operator <new>. as you might already know when we apply pointers to such data types they simply stores the adress of a variable it points to but to access the value stored in the variable we simply dereference the pointer using the asterisk* or derefence operator

e.g int x=5; int* x_ptr;
x_ptr= &x; /// x_ptr takes the adrress of x; //NOTE: the & is called addressof operator and returns an address of a variable;
// to obtain value stored in the pointed variable we dereference the pointer
// coz if we use the pointer without the arsterisk it simply gives the address
cout<<*x_ptr<<endl; /// prints 5 and not the address of x;
*x_ptr=8; cout<<x; // prints 8 coz the pointer sets x value to 8;

if we have a structure that uses dynamic memory we need to uses pointers because if we use such datatype as parametes in fuctions that need to return the same type we need to return a pointer since a struct/class is made up of many simple datatypes and it is impossible to return all at the same time;

for example;
struct employee
{
int pay;
string name;
};

employee* newemployee()
{
employee* temp_ptr; /// we use the operator {(new) without braces} to allocate dynamic memory;
temp_ptr= new employee; /// this line dynamicaly allocates a new object{//note the new objects have no identifier but we can access it thruogh the temp_ptr->name="no name yet" ; ///yu can also do as (*temp_ptr).name="no name yet" pointe temp_ptr;}
temp_ptr->pay= 0;

return temp_ptr; /// pointer to the new object is returned ,it contains the adress of the new object;
}


Simply what the arrow operator does is that it combines(the * and the . operators)
1.used to dereference the address a pointer contains to get or set the value stored int the varible itself; e.g temp_ptr->pay=1200; /// temp_ptr is a pointer;
2.it is used to access the member variables pointed to by a pointer similar to the dot operator;

note that temp_ptr->pay=1200; is equal to the manual way of derferencing as
(*temp_ptr).pay=1200; /// here you derefence the pointer and then use the dot to access its member variable;

Last edited on
Topic archived. No new replies allowed.