News for hackers. Stuff that matters.
Re:A convenient untruth: Array notation in C is a lie by cessor on Thursday December 22, 2016 @07:28AM (#13236783)
The equivalent in C# always made more sense to me (not comparing memory or allocation model between the languages, but simply syntax in relation to a person reading it):

    int[] arr = new int[5]; // C#

    int arr[5]; // C

The fact that the brackets go on the datatype always made more sense to me, after all, I want to refer to memory of a certain cell size (as indicated by int). I realize that there is a lot of stuff going on when using new, but i believe even in C it should read, because you effectively change the datatype (to be of type pointer, rather than int).

    int[5] arr;
But then, I will now duck and get the hell out. This is the same reason why it feels wrong to write:

    int *arr;
You're not changing the identifier, you're trying to change the datatype.

In summary, I believe that the syntax to fiddling with pointers in C is very misleading, and this I fully agree with the article, but as many people are accustomed to this notation, I will now duck and get far away from the internet, in fear of all the hateful comments explaining to me how I am wrong, and apparently just don't understand the superior beauty of complicated C syntax. I will now go and check my garbage collected privilege.

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @07:37AM (#13236846)
> I will now duck and get far away from the internet, in fear of all the hateful comments explaining to me how I am wrong, and apparently just don't understand the superior beauty of complicated C syntax.

I am going to be that person :-)

One phrase – "declarations mirror use". In a declaration, you use the same set of operators around the declared object that you would use in a normal expression. All of these operators (asterisk, `[]` and `()`) have the exact same precedence and associativity as in the rest of the language. The type specifier(s) on the left is the final type of the expression that you get after applying all of the operators in the correct order as per the precedence/associativity rules.

So when you see:

    char *arr[X]
You identify the identifier first: `arr`. Then, because `[]` takes precedence over the asterisk, you say that `arr` is an array (of size X) of pointers to `char`. In other words, the expression `*arr[some_index]` is of type `char`.
Re:A convenient untruth: Array notation in C is a lie by mannykannot on Thursday December 22, 2016 @07:50AM (#13236911)
To be fair, while declaration mirrors use, the use contains a few traps for the beginner.

If you have written some assembler, you will see where C is coming from. It just occurred to me that most early C programmers were probably proficient In assembly.

Re:A convenient untruth: Array notation in C is a lie by mnarayan01 on Thursday December 22, 2016 @08:31AM (#13237127)
I don't know if "declarations mirror use" gets you all the way there. You can use:

  *some_index[arr]
(though obviously not saying you should), but you can't declare:

  char *5[arr];
Obviously there's good reasons for that, but then you have "declarations mirror use, except when there's good reason not to", which basically brings you back to the original question.
Re:A convenient untruth: Array notation in C is a lie by wildmusings on Thursday December 22, 2016 @11:56AM (#13238510)
Finding an exception doesn't invalidate the explanation. Being able to switch the index and array is more an accident of how C implements arrays than an actual use case.
Re:A convenient untruth: Array notation in C is a lie by chc on Thursday December 22, 2016 @01:01PM (#13239026)
> char *5[arr]

Isn't that exploiting a quirk of the operator's definition, rather than how the operator is intended to be used?

Re:A convenient untruth: Array notation in C is a lie by mnarayan01 on Thursday December 22, 2016 @10:17PM (#13242548)
I assume you're right, though I would love to see some of the original discussion around it. That said, while I don't think it's a catastrophic point against "declarations mirror use", I do think it's a strike against it.

Edit: I guess I'll also note here just for fun that while

  char [5]arr;
seems reasonable enough,

  char [4][5]arr1;
  char[5][4] arr2;
seems less so.
Re:A convenient untruth: Array notation in C is a lie by sqeaky on Thursday December 22, 2016 @04:09PM (#13240410)
I have been writing C and C++ for a little more than 10 years now and this is the first I have heard "declarations mirror use". I understood that was the case, but why is the phrase important.

To me understanding the actual types involved is more important than getting the declaration to look some specific way. (So I always jammed the star next to type and did whatever else I though would most ease expressing types)

I am also the kind of developer who who have declared it as:

std::array<std::string, X> arr;

or

std::vector<std::string> arr;

depending on if X was known at compile time. Because I want to give the compiler as many chances to call out my mistakes as possible.

Re:A convenient untruth: Array notation in C is a lie by dkersten on Thursday December 22, 2016 @10:20AM (#13237768)
I think if all type-information were kept together, it might read simpler:

    char*[X] arr;
You read strictly left to right: char pointer array of size X called arr. Basically, it takes a simple type (char in this case) and for each thing to the right, wraps it in something. You could read it as: given a char, we have a pointer to it, and an array of size X of these pointers.

It would be interesting if the use did "mirror" declaration:

    arr[5]*
That is, take element 5 of arr and derefence it.

Just some random thoughts...

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @10:47AM (#13237995)
> You read strictly left to right

That wouldn't play well with pointers to arrays and pointers to functions:

    int (*pa)[X]; // int(*)[X] pa;
    int (*fp)(void); // int(*)(void) fp;
Here is how I designed the type declaration syntax in my own language, Quaint (https://github.com/bbu/quaint-lang):

    pa: ptr(int[3]); // int (*pa)[3]
    fp: fptr(): int; // int (*fp)(void)
    arr: int[5]; // int arr[5]
    p: ptr(int); // int *p
    pp: ptr(ptr(int)); // int **pp
    arrp: ptr[5](int); // int *p[5]
You basically have the identifier first, and then a recursive expression which uses mostly function-call-like expressions in order to nest recursive types.
Re:A convenient untruth: Array notation in C is a lie by fredmorcos on Thursday December 22, 2016 @12:34PM (#13238821)
> char pointer array of size X called arr

That does not even English.

Re:A convenient untruth: Array notation in C is a lie by contravariant on Thursday December 22, 2016 @12:14PM (#13238660)
Surely it would still mirror use if the operators were applied to the type, rather than the identifier?

Preferring 'char ٭arr[X]' over '٭char[X] arr' seems arbitrary to me. I see no reason the 'declaration mirror use' principle can differentiate between the two.

Personally I prefer the latter since it makes it easy to separate the type from the identifier.

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @12:27PM (#13238751)
> Surely it would still mirror use if the operators were applied to the type, rather than the identifier?

The "type" is actually a list of storage class specifiers (static, extern, auto, register, typedef, _Thread_local), type qualifiers (const, volatile, restrict) and type specifiers (int, char, float, double, signed, unsigned, long, short, void). Imagine the soup of keywords that would have to be at the deepest level of the expression:

    static const volatile unsigned char *arr[X];
    // vs
    *static const volatile unsigned char[X] arr;
Re:A convenient untruth: Array notation in C is a lie by Nullabillity on Thursday December 22, 2016 @07:37PM (#13241825)
static, const, and volatile modify the variable itself, not what values it's legal for it to contain. So the outcome should be:

    static const volatile *unsigned char[X] arr;
Or, because signedness should be a property of the type, not an arbitrary modifier:

    static const volatile *char[X] arr;
Re:A convenient untruth: Array notation in C is a lie by fredmorcos on Thursday December 22, 2016 @12:32PM (#13238796)
'char ٭arr[X]' over '٭char[X] arr'

What is this dirt after char and before char? This wouldn't even compile.

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @12:35PM (#13238830)
HN converts asterisks to italics and I too couldn't find a way to escape them, except when surrounded by backticks `*`. Not a C-friendly discussion forum :-)
Re:A convenient untruth: Array notation in C is a lie by bogomipz on Thursday December 22, 2016 @11:25AM (#13238263)
I've heard this phrase - "declarations mirror use" many times before but it just doesn't click for me for some reason.

Who's use? The compiler's or the programmer's? Can you elaborate? Sorry if this silly question but I've scratched my head enough on hearing that phrase that I thought I would ask. Cheers.

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @12:54PM (#13238974)
The meaning of "declarations mirror use" is that the programmer can use the declared variable in the exact same way as being declared (by applying the exact same operators):

    int **p; // declares "p" as a pointer to a pointer to int
    int x = **p; // the "**p" expression mirrors the declaration and its type is "int"
This is true even for functions:

    int *f(int a); // declares a function which takes an int and returns a pointer to int
    int res = *f(3); // the "*f(3)" expression mirrors the declaration and its type is "int"
Arrays are obvious when looked that way:

    int arr[5][5];
    int elem = arr[1][2]; // the "arr[1][2]" expression mirrors the declaration
Re:A convenient untruth: Array notation in C is a lie by bogomipz on Thursday December 22, 2016 @01:16PM (#13239144)
Thanks for the clear explanation. This makes sense. I do have a tangential question.

I have read that the reason for this declaration syntax is that it was easier form compilers and compiler writers to parse.

How does this declaration syntax facilitate parsing exactly? I fail to see why this is.

Re:A convenient untruth: Array notation in C is a lie by bluetomcat on Thursday December 22, 2016 @01:31PM (#13239263)
The advantage is that you don't introduce any new operators or syntactic forms which would only be used in declarations. This means a smaller set of tokens and a smaller set of syntactic forms. Being able to parse an expression like

    (*arr[i])(42)
means that you can use pretty much the same machinery to parse

    int (*arr[SIZE])(int a)
Re:A convenient untruth: Array notation in C is a lie by bogomipz on Thursday December 22, 2016 @05:01PM (#13240757)
This is awesome. Thank you.
Re:A convenient untruth: Array notation in C is a lie by cessor on Friday December 23, 2016 @10:16AM (#13245080)
Thank you for clarifying. I have never heard of "declaration mirrors use" (DMU) before. Also thank you for explaining it in a non-hateful way (which isn't always the tone when touching religious matters). Next up: Tabs vs. Spaces ;)

This sounds really odd to me. I can see, and others have pointed this out in the comments, too, that DMU might facilitate building a compiler for such a language and reuses several operators. However, I am not sure whether I like this goal from a person-centric perspective.

Experts might be used to matching declaration and use structures in their code, maybe to make it more 'symmetric', but I am sceptical whether this match is actually quite 'expensive' when developing code with many people of different skills.

Use and declaration are differnt concepts, and as such should have different representations (i.e., syntactically), otherwise you might introduce synonym defects (i.e., ambiguities, where it becomes hard to understand what somebody means). I found that some of my colleagues and students had a hard time to learn what pointers are about (and I believe this is a fairly common phenomenon, that people find pointers in C hard to grasp), because the asterisk is used both in declaration and in dereferencing. I found that some of my students got the concept more easily after I introduced a couple of macros:

    #include "stdio.h"

    #define IntPointer int*
    #define value_of(ptr) *ptr 
    #define address_of(v) &v

    int main() {
        int a = 42;
        IntPointer p = address_of(a);
        printf("%i\n", value_of(p));
        return 42;
    }
The above code has no other purpose than to separate the concepts verbally, so that they can be reasoned about explicitly. This can be achieved in other ways, for example, in C++ I tend to use templates or classes to abstract the pointer syntax away. Of course, I am aware that using classes introduces overhead and this technique is not necessarily feasible when you need as much performance as you can get.

This also underlines my original point to place the asterisk on the datatype rather than on the identifier, because the #define wouldn't make sense otherwise.

In summary, I was unaware that DMU was a design goal, but I suspect it to make the language more difficult to learn, code harder to read, altough this effect might not be an issue for experts.

I am not trying to convince anybody that DMU is "bad", but I am interested in the actual properties. Everybody making claims for readability owes the community an empirical evaluation. Disclaimer: I am currently working on a research project about program comprehension, including some eyetracking and FMRI work, providing exactly such evaluations. :)

Re:A convenient untruth: Array notation in C is a lie by bluecalm on Thursday December 22, 2016 @08:20AM (#13237060)
I don't agree with:

    int[5] arr;
being a more readable syntax. The problem is that if you later have:

    int[7] arr2;
It would make sense that arr and arr2 are different types (as the thing on the left is different) while they are the same type and you should be able (hopefully!) to use them as arguments to the same function.

On the other hand I agree that:

    int* arr; 
makes more sense than more commonly used:

    int *arr;
although it's messed up anyway as:

   int* a, b, c, d; 
will result in a surprise.
Re:A convenient untruth: Array notation in C is a lie by dkersten on Thursday December 22, 2016 @10:24AM (#13237796)
although it's messed up anyway as

But that's because they wanted it to be like that.

To me, this code is very unclear:

    int a, *b, c, d;
because everywhere else, multiple declarations in one statement all have the exact same type, but for some reason pointers get special treatment so that you can declare variables of type X and variables that are pointers to type X in the same statement, which just seems odd to me.

I'd be much happier if they were all strictly different and that

   int* a, b, c, d;
did, in fact, declare all four variables to be pointer to int.
Re:A convenient untruth: Array notation in C is a lie by tzs on Thursday December 22, 2016 @11:02AM (#13238128)
> [...] everywhere else, multiple declarations in one statement all have the exact same type, but for some reason pointers get special treatment so that you can declare variables of type X and variables that are pointers to type X in the same statement, which just seems odd to me.

It's not just pointers:

  int a, *b, c();
Re:A convenient untruth: Array notation in C is a lie by odbol_ on Thursday December 22, 2016 @01:20PM (#13239175)
Jesus and you guys say JavaScript is bad...
Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @02:41PM (#13239731)
oh, don't worry, we also say that C is bad. C++ also gets an honourable mention.
Re:A convenient untruth: Array notation in C is a lie by comex on Friday December 23, 2016 @12:12AM (#13242997)
I've occasionally written code like:

    char buffer[16], *ptr = &buffer[0]; // declare an array and a pointer into it
Re:A convenient untruth: Array notation in C is a lie by ant6n on Thursday December 22, 2016 @10:35AM (#13237896)
I'd say it's better style anyway to only ever declare multiple primitive types in the same statement.
Re:A convenient untruth: Array notation in C is a lie by SomeStupidPoint on Thursday December 22, 2016 @09:12AM (#13237323)
I mean, int[5] and int[7] would be different types, and lots of bugs happen from passing something besides the appropriate array type to a function.

That being said, most languages which disambiguate between int[5] and int[7] provide some kind of polymorphism (and usually store it as a struct of size + data, to enable that).

For example: you can define a first function that goes from t[N]->t pretty easily, and it would operate on both int[5] and int[7] (returning an int).

Re:A convenient untruth: Array notation in C is a lie by pasquinelli on Thursday December 22, 2016 @11:17AM (#13238213)
Right, in a language with dependent types there is a type-level difference between int[5] and int[7], but c is not such a language, therefore using a syntax that encourages the mistaken notion that there is a type-level difference between int[5] and int[7] would be misleading.
Re:A convenient untruth: Array notation in C is a lie by kibwen on Thursday December 22, 2016 @08:13PM (#13242014)
A language doesn't need dependent types to make fixed-size arrays of differing size count as incompatible types. Rust does this, for example:

  // error: expected an array with a fixed size of 5 
  // elements, found one with 7 elements
  let x: [i32; 5] = [1,2,3,4,5,6,7];
Re:A convenient untruth: Array notation in C is a lie by pasquinelli on Saturday December 24, 2016 @12:41AM (#13248752)
i don't know rust, but i read a bit about fixed size arrays in it and the fact that 32 is the largest fixed size array makes me suspicious that it works like a pair. you can do this without depending on a value, because the length is encoded in the type.

like, you can have a type (a, b), and b can, of course, be of type (a, b), b again having type (a, b), and so on. then you always carry around the length encoded in the type, and it can be checked like above.

ghc has a limit on tuple sizes, and haskell makes no type distinction between [a] based on the number of elements in it, and it isn't dependently typed.

but again i don't know rust.

Re:A convenient untruth: Array notation in C is a lie by kibwen on Saturday December 24, 2016 @01:09AM (#13248828)

  > i read a bit about fixed size arrays in it and the fact 
  > that 32 is the largest fixed size array
This is exceptionally mistaken, where did you read it? Arrays in Rust top out at the maximum value of a platform-sized pointer, which is either 2^32 or 2^64 depending on platform.
Re:A convenient untruth: Array notation in C is a lie by pasquinelli on Saturday December 24, 2016 @06:02PM (#13252001)
"Arrays of sizes from 0 to 32 (inclusive) implement the following traits if the element type allows it:

Clone (only if T: Copy) Debug IntoIterator (implemented for &[T; N] and &mut [T; N]) PartialEq, PartialOrd, Eq, Ord Hash AsRef, AsMut Borrow, BorrowMut Default This limitation on the size N exists because Rust does not yet support code that is generic over the size of an array type. [Foo; 3] and [Bar; 3] are instances of same generic type [T; 3], but [Foo; 3] and [Foo; 5] are entirely different types. As a stopgap, trait implementations are statically generated up to size 32."

from the rust docs. https://doc.rust-lang.org/std/primitive.array.html

i sort of leapt to the conclusion that these traits couldn't be implemented because generically because the type system requires them to be implemented for each size of N, and they provide the first 32 as a nicety. the similarity to the situation with tuples in haskell, (http://stackoverflow.com/questions/2978389/haskell-tuple-siz...), and the fact that rust doesn't have dependent types and so a type couldn't depend on a value, and i just kind of guessed at a possible reason.

Re:A convenient untruth: Array notation in C is a lie by kibwen on Sunday December 25, 2016 @02:00AM (#13253299)
Ah I see, yes the explanation there is correct. The types exist up to ginormous sizes, but the standard library only implements certain convenience traits for certain sizes (though using newtypes, you can implement those traits yourself for any array size you want). The specific feature we're lacking is type-level numerals, which is on the way towards but not anywhere close to a dependent type system AIUI.
Re:A convenient untruth: Array notation in C is a lie by Nullabillity on Thursday December 22, 2016 @07:45PM (#13241864)
Saying int[5] and int[7] are the same type because the compiler doesn't enforce the difference is like saying JavaScript is untyped because there is no compiler to enforce types at all.

Regardless of what the spec or the compiler says, you the programmer absolutely need to treat them separately. Especially in a language like C, where arrays aren't even self-describing at runtime.

Re:A convenient untruth: Array notation in C is a lie by pasquinelli on Saturday December 24, 2016 @12:32AM (#13248722)
no it isn't. javascript has types; they exist only at runtime, but they're there. it's patently false to say it's untyped. but type differences codify certain classes of differences, and it really isn't that crazy or unusual to say that the length of a vector, array, list, whatever you want to call it, isn't a difference of type. to say that there's a type difference even if the type checker disagrees just means you and the type checker are using different type systems.
Re:A convenient untruth: Array notation in C is a lie by Vogtinator on Thursday December 22, 2016 @02:39PM (#13239711)
There is a small type difference in C, sizeof() of those two types is different.
Re:A convenient untruth: Array notation in C is a lie by flukus on Thursday December 22, 2016 @07:44PM (#13241861)
Are there languages where these would be different types? Could they still be passed to a function that accepts a variable length array?
Re:A convenient untruth: Array notation in C is a lie by netheril96 on Thursday December 22, 2016 @09:25AM (#13237390)
> It would make sense that arr and arr2 are different types (as the thing on the left is different) while they are the same type and you should be able (hopefully!) to use them as arguments to the same function.

arr and arr2 are indeed distinct types. They are of type int[5] and int[7]. You can see this by checking that they have different sizes, or that `printf("%s\n", std::is_same<decltype(arr), decltype(arr2)>::value ? "true" : "false");` will outputs `false` on your screen.

Both of them decay into a pointer to int, that is why people commonly confuse them with pointers. But they are pointer types; they are distinct types on their own.

Re:A convenient untruth: Array notation in C is a lie by pasquinelli on Thursday December 22, 2016 @11:20AM (#13238238)
Sizes aren't types though, and if I'm not mistaken that line of code is c++ no?
Re:A convenient untruth: Array notation in C is a lie by wahern on Thursday December 22, 2016 @02:08PM (#13239518)
On older versions of clang (including macOS 10.10 / Xcode 7.2) and, I believe, GCC this code

  #include <stdio.h>
  #define is_same(T, x) _Generic((x), T: "true", default: "false")
  int main(void) {
    int arr[5];
    int arr2[7];
    printf("arr === int[5] -> %s\n", is_same(int[5], arr));
    printf("arr === int[7] -> %s\n", is_same(int[7], arr));
    printf("arr2 === int[5] -> %s\n", is_same(int[5], arr2));
    printf("arr2 === int[7] -> %s\n", is_same(int[7], arr2));
    return 0;
  }
produces

  arr === int[5] -> true
  arr === int[7] -> false
  arr2 === int[5] -> false
  arr2 === int[7] -> true
Unfortunately either clang or GCC (I can't remember) decided the obvious behavior was wrong and changed it so that _Generic behaved as-if array expressions decayed to pointers. The C11 specification for _Generic was insufficiently precise, and for various reasons both vendors and (IIRC) the C committee are going to go with the least common denominator approach (just treat them like pointers) for consistency.

So newer versions of clang and GCC print out all false.

But another way of showing that arrays are real types is with

  #include <stdio.h>
  #define countof(a) (sizeof (a) / sizeof *(a))
  int main(void) {
    int arr[5];
    int arr2[7];
    printf("countof(arr) -> %zu\n", countof(arr));
    printf("countof(*&arr) -> %zu\n", countof(*&arr));
    printf("countof(arr2) -> %zu\n", countof(arr2));
    printf("countof(*&arr2) -> %zu\n", countof(*&arr2));
    return 0;
  }
which produces

  countof(arr) -> 5
  countof(*&arr) -> 5
  countof(arr2) -> 7
  countof(*&arr2) -> 7
on all version of clang and GCC, and should on any other conformant C compiler. Although I would think that the simple sizeof proof should suffice to show that arrays are real types, notwithstanding that their evaluation rules are peculiar.

Alas, the disaster with _Generic and array expressions only proves that the situation is less than ideal. Although part of the problem is that _Generic was a novel language feature that didn't fit neatly into the historical translation phases. IMO C++ gets a lot of things wrong about C semantics, but apparently they got decltype right (presuming the behavior is a product of a clearer specification, and that behavior is consistent across implementations).

To be fair, although inelegant the compromise behavior for _Generic makes some sense. The principle use for _Generic is to implement crude function overloading. Because arrays always decay to pointers when passed to functions, it's convenient that _Generic would capture array expressions as pointers. OTOH, it makes some useful behaviors impossible. And the convenient behavior could have been had by manually coercing arrays to pointers using a trick like:

  #define decay(arr) ((0)? 0 : (arr))
  #define is_same(T, x) _Generic(decay(x), ...)
Re:A convenient untruth: Array notation in C is a lie by netheril96 on Thursday December 22, 2016 @01:24PM (#13239197)
Yes, it is c++, but both the concept of array and type decay comes from C.
Re:A convenient untruth: Array notation in C is a lie by unscaled on Thursday December 22, 2016 @04:45PM (#13240655)
Well, had C been really strongly-typed then the hypothetical 'int[5]' and 'int[7]' would have been different types, and 'int[]' would be the size-agnostic type for an array.

Then you could easily get creature comforts such as:

  typedef uint8_t[16] guid_t;
  typedef uint8_t[20] sha1_digest_t;
These typedefs would then have copy assignment, pass-by-value semantics and everything else you'd expect from value types.
Re:A convenient untruth: Array notation in C is a lie by ssalazar on Thursday December 22, 2016 @06:36PM (#13241476)
> Then you could easily get creature comforts such as:

    > typedef uint8_t[16] guid_t;
    > typedef uint8_t[20] sha1_digest_t;
> These typedefs would then have copy assignment, pass-by-value semantics and everything else you'd expect from value types.

What does this accomplish that a struct can't?

Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @01:39PM (#13239316)
>It would make sense that arr and arr2 are different types (as the thing on the left is different) while they are the same type

For what is worth, they are really different types in C++ (they can still decay to pointers for compatibility with C).

edit: netheril96 pointed out exactly the same thing.

Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @01:19PM (#13239162)
Unfortunately this is differently wrong in C# (and many other modern languages). arr here is not an array at all, but is actually a reference to an array and has different semantics from the the corresponding C declaration.

This is not just being pedantic: using the same syntax for values and references does lead to confusion. At least C# has actual value types and 'ref' (but not value type arrays AFAIK), in Java this is still being worked on.

Re:A convenient untruth: Array notation in C is a lie by cessor on Friday December 23, 2016 @09:37AM (#13244896)
I wouldn't agree to call it "wrong", since we're talking about languages that are trying to discourage you from doing your own memory management *(C#, that is, but I'd say this applies to any language running in a VM with gc, really). Thus, in such languages, there shouldn't be a conceptual or syntactical difference between values and references. The fact that C# allows to differentiate them from each other is a leaky abstraction imho, built in to satifsy people form a C/C++ background.

I believe that whether this is 'wrong' depends on the problem you're trying to solve. In my day to day work the difference usually doesn't matter, and I am totally happy passing around objects that others would call heavy. I am really happy that I don't have to care about references or values when doing scientific python stuff (pandas, numpy, etc), whereas I am also really happy that this stuff is important to people implementing these libraries.

Re:A convenient untruth: Array notation in C is a lie by braveo on Friday December 23, 2016 @09:58PM (#13248349)
> Thus, in such languages, there shouldn't be a conceptual or syntactical difference between values and references. The fact that C# allows to differentiate them from each other is a leaky abstraction imho, built in to satifsy people form a C/C++ background.

It's not a leaky abstraction, it's a very specific design choice for performance reasons. Java also has this dichotomy between types, and for the same reason, although I believe they only delineate between primitive/non-primitive.

Re:A convenient untruth: Array notation in C is a lie by gpderetta on Friday December 23, 2016 @11:11AM (#13245423)
Having first class references has very little to do with manual memory management. As you said, confusing values with references is a very leaky abstraction, especially when you have mutable data.
Re:A convenient untruth: Array notation in C is a lie by protomyth on Thursday December 22, 2016 @08:17AM (#13237042)
"The Design and Evolution of C++" by Bjarne Stroustrup has a section on alternate declaration syntax that he considered. Compatibility won out but you might find the suggestions interesting.
Re:A convenient untruth: Array notation in C is a lie by itsuart on Thursday December 22, 2016 @01:25PM (#13239213)
It was always puzzling to me why people stick to

  char *foo 
instead of

  char* foo
foo is obviously a pointer to the char, not a char. It has different size and behavior.
Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @01:35PM (#13239288)
That's because [1]

  char* foo, bar;
doesn't mean what one would expect it to mean.

[1] as per Rule of Maximum Astonishment.

Re:A convenient untruth: Array notation in C is a lie by itsuart on Thursday December 22, 2016 @01:44PM (#13239350)
I find that

char* foo = NULL;

char bar = 0;

are preferable. Is there any reason one would want to smash N declarations into one?

Re:A convenient untruth: Array notation in C is a lie by gpderetta on Thursday December 22, 2016 @02:10PM (#13239528)
I prefer that as well, but because the other option is possible and sometimes used, a few prefer to put the star close to the name as to avoid mistakes.
Re:A convenient untruth: Array notation in C is a lie by cessor on Friday December 23, 2016 @10:22AM (#13245116)
I agree.

Simply put, it would sense to provide

    #define CharPointer char*
and then declare

    char c = 'a';
    CharCointer p = &c;
To me this became clear when I did some C# interop, where IntPtr is a common class, such as:

    [DllImport("user32.dll")]
    static extern IntPtr CallWindowProc(WndProcDelegate lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
It's just an object oriented pointer interface. It looks different. I agree:

> It has different size and behavior.

Re:A convenient untruth: Array notation in C is a lie by wruza on Thursday December 22, 2016 @01:52PM (#13239416)
Otoh,

  *foo
is obviously a char. Do you put a space between dereference operator and its operand?
Re:A convenient untruth: Array notation in C is a lie by itsuart on Thursday December 22, 2016 @02:10PM (#13239525)
No, I don't. If foo is a (storage of) pointer to something, to get that something from memory I use dereference operator `* so

  *foo = 'a';
will write value of 'a' into memory at address that is in foo.

  type* foo;
is declaration and

  *foo
is dereferencing.
Re:A convenient untruth: Array notation in C is a lie by posterboy on Thursday December 22, 2016 @07:42AM (#13236870)
Java also gets this right, probably in direct response to the crufty C syntax.

> (to be of type pointer, rather than int)

No,the array typeis not a pointer, although implicit casts to pointer occur e.g. for passing arrays as function arguments. The practical difference is ... well, I don't know.