Question about the dot operator and the arrow operator in c.

May 11, 2008
21,688
1,297
126
I have a question about when to use a dot . operator or arrow -> operator.
And why gcc gives an error.

I have an array declared.
And in a function i make use of that array.
This code works but since i have some variables and arrays i decided to put them in a struct to clean up the code.
Code:
int16_t		ssc_audio_buffer1[AUDIOBUFFER_SIZE];

[B]code : snippet of function :[/B]

pointer = (uint8_t *)ssc_audio_buffer1;
fr = f_read(&file,pointer, sizeof ssc_audio_buffer1, &bytesread);
if(bytesread == 0)
{
	Stop_Song();
}

I made a struct :


Code:
#define AUDIOBUFFER_SIZE 1024
typedef struct
{
	uint8_t		ssc_dma_flag;
	uint8_t		ssc_dma_option_flag;
	uint8_t		dmabuffer_select_flag;
	uint8_t		ssc_pause_flag;
	int16_t		ssc_audio_buffer1[AUDIOBUFFER_SIZE];
	int16_t		ssc_audio_buffer2[AUDIOBUFFER_SIZE];
} audio_struct_t;

This compiles :
Code:
pointer = (uint8_t *)audio_ctrl.ssc_audio_buffer1;
fr = f_read(&file,pointer, sizeof audio_ctrl.ssc_audio_buffer1, &bytesread);
if(bytesread == 0)
{
	Stop_Song();
}

But this does not :

Code:
audio_ctrl.dmabuffer_select_flag = 1;
pointer = (uint8_t *)audio_ctrl->ssc_audio_buffer1;
fr = f_read(&file,pointer, sizeof audio_ctrl->ssc_audio_buffer1, &bytesread);
if(bytesread == 0)
{
	Stop_Song();
}


Why does gcc 4.7.2 not compile this ?
It stops with error :
src/ssc.c: In function 'UpdateDmaBuffers':
src/ssc.c:75:35: error: invalid type argument of '->' (have 'audio_struct_t')
src/ssc.c:76:48: error: invalid type argument of '->' (have 'audio_struct_t')

What am i not understanding about the difference between an dot operator and an arrow operator?
I googled for it, but i do not want examples on how to use it, i want to understand what the differences are and when to use either properly.
I am just a hobby programmer, it is not my profession.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,569
4,484
75
The difference is very simple. x->y is an alias for (*x).y.

But I suspect you want a little more than that. OK, you have a struct of type audio_struct_t. If I declare:
Code:
audio_struct_t ssc_audio_buffer1;
Then that's a set of variables declared one after the other. If I make:
Code:
uint8_t *ssc_dma_ptr = &ssc_audio_buffer1
(Ignoring pointer type) I get a pointer to ssc_audio_buffer1.ssc_dma_flag. If I offset it by some amount (which may be greater than 1 byte and is likely 8) I can get a pointer to ssc_audio_buffer1.ssc_dma_option_flag.

Now, why would you use ->? If you malloc(sizeof(audio_struct_t)) you get a pointer to a new struct. If audio_ctrl were declared that way, audio_ctrl->ssc_audio_buffer1 would be more convenient than (*audio_ctrl).ssc_audio_buffer1. Sometimes (not here) you might want an array of structs, and the same logic applies if you use a pointer to access the elements instead of dereferencing the array.

But what about:
Code:
pointer = (uint8_t *)audio_ctrl->ssc_audio_buffer1;
Let's go through what that does. You have audio_ctrl. You treat that as a pointer, dereference it, and get the pointer in the struct at the offset for ssc_audio_buffer1. Then you cast that pointer to (uint8_t *). Makes sense, if audio_ctrl was a pointer to a struct in allocated memory, but it's a local variable.
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
I have a question about when to use a dot . operator or arrow -> operator.

The "->" operator is for dereferencing a pointer. It's that simple.

Your "audio_ctrl" variable is NOT a pointer, so you can't dereference it as such.

Code:
// Example structure
typedef struct
{
  int  var1;

} my_struct_t;


// An instance of my_struct_t
my_struct_t   j1;

// a pointer to a memory location
// where an instance of my_struct_t
// may be stored
my_struct_t  *j2; 

// Access/manipulate j1 structure
j1.var1  = 5; // OK
j1->var1 = 5; // NOT OK - will not compile (j1 is NOT a pointer and you must use ".")

