Tell me, Sup Forums

What is the problem with this?

enjoy your buffer overflows

>programming in C

I found your problem nigger.

>n*gger
Uh, racist much?

calm your nigger tits, cunt

types in C start with lower case

this

Is it even legal to have that asterisk there?

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.

why not
struct string {
char * text;
size_t length;
};

It's a endearing term here, nigger.

I'm not sure but I believe it'd even be allowed to have a space separating the star and the char.

Except in C, the * behaves kind of like [] does, so it sticks to the identifier, not the type.

You would write int *foo, *bar; in the same way you would write int foo[3], bar[4];.

It also would make sense to write the * with space either side of it, like unsigned long int * foo; but this is less popular and it doesn't make it clear that the * is stuck to the identifier.

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.

because they're not same thing you dense retard.

Bitch nigger

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.

But when casting, you have to stick it in with the type

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.

Something wrong, nigger?

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

This is also legal:
char*String

Rekt em well user.

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)

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.

You would write it like (int *)foo or you could even do (int *) foo.
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`.

At the end of the day - it depends on how you read it

int* foo - foo has the type of a pointer to an integer
int *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

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.

this, use a real language OP

>int *foo - a pointer to foo which is of type integer.

That makes no sense. foo is a pointer.

* is the dereference operator. Thus int *foo means 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.

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* foo is the only one that isn't mental gymnastics or some amateur mnemonic.

You don't see people using generics in other languages like ArrayList someClassList

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 write int* x, * y, * z which just looks stupid. Hence the only reasonable thing to do is put the * with the identifier, giving you int *x, *y, *z.

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.

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.

Just use X, Y, Z : access Integer; like a normal person.

i know this and i'm a different user, 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"

so
int* pointer;
//declaring a pointer to int
*pointer = 10;
//accessing and modifying the content on "pointer"
//which is a pointer to int, or int*

don't know how C arrays work

Get a load of this nigger.

C is such a crudely defined language it's shocking how popular it still is.

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.

>C's type system
kek

>Subject field:
> Tell me, Sup Forums
autism

Yeah like PHP, or POOs as we say it here in new Delhi.

>The asterisk is part of the type and should be next to the int, to denote an int pointer.

In int* 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 and int (*x) are equivalent.

Figuring out which of C and PHP is the worse language would be an interesting exercise.

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 with char text[0];.