Go Down

Topic: "Pointers make my head spin" etc ... (Read 4140 times) previous topic - next topic

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

pYro_65

Forum Mod anyone?
https://arduino.land/Moduino/

Naneen

As some of you know I took a little time off to learn C++. We covered pointers and how they worked. After reading through these post I'm so confused.  Hey teach help!!!

pYro_65

As some of you know I took a little time off to learn C++. We covered pointers and how they worked. After reading through these post I'm so confused.  Hey teach help!!!
What is confusing you?
Forum Mod anyone?
https://arduino.land/Moduino/

Rupert909

Code: [Select]
typedef int ArrayType[5];

void setup() {

  ArrayType array = {1,2,3,4,5};

  AddOneToArray(array);
}

void AddOneToArray( ArrayType &array ){

  for( auto &element : array ){  //Size info is maintained
    element += 1;
  }
}


Nice, didn't consider that array types remember their size, which they of course do (sizeof()).

Wasn't aware that C(++?) supports for-each type of loop syntax.

:-)

The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

pYro_65

Nice, didn't consider that array types remember their size, which they of course do (sizeof()).

Wasn't aware that C(++?) supports for-each type of loop syntax.
Yeah, maintaining the size info is useful, no need to force to a pointer, then pass a length anyway. But all depends on how it is used for what is best.

Ranged for loops were added in C++11.
Forum Mod anyone?
https://arduino.land/Moduino/

chummer1010

Reading this thread made my head spin.
#include <signature.h>

Robin2

an array is not a pointer. It will cast to a pointer to the first element if used in an expression that accepts a pointer as an operand,
I have not been explaining myself clearly enough.

I know that an array will cast to a pointer. What I don't understand is why a simple variable will not also cast to a pointer?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Rupert909

I have not been explaining myself clearly enough.

I know that an array will cast to a pointer. What I don't understand is why a simple variable will not also cast to a pointer?

...R
I guess it was deemed too confusing.

Consider this example from Java, and you see what fun can arise from automatically handling Integer as object (pointer) and arithmetic value. The glory of "autoboxing" ...

Code: [Select]

// Integer is an object, but it can be assigned an int value. The compiler
// provides "autoboxing", which means the int is converted into an Integer
// with the given value

Integer a=1;
Integer b=1;
Integer c=2;

// (1)
if (a==b) {...} // pointer comparison - fails

// (1b)
if (a.intValue()==b.intValue()) {...}  // value comparison

// (2)
a=b; // pointer assignment

// (3)
a=b+3; // see below

// (4)
c++;




Example (3) shows auto(un)boxing in all its uglyness. Since the right hand side is an arithmethic expression, the reference to c is unboxed, becoming int value 1, the values are added, becoming 4, and the result is autoboxed into a new object of type Integer, with value 4.

So the code in (2) and (3) do radically different things.

Example (4) follows the logic of example (3). Integer objects are immutable, so here the value of the original Integer object referred by c, is unboxed, then incremented, then autoboxed into a new Integer object.

The boxed versions of primitive types in Java are good for exactly one thing, and that is that variables of these types can be set to null, which is handy in many situtations.

However, this gives rise to interesting runtime situations, since parameters to methods are also auto(un)boxed.

Code: [Select]

void myMethod (int x) {...}

Integer i=4;
myMethod(i); // ok

Integer j=null;
myMethod(j); // fails at runtime


The autoboxing feature lets programmers get away with inconsistencies between parameter declarations and actual parameter expressions, which work okay for non-null values.

:-)

Its a mess.



The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

pYro_65

#24
Jul 13, 2016, 12:06 pm Last Edit: Jul 13, 2016, 12:13 pm by pYro_65
I have not been explaining myself clearly enough.

I know that an array will cast to a pointer. What I don't understand is why a simple variable will not also cast to a pointer?

...R
Without trying to be rash, I explained this in the post you decided not to read.

If you do not want to read it all, start from the paragraph that starts with "To make things a bit clearer". It gives you the reasons why an array casts implicitly.
Forum Mod anyone?
https://arduino.land/Moduino/