// Now set your j2 pointer to a valid memory
// location (point to j1's location in memory)
j2 = &j1;

// Now, use j2 to access/manipulate the
// contents of the j1 structure
j2->var1 = 12; // OK
j2.var1  = 12; // NOT OK - will not compile (j2 IS a pointer and you must use '->')
 
May 11, 2008
21,688
1,297
126
Well, Ken G and ckGunslinger I did not feel like cooking and went to the snackbar. Waiting in line, i started reading your posts again and i think i understand it.


There is something else i would like to ask, i have grown accustom to using the arrow operator with structs.
I copied some code to make use of it. But i never really understood what it does.
It is i think for for mapping addresses to pointers.

Code:
#define OSMEM_CAST(a) (a)
typedef volatile uint32_t osmem_reg_t;	// os memory registers. 
typedef volatile uint8_t  osmem_8bit_t;	// os memory registers.

I use it for this :

Code:
#ifdef MCU_USED_SAM7S256	
#define OS_MEMORY_BASE					0x0020F000	// First address of os memory ram space. 0x0020 FFFF - 1000 + 1. 
#endif 


typedef struct 
{
	osmem_reg_t	timer1;													
	osmem_reg_t	timer2;
	osmem_reg_t	timer3;
	osmem_reg_t	timer4;
	osmem_reg_t	timer5;
	osmem_reg_t	timer6;
	osmem_reg_t	timer7;
	osmem_reg_t	timer8;
	osmem_reg_t	timer9;
	osmem_reg_t	timer10;								
	osmem_reg_t	timer11;											
	osmem_reg_t	timer12;													
	osmem_reg_t	timer13;
	osmem_reg_t	timer14;
	osmem_reg_t	timer15;
	osmem_reg_t	timer16;
	osmem_reg_t	timer17;
	osmem_reg_t	timer18;
	osmem_reg_t	timer19;
	osmem_reg_t	timer20;
	osmem_reg_t	used_timers;												// Amount of used timers are stored here.
} struct_osmem_timers, *ps_osmem_tmrs;
#define OSMEM_TIMERSTRUCT_SIZE sizeof(struct_osmem_timers)


snip....

// Address calculations.
//			ADDRESSVALUE0    OS_MEMORY_BASE.
#define ADDRESSVALUE1 	(OS_MEMORY_BASE + OSMEM_TIMERSTRUCT_SIZE)	
#define ADDRESSVALUE2		(ADDRESSVALUE1 + OSMEM_DEVICESSTRUCT_SIZE)
#define ADDRESSVALUE3		(ADDRESSVALUE2 + OSMEM_RAMINFOSTRUCT_SIZE)
#define ADDRESSVALUE4		(ADDRESSVALUE3 + OSMEM_POINTERSSTRUCT_SIZE)
#define ADDRESSVALUE5		(ADDRESSVALUE4 + OSMEM_CRCSTRUCT_SIZE)
#define ADDRESSVALUE6 	(ADDRESSVALUE5 + OSMEM_MULTITSKSTRUCT_SIZE)
#define ADDRESSVALUE7		(ADDRESSVALUE6 + OSMEM_USBSTRUCT_SIZE)
#define ADDRESSVALUE8		(ADDRESSVALUE7 + OSMEM_UARTBUFFERSSTRUCT_SIZE)


// Base addresses.
#define OSMEM_TIMERS      (OSMEM_CAST(ps_osmem_tmrs) OS_MEMORY_BASE) 			// Os memory timers base address.
#define OSMEM_DEVICES			(OSMEM_CAST(ps_osmem_devs) ADDRESSVALUE1) 			// Os memory devices base address.
#define OSMEM_RAM_STAT    (OSMEM_CAST(ps_osmem_ram_info) ADDRESSVALUE2) 	// Os memory ram check base address.
#define OSMEM_POINTERS		(OSMEM_CAST(ps_osmem_pntrs) ADDRESSVALUE3) 			// Os memory pointers base address.
#define OSMEM_CRC_UNIT		(OSMEM_CAST(ps_osmem_crc_unit) ADDRESSVALUE4)		// Os memory crc unit base address.
#define OSMEM_MULTITSK		(OSMEM_CAST(ps_osmem_mlt_tsk) ADDRESSVALUE5)		// Os memory multi tsk base address.
#define OSMEM_USB 				(OSMEM_CAST(ps_osmem_usb) ADDRESSVALUE6)				// Os memory usb base address.
#define OSMEM_UART_BFFRS  (OSMEM_CAST(ps_osmem_uart_bffr) ADDRESSVALUE7) 	// Os memory uart buffers base address.


