What is the problem with this?
>>56443276
enjoy your buffer overflows
>>56443276
>programming in C
I found your problem nigger.
>>56443501
>n*gger
Uh, racist much?
>>56443526
calm your nigger tits, cunt
types in C start with lower case
>>56443545
this
Is it even legal to have that asterisk there?
>>56443582
That's how I always declare my pointers.
I see a pointer as a type, kind of like how in the Win32 API pointer types start with a "p" prefix.
>>56443276
why notstruct string {
char * text;
size_t length;
};
>>56443526
It's a endearing term here, nigger.
>>56443582
I'm not sure but I believe it'd even be allowed to have a space separating the star and the char.
>>56443592
Except in C, the * behaves kind of like [] does, so it sticks to the identifier, not the type.
You would writeint *foo, *bar;in the same way you would writeint foo[3], bar[4];.
It also would make sense to write the * with space either side of it, likeunsigned long int * foo;but this is less popular and it doesn't make it clear that the * is stuck to the identifier.
>>56443738
Only idiots do it this way.
If you aren't putting your asterick with your type, as it should fucking be then you shouldn't be coding in C.
>>56443276
because they're not same thing you dense retard.
>>56443526
Bitch nigger
>>56443759
Except that's completely wrong. Only idiots stick the * to the type without any space. It looks wrong, and is inconsistent with how C works.
>>56443783
But when casting, you have to stick it in with the type
>>56443276
Because char* is a String LITERAL, not a String.
A LITERAL doesn't support concatenation and strings longer than the original one, besides you're opening your program to buffer overflows and all kind of bad shit.
>>56443276
There's nothing WRONG with it, it just adds little to no value to the code, other than making things a bit more readable. Kinda like how arrays are a completely unnecessary abstraction of pointers to make them look nicer.
A potential issue with both arrays and typedefining char* to string is that they further hide pointers from developers, which contributes to the everlasting problem of "pointer illiteracy," nigger
>>56443582
This is also legal:char*String
>>56443812
Rekt em well anon.
Asterisk should accompany the type.
What the type with a _*_ becomes is actually the type that the pointer points to (variable is now a mere address)
>>56443841
AMEN
go check Golang then, you'll like it.
It's basically C+important bareboans (map list...) + great concurrency.
Inb4 Google
Well Ken Thompson(C) & Rob Pike(UTF-8) are writing it.
>>56443812
You would write it like(int *)fooor you could even do(int *) foo.
>>56444140
Of course * is the type, but in `int *`, the * isn't part of `int`. It's just like another modifier like `unsigned` or `long`. It makes no sense to join it squashed so tightly against `int`.
>>56444334
At the end of the day - it depends on how you read itint* foo- foo has the type of a pointer to an integerint *foo- a pointer to foo which is of type integer.int * foo- foo is an integer, but reading the asterisk as a modifier to the type makes the type a pointer to an integer.
desu senpai im a int* boi
>>56443276
The problem is a char pointer isn't a string. It's a char pointer. C has no concept of strings, so aliasing char* as such is plainly a lie and leads to unintuitive code and/or misled beginners.
>>56443501
this, use a real language OP
>>56445366
>int *foo- a pointer to foo which is of type integer.
That makes no sense. foo is a pointer.
* is the dereference operator. Thusint *foomeans that dereferencing foo gives you an int, which means that foo is a pointer to an int.
And you put the * with the identifier rather than the type to prevent this error:int* x, y, z;, which reads like x, y, z are all pointers to ints, when actually x is a pointer to an int and y, z are just ints. So you write:int *x, *y, *z;
instead.
>>56444334
>>56445366
>>56447914
This really feels like something that shouldn't have to be debated. Foo is a pointer to an int. That's its type. The asterisk is part of the type and should be next to the int, to denote an int pointer.int* foois the only one that isn't mental gymnastics or some amateur mnemonic.
You don't see people using generics in other languages likeArrayList <SomeClass>someClassList
>>56447972
Did you completely miss my explanation or what?int* x, y, z;is not interpreted as "x, y, z are pointers to int", but rather as "x is a pointer to int, y and z are just ints" which is very unintuitive. You would have to writeint* x, * y, * zwhich just looks stupid. Hence the only reasonable thing to do is put the * with the identifier, giving youint *x, *y, *z.
>>56447989
To be honest I've always kind of disliked multiple variable declarations on a line, outside of the odd i, j or x, y]. I can't say I've ever been pleased to encounter it in existing code.
>>56448034
Whatever, it was just an example to show that int *x is how the compiler interprets things, and how the language creators intended it (K&R uses *x). Now C isn't very strict so you can put spaces where you like, so some people like to pretend that C is Java and put the * with the type. And then they have to ban certain features of the language like multiple declarations to maintain consistency.
>>56447914
>>56447972
>>56447989
Just useX, Y, Z : access Integer;like a normal person.
>>56443738
>>56443592
i know this and i'm a different anon, but i've always thought it was a little confusing because depending on the context the asterisc next to the identifier means the "content operator"
soint* pointer;
//declaring a pointer to int
*pointer = 10;
//accessing and modifying the content on "pointer"
//which is a pointer to int, or int*
>>56443841
>>56444184
don't know how C arrays work
>>56443526
Get a load of this nigger.
C is such a crudely defined language it's shocking how popular it still is.
>>56448112
If you put it with the identifier there is no confusion.int *pointer;
// declaring that *pointer is an int, that is that dereferencing pointer gives you an int
*pointer = 10;
// declaring that *pointer = 10, that is that dereferencing pointer gets you 10
Simple. It literally reads as it is. Just disregard preconceived notions from other languages.
Writing "int* x" just shows you're a drooling retard who doesn't understand C's type system and just guesses at how to express more complicated types until it works.
>>56448150
>C's type system
kek
>>56443276
>Subject field:
> Tell me, /g/
autism
>>56447714
Yeah like PHP, or POOs as we say it here in new Delhi.
>>56447972
>The asterisk is part of the type and should be next to the int, to denote an int pointer.
Inint* foo, the operand of * is foo, not int. The operand is to the right. I'm sure you'd agree that int x indicates that x is an int. Then, int *x says that x *dereferenced* is an int (by substitution.) int *x andint (*x)are equivalent.
>>56448255
Figuring out which of C and PHP is the worse language would be an interesting exercise.
>>56443628
You can also do a flexible array member.struct string {
size_t length;
char text[];
};
struct string *s = malloc(sizeof(struct string) + 10);
s->length = 10;
strncpy(s->text, 10, "string it");
Now instead of just a pointer, your struct contains the string which could be variable size.
This is part of C99, but before that there was a GNU extension to do the same thing, but withchar text[0];.