Robin2

In spite of my smileys I did read your Reply.

I think the difference between us is that you are describing HOW C/C++ works whereas I am asking WHY does it work like that (because it seems to me to be inconsistent).

Another way that this "inconsistency" could be avoided would be if it was necessary to do an explicit cast to a pointer for an Array as well as for a simple variable.

(Don't lose sight of the fact that this is the Bar Sport section :) )

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Rupert909

In spite of my smileys I did read your Reply.

I think the difference between us is that you are describing HOW C/C++ works whereas I am asking WHY does it work like that (because it seems to me to be inconsistent).

Another way that this "inconsistency" could be avoided would be if it was necessary to do an explicit cast to a pointer for an Array as well as for a simple variable.

(Don't lose sight of the fact that this is the Bar Sport section :) )

...R
Taking the address of an array returns a pointer to array (object), whereas the auto-conversion gives a pointer to the first element. Two functionalities can not be represented by a single implicit conversion, or explicit operator ('&').

Also, who said that the inventors of C, and later C++, were first and foremost concerned with internal consistency? With C being a step up from assembly, I'd think that terseness had high priority.

There's the old joke that the one regret Kernighan and Ritchie had about unix, was the "mail" command, should have been called "ml".

:-)
The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

pYro_65

#27
Jul 13, 2016, 02:05 pm Last Edit: Jul 13, 2016, 02:07 pm by pYro_65
In spite of my smileys I did read your Reply.

I think the difference between us is that you are describing HOW C/C++ works whereas I am asking WHY does it work like that (because it seems to me to be inconsistent).

Another way that this "inconsistency" could be avoided would be if it was necessary to do an explicit cast to a pointer for an Array as well as for a simple variable.
To mention it as an inconsistency, then to mention an explicit cast gives me reason to believe you did not understand my post.

I pointed out how C++ works, yes.

But I go on to use that to explain why arrays cast like that, so which ever way you want to describe it, I have covered it.

And to repeat very briefly, an array casts implicitly because it is the only way you can access elements (by using a pointer), so it makes sense to provide this implicitly.

...And why does an 'int' not do casting like this by default? Well that is because it does not make sense, is illogical, and is unrelated to an array.

You are asking why does an apple not taste like an orange, even though they are both fruit.

Fur is a trait of animals, a dog is an animal, but in no means does this imply that every animal is a dog, or every animal has fur...
Forum Mod anyone?
https://arduino.land/Moduino/

Robin2

#28
Jul 13, 2016, 07:31 pm Last Edit: Jul 13, 2016, 07:32 pm by Robin2
Biology is full of inconsistencies because evolution has neither a plan nor foresight. There is no reason why human creations (such as programming languages) have to have them.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pYro_65

Biology is full of inconsistencies because evolution has neither a plan nor foresight. There is no reason why human creations (such as programming languages) have to have them.

...R
There are no inconsistencies here. I'm confused as to why you trying so hard to connect two unrelated things.

Quote
Another way that this "inconsistency" could be avoided would be if it was necessary to do an explicit cast to a pointer for an Array as well as for a simple variable.
Write some pseudo code and see how your trail of thought does not really make sense. An array cast can not be applied to a 'simple' variable. For one, you need to use the address of operator to get the address of a variable. Casting it to a pointer gives you a pointer to somewhere, not a pointer to the variable.

Just rephrase your argument with another questionable statement: why can't I use the subscript operator with a simple variable ...Its inconsistent?

Well they may both be variables, but it'd be absurd to demand that a feature for arrays/pointers be available with things that have no use for them.

These aren't inconsistencies, its a flawed approach comparing two dissimilar things.

Two blueberry muffins, one with heaps of berries, and the other only has one. As the mixture was not mixed properly the muffins were inconsistent.

You cannot find an inconsistency between a brownie and a muffin, they are simply just different things. Saying your muffin is inconsistent because it does not look like a brownie is silly, but none the less, the same argument you are filing for arrays vs 'simple' variables.
Forum Mod anyone?
https://arduino.land/Moduino/

Go Up