// Total size of OS memory map.
#define OSMEM_MEMORYMAP_SIZE	(OSMEM_TIMERSTRUCT_SIZE\									+ OSMEM_DEVICESSTRUCT_SIZE \
	+ OSMEM_RAMINFOSTRUCT_SIZE \
	+ OSMEM_POINTERSSTRUCT_SIZE \
	+ OSMEM_CRCSTRUCT_SIZE \
	+ OSMEM_MULTITSKSTRUCT_SIZE \
	+ OSMEM_USBSTRUCT_SIZE \
	+ OSMEM_UARTBUFFERSSTRUCT_SIZE)

What does this do ?
Code:
OSMEM_CAST(a) (a)

I understand why i never have to use a dot operator here, i have all these pointers to structs declared as can be seen at the end of the timer struct.

Code:
} struct_osmem_timers, *ps_osmem_tmrs;

What makes the pointer to the struct so special that i can use the arrow operator instead of the dot operator ?
Is it because of the cast ?
 
Last edited:

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
If you picture memory as a bunch of numbered boxes, when you dereference a pointer you're basically saying something like "get me what in box 1044".

In short, dereferencing means "get me what's at the end of the pointer".

Dave

Hmmm. I have trouble grasping what "dereferencing" means.
 

sao123

Lifer
May 27, 2002
12,653
205
106
The dot operator (.) and the deferencing operator -> serve two seperate and distinct functions.

Lets start with the dot operator... which is also called the member selection operator.

In object oriented programming... you have objects which are structs or classes.
Structs, usually only have data variables. Classes, have data variables and member functions (and special functions called constructors, destructors, and accessors).

So you use the dot operator to access a member of an object.

When memory is allocated for an object, the whole object is stored together in a memory block.

Lets do an example:

struct cylinder
{
int x;
int y;
double radius;
double height;
};

so now you declare an object of type cylinder.

cylinder MyCylinder = new Cylinder();

to get the member parts of the object MyCylinder, you use the dot operator.

MyCylinder.x = 1;
MyCylinder.y = 2;
MyCylinder.Radius = 3.0;
MyCylinder.Height = 7.5;

-----------------------
Pointers work to access a specific address and memory.
The dereferencing operator (->) is closely associated with the referencing operator (&) and the pointer operator (*);

Lets say you have a int variable.

int Z=3;

you can get the physical address of Z (where its located in memory) by using
&Z;

A pointer is a variable type which is able to store an address in memory.
Pointers are declared using the * operator.

int *Y;

Y is a variable of type pointer, which must point to a memory location storing a type integer.

int *Y = &Z

Y now points to the location of Z. the value of Y is the address of Z. which mean I can access the value of Z, through the name of Y.

cout << Y;
and
cout << &Z;
both give the same output 0xAD1489C

cout << Z;
and
count << *y;
both give the same output 3

----------------------------------------------------------------------------

The appropriate time to use the -> operator, is when you now combine the two above concepts. When accessing an objects member, through its pointer it when you replace the dot (.) with the dereferenging (->).

Lets look at another example:

consider this simple struct:


struct node
{
int data;
node *next_node;
};

Lets consider a data structure (linked list) which has 2 of these structs chained together.
when completed, it will contain 2 values. [1][6]

node *Head_ptr;

node A;
A.data = 6;
A.next_node = null;
Head_ptr = &A;
//at this point the list has 1 node in it. [6]
//you can access the address of the node by &A, or Head_ptr

//to access the data, through normal means, you could simply do A.data!
//OR
//you could do Head_ptr->data

//Let reconsider a more efficient version of the program, which doesnt name the A:

node *Head_ptr;

Head_ptr = new node;
Head_ptr->data = 6;
Head_ptr->next_node = null;

//when the Node member is access by a pointer instead of its name, you //replace the . operator with the -> operator.
//Lets add another node.

node *temp_ptr;

Temp_ptr = new node;
Temp_ptr->data = 1;
Temp_ptr->next_node = Head_ptr;
Head_Ptr = Temp_ptr;

