Worst ideas/features in programming languages?
drug
drug2004 at bk.ru
Mon Oct 11 19:26:37 UTC 2021
On 11.10.2021 22:15, bachmeier wrote:
> On Monday, 11 October 2021 at 15:59:10 UTC, Atila Neves wrote:
>> I'm brainstorming about what I'll talk about at DConf, and during a
>> conversation with Walter I thought it might be cool to talk about:
>>
>> * Worst features implemented in a non-toy language
>
> C's pointer notation is too stupid for words. (I understand how it
> works. I've been using it for decades. Please no explanations.)
>
> Writing functions like this is horrible:
>
> ```
> int timesTwo(int *x) {
> return 2 * *x;
> }
> ```
>
> When you learn C, you ask what the `*x` is. It's an int. That makes
> sense. That's why it's `int *x`. Okay, let's call that function:
>
> ```
> int main() {
> printf("%d\n", timesTwo(4));
> return 0;
> }
> ```
>
> `expected ‘int *’ but argument is of type ‘int’`
>
> Okay, let's try this:
>
> ```
> int main() {
> int z = 4;
> printf("%d\n", timesTwo(&z));
> return 0;
> }
> ```
>
> `8`
>
> So `*x` is an int, but you have to pass `&z`, which is not an int, as
> the argument. The only way this can't be confusing to someone learning
> it for the first time is if they're memorizing syntax.
I would say that `x` is a pointer to int and `*x` is a dereferenced
pointer to int. 4 is an int value and it can not be treated as a
dereferenced pointer to int. So you need to create an int value, take
the pointer to it and then pass it to the function.
More information about the Digitalmars-d
mailing list