Why people write such cryptic code I will never understand...

Can someone explain what is going on in the following:

double color = {1,2,3};

double *ptr;
// ... assign ptr ....

*ptr = *color++;
ptr++;

So this line *ptr = *color++; sets the value of the first location of the ptr array to the first location of the color array, and then increments the color array? So the next time *ptr =*color is called, *color will now be 2 instead of 1? Is this correct?

Dave

What is going on in that code is called undfined behavior and will not work. Why? Because ptr is an uninitialized pointer, and dereferencing it like *ptr = (put anything here) is illegal and will most likely crash the program.


The second problem with that code snippet is *color++; on line 6. Variable color is not a pointer so it can not be incremented. The compiler should produce an error on that line.


line 1 is an error because a single integer can not be initialized with multiple values. color progably needs to be an array, like this: int color[] = {1,2,3};

double color[3] = {1,2,3}; //an array with 3 elements of type double

double *ptr; //create a pointer to a double

// ... assign ptr ....

ptr = color; // set pointer to color[0] (beginning of it) essentially
ptr++;//increment ptr so it points to color[1] which is 2
	  cout << "ptr " << *ptr;

This would produce 2.
The asterisk is known in a few different ways in C++ (feel free to correct me i'm learning still) , the operator to create a pointer, the dereference operator, and multiplication operator.

In this case, on line 9 I use the dereference operator which basically gets the value stored by the pointer and in this case it is 2.

Remembering pointers point to specific memory addresses, outputting only ptr with no dereference will return the address at color[1]. Again correct me if i'm wrong.

Sorry, this was a typo - should be: double color[3] = {1,2,3}; // ... assign ptr .... meant do something like ptr = GetAValidPointer() The part I was concerned with is the following. I was just trying to set the stage (which I apparently did worse than poorly!):

*ptr = *color++;
ptr++;

Sorry, this was a typo - should be: double color[3] = {1,2,3}; // ... assign ptr .... meant do something like ptr = GetAValidPointer() The part I was concerned with is the following. I was just trying to set the stage (which I apparently did worse than poorly!):

*ptr = *color++;
ptr++;

Even if ptr were properly allocated, *color++ is an illegal statement because color is not a pointer.

if color were an array though it would have worked right??

No. color must be a pointer.

The identifier of an array "color" is equivalent to the address of its first element and an array "color" can be considered a constant pointer.

So we cannot assign value to the constant,

color++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.