//now the list has 2 elements in it [1][6].
//i can access the second element [6] by doing the following:

cout << Head_ptr->Next_node->Data;
//would produce the number 6.


Hope this explanation was helpful.
 
May 11, 2008
21,688
1,297
126
Thank you for the explanation. I understand pointers, as long as it is close to the hardware, i get it. I automatically see variables stored in memory as addressable locations. It is when it gets abstract that i have difficulty following.

The & for address i understand as i do the *pointer.
Just the rules when to use . or -> are not clear to me.

If i understand correctly, if i had done this :

Code:
audio_struct_t  *ptr_to_audio_struct;
audio_struct_t   audio_ctrl;

It would have worked. I could have used -> without a problem after i did the following :

Code:
ptr_to_audio_struct = &audio_ctrl;

Then i would have had a pointer that is capable of accessing all variables in the struct. Yes ?

I will try that later on.
 
Last edited:
May 11, 2008
21,688
1,297
126
Well, this compiles :

headerfile :

Code:
#define AUDIOBUFFER_SIZE 4096
typedef struct
{
	uint8_t		ssc_dma_flag;
	uint8_t		ssc_dma_option_flag;
	uint8_t		dmabuffer_select_flag;
	uint8_t		ssc_pause_flag;
	int16_t		ssc_audio_buffer1[AUDIOBUFFER_SIZE];
	int16_t		ssc_audio_buffer2[AUDIOBUFFER_SIZE];
} audio_struct_t;

In my c file.

Code:
audio_struct_t	audio_ctrl;


void function (void)
{
audio_struct_t    *ptr;

  ptr = &audio_ctrl;
  ptr->ssc_pause_flag = 1; 

 snip...

}
 

sao123

Lifer
May 27, 2002
12,653
205
106
your usage is correct.

//access by name = (.)
audio_ctrl.ssc_pause_flag = 1;

OR

//access by pointer = (->)
ptr->ssc_pause_flag = 1;
 

purbeast0

No Lifer
Sep 13, 2001
53,472
6,315
126
i remember as a freshman in school i could not understand pointers to save me. then my buddy who had graduated drew it out for me and BAM it clicked like a mofo.

now it's crazy how much you use lists and stuff and don't even bat an eye about it.
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
sao123 did a wonderful job explaining it, but truth be told, you rarely if ever need to use the -> operator. It's more for programmers that like to keep code written in less lines. There isn't any real efficiency benefit to using it at all. It does work if you know how to use it, but it doesn't impart any practical benefits.
 

purbeast0

No Lifer
Sep 13, 2001
53,472
6,315
126
sao123 did a wonderful job explaining it, but truth be told, you rarely if ever need to use the -> operator. It's more for programmers that like to keep code written in less lines. There isn't any real efficiency benefit to using it at all. It does work if you know how to use it, but it doesn't impart any practical benefits.

going to disagree with this comment. it's used a lot in the real world for C++ applications.

and there is a benefit of using it over the . operator, like if you want to change the address that a pointer is pointing at instead of the value of the object it is pointing at.
 
May 11, 2008
21,688
1,297
126
One more question, if i understand correctly, this is a means to map a physical address to for example a pointer . Yes ?
Code:
OSMEM_CAST(a) (a)
I define it like this, i had seen it used in a header file of Atmel, tried it for my own purposes and it worked.
Code:
#define OSMEM_CAST(a) (a)
It is useful in gcc when you want directly map a physical address to a pointer for some reason. At least that is what i think it does. Seems to be that way, because it works. The right a is the address and the left a is a pointer.
Silly question maybe, does it have to be a ?
If i would write :
Code:
#define OSMEM_CAST(a) (b)
Would it still work ?
I will just try when i have some time.
Strange, i was so afraid i would break my code, i never thought of it to just try it out. And at the time , it was a means for another purpose, so i never really thought about it anymore. Until last weekend.

EDIT:
forgot something to add :

Code:
typedef struct 
{
uint32_t a;
uint8_t b;
uint8_t array[6];
} my_struct, *pointer_to_struct;

Code:
#define Pointer    (OSMEM_CAST(pointer_to_struct) 0x00001000)
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Someone can correct me if I'm wrong, But I believe this is commonly done in the case of embedded systems.
Code:
const int* BOB = (int*)0xdeadbeef;

I've not used C++ professionally so I couldn't tell you if it is the right way to do things.
 

Spungo

Diamond Member
Jul 22, 2012
3,217
2
81
and there is a benefit of using it over the . operator, like if you want to change the address that a pointer is pointing at instead of the value of the object it is pointing at.

This whole thread is interesting. I didn't know there was a difference between . and -> because they seemed like the same thing in the two main languages I've used. Objects in Perl use -> for everything because . is used for concatenation. I'm wondering if the arrow in C is similar to that in Perl since Perl is based on C. Perl also uses -> to go through tables easier. Example below.

For those not familiar with Perl, scalar values start with $. A scalar just means a single thing, not a list, not a hash, not a dictionary. It can be an integer, a string, a reference (same as a pointer?), or an object. Arrays start with @. An array in Perl is just a list with a numbered index. It's not type restricted or length restricted. It's like a C# Collection.
Code:
my @array = ("steve", "james", "matt"); 
my $ref = \@array;       # $ref doesn't store the value. it says where to find the value. 
# the $ref doesn't know what kind of data it points to. it could point to nothing.

print $ref;       #outputs "SCALAR(0xaca42ac)"

print @{$ref};   #go to whatever $ref points to and interpret it as an array, outputs "stevejamesmatt"
# putting "@{$ref}" inside double quotes would add spaces between the elements, but that's a perl quirk

print ${$ref};  # follow the reference and interpret the value as scalar, outputs "Not a SCALAR reference" warning and fails to compile

# now we can try to extract individual elements from the array using arrows
print $ref->[0];  # follow $ref, interpret as an array, and return the first element of that array. outputs "steve"

# now let's change the array it's pointing to. the syntax looks weird because the elements of the array are scalar
$array[0] = "sammy";   # so @array is now ("sammy" "james" "matt")

print $ref->[0]; #outputs "sammy"


Perl is interesting because it forces you to reference things. In a language like C#, you can pass things as values because input variables are declared in the method signature, so the method knows what each of your arguments are supposed to mean:
Code:
class Person
{

// constructor of a class
public Person(string name, int age, List<string> hobbies)
{
    private string _name = name;
    private int _age = age;
    private List<string> _hobbies = hobbies;
}

// make some getters and setters here
// add some methods here, etc
}
The important one is List<string> hobbies. C# knows it's a list that should stay together as a list.

You can't do that in Perl because Perl doesn't use method signatures. All values are passed to the method in the array @_. If you try passing a list of values, Perl thinks you're trying to pass a bunch of individual scalar values. For example, the following code is perfectly valid:
Code:
 my ($name, $age, $hobby);
sub CreatePerson{
   $name = shift;  #shift from @_ input array
   $age = shift;
   $hobby = shift;
}

my @parameters = ("sammy", 30, "dancing");

CreatePerson(@parameters);

print $name; #outputs "sammy"
print $age; #outputs "30"
print $hobby; #outputs "dancing"

What happens when I try passing an array with the intention of keeping it as an array? It won't work as expected.
Code:
my ($name, $age, @hobbies);
sub CreatePerson{
   $name = shift;
   $age = shift;
   @hobbies = shift; 
}
my @hobbies_input = ("dancing", "singing", "reading");

CreatePerson("sammy", 30, @hobbies_input);

print @hobbies; #outputs "dancing" instead of the list I wanted

Perl thinks I'm trying to say this:
Code:
CreatePerson("sammy",  30, "dancing", "singing", "reading");

The third item, dancing, is the only one that got shifted into @hobbies. The only way to get a list into the function is with a reference.
Code:
my ($name, $age, @hobbies);
CreatePerson{
   $name = shift;
   $age = shift;
   my $ref_to_hobbies = shift;  #this line changed
   @hobbies = @{$ref_to_hobbies};   #de-reference this thing as an array
}
my @hobbies_input = ("dancing", "singing", "reading");

CreatePerson("sammy", 30, \@hobbies_input);   #passing the \@ reference instead of passing the values

print "@hobbies";   #outputs "dancing singing reading"


I can't even remember where I was going with this. Hopefully that clears up a little bit about referencing and de-referencing.
 
Last edited:

sao123

Lifer
May 27, 2002
12,653
205
106
sao123 did a wonderful job explaining it, but truth be told, you rarely if ever need to use the -> operator. It's more for programmers that like to keep code written in less lines. There isn't any real efficiency benefit to using it at all. It does work if you know how to use it, but it doesn't impart any practical benefits.

Actually -> is used a lot in the real world.
polymorphism would be virtually impossible without it.

One of the advantages of polymorphism, is you can have a container of derived objects, and access them all through their base class pointer. (with some type casting magic)

http://www.cplusplus.com/doc/tutorial/polymorphism/
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
This whole discussion makes me glad I don't have to work with pointers... but at the same time I run into certain cases where I know pointers would help me with what I'm doing.

However, I always just made the mental association that if the variable I'm working with is *already a pointer*, then I need to use "->" to access methods/members. That's as far as I ever looked into it when I did some C++.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
This whole discussion makes me glad I don't have to work with pointers... but at the same time I run into certain cases where I know pointers would help me with what I'm doing.

However, I always just made the mental association that if the variable I'm working with is *already a pointer*, then I need to use "->" to access methods/members. That's as far as I ever looked into it when I did some C++.

Most of the concepts of pointers can be easily created in other languages (but not all the concepts)

For example, if you wanted to approximate the ptr goodness in javascript you could make something like this.
Code:
var ptr = { value: 3 }

Now ptr is effectively a pointer to the value 3. If you have this

Code:
var ptr = { value: 3 };
var ptr2 = ptr;
ptr.value = 4;
console.log(ptr2.value);

You are going to see 4 on the output screen.

The only thing that is hard/impossible to recreate in other languages is pointer math, but really there isn't much loss there.

If this helps, you can think of (*ptr) == (ptr.value) and ptr->thing as a shorthand for (*ptr).thing or effectively ptr.value.thing.
 

purbeast0

No Lifer
Sep 13, 2001
53,472
6,315
126
eh that isn't really true. that javascript ptr is not a "pointer to the value 3".

ptr is an object, that is more like a map, where it has 1 item and the key is "value" and the value for that key is 3.

javascript basically passes objects around by reference, and primitives around by value. there's more to it than that, but that is basically what it acts as if it's doing.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
eh that isn't really true. that javascript ptr is not a "pointer to the value 3".

ptr is an object, that is more like a map, where it has 1 item and the key is "value" and the value for that key is 3.

javascript basically passes objects around by reference, and primitives around by value. there's more to it than that, but that is basically what it acts as if it's doing.

It behaves almost exactly equivalent. Javascript objects with single fields behave very similarly to the way C++ pointers do. If you don't violate the notion of having a single field (by adding extra fields or what have you), you essentially have a C++ pointer (minus the ability to do pointer math/casting/or the type safety).

I get it, it isn't exactly the same and it is implemented differently. I'm rather showing how to emulate the behavior to show that a pointer really isn't that hard of a concept to grasp.

If you wanted something even closer than here is how to emulate it in java

Code:
public class Box<T> {
  public T value;
}

A box of T will behave pretty much exactly how a pointer of T would (minus the pointer math). The only thing you can't really do here is something like casting Box<Integer> to Box<Float> and getting something intelligible out (in C++ you can do this, but probably shouldn't). Other than that, the behavior of ptr.value = 7; ptr = ptr2; works in pretty much the same fashion as a C++ pointer (in fact, it will compile to almost exactly the same code in java).
 
Last edited:

purbeast0

No Lifer
Sep 13, 2001
53,472
6,315
126
i still disagree. all you are showing in your example is that javascript passes objects around by reference. that doesn't really have to do with pointers.

depending how you actually use pointers (in C/C++), you can change their reference or you can change the actual values.
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
Actually -> is used a lot in the real world.
polymorphism would be virtually impossible without it.

One of the advantages of polymorphism, is you can have a container of derived objects, and access them all through their base class pointer. (with some type casting magic)

http://www.cplusplus.com/doc/tutorial/polymorphism/

going to disagree with this comment. it's used a lot in the real world for C++ applications.

and there is a benefit of using it over the . operator, like if you want to change the address that a pointer is pointing at instead of the value of the object it is pointing at.



Not needed at all. It does allow for it, but can be done without. Again, you just need to know how pointers work to not have to use the operator at all. There are ways to code around it if it confused you. I am not saying not to use it, because if you know how to use it and use it properly then fine, but one doesn't need to use it.
 
Last edited: