>hurr look at me abusing syntax >stupid programmers it's you
Logan Martinez
let a = 1;
Michael Long
>abusing syntax im not though, am i this is a quick method of initializing variable_0-5 to 0-5 for all elements in the structure array where all elements are of equal size and need to be accessible uniquely, instead of using enums for indexing array or some other method
Kayden Robinson
this is how it works under the hood regardless javeesh except you can do more stuff if you actually are given tools to manipulate memory
Logan Price
!!a
Landon Gray
You clearly don't write code for money. Shit like this is what neckbeards write in their mom's basement because nobody has to ever read it again because of "muh elegance" and "muh shortness of code". Any sensible programmer would just write it into a function or as multi-line initialization.
Cooper Morgan
Nobody writes C like that.
Adrian Rodriguez
stop posting this all over again
Daniel Williams
void*
Luis Morgan
Real talk. Does this code invoke undefined behaviour? At first I thought it did, but now that I think of it, I'm not sure, because I'm pretty sure you're allowed to cast a pointer to a struct to a pointer to its first member without violating strict aliasing rules. Also I have a vague memory (probably wrong) that treating structs like arrays is actually okay as long as the underlying types match up. Does C guarantee the padding between the struct members is the same as the padding between the array members?
can a high level wizard explain me these casts, particularly the *(long *)?
Juan Murphy
well that retarded way of doing it. *((long*)(some_struct + k) + l) = l;
(some_struct + k), get the current struct cast it to long pointer and loop the longs in the struct. dereference long pointer and set it to l.
Jason Hernandez
I think this has more to do with your code than it does the C language. Why are you casting your pointer as a long, then adding adding a size to it, and then casting back to a long*? Shouldn't using C's built in pointer arithmetic is the way to go here? *( (long *)(some_struct + k) + l ) = l
Logan Foster
(void)
Joshua Campbell
The only thing wrong with this code is assuming pointers are long. Here's the fix *(long *)((size_t)&some_struct[k] + (l * sizeof(long))) = l;
Zachary Peterson
It's undefined behavior, because the compiler doesn't guarantee that the variables will be placed exactly like you defined it.
Jaxson Morris
Imagine being this mad
Samuel Fisher
Just get the address of the struct in the array, cast it to a long pointer, add the offset for the field, and dereference struct some_struct_s { long variable_0; long variable_1; long variable_2; long variable_3; long variable_4; long variable_5; };
struct some_struct_s some_struct[10];
for (int k = 0; k < 10; ++k) { for (int l = 0; l < 6; ++l) { *(((long *)&some_struct[k])+l) = l; } }
Ian Cruz
You could even pack it to ((long *)&some_struct[k])[l] = l;
Landon Reyes
for (int k = 0; k < 10; k++) { long *ptr = &some_struct[k].variable_0; for (int l = 0; l < 6; l++) { ptr[l] = l; } }
Brandon Campbell
Why not init the struct once on the stack with initial values using temp = { ... } and then memcpy it 10 times into the array?
Brody Sullivan
*things stupid C programmers do FTFY #include
typedef struct { long variable_0; long variable_1; long variable_2; long variable_3; long variable_4; long variable_5; } some_struct_s;
some_struct_s some_struct[10]; for (int k = 0; k < 10; ++k) { some_struct[k] = (some_struct_s){ 0,1,2,3,4,5 }; }
Wyatt Clark
>this is how it works under the hood It's not.
Jordan Thomas
Using prefix and postfix operators inside more complex expressions is absolutely retarded, but apparently writing deliberately hard to read code is a dogma among Cniles. Even K&R is full of this shit.
Hunter Cruz
>that treating structs like arrays is actually okay as long as the underlying types match up I'm not sure if you actually want to do that in real life as it makes your code quite unreadable but it's totally possible. And 'cause structs are bit-fields you can abuse this like so:
I had this snipped of code in an exam some time ago, you had to figure out what would cause the printf to print "flag".
Evan Brown
What is the answer? >pure chance because there is no grantee s2 comes immediately comes after s1 ?
Jeremiah Mitchell
((char)%s)[3] The answer is in the printf, the struct in the snippet creates an 8 byte wide bit-field, by accessing the 4th element of the struct you're accessing the first char of s2.
Chase Morris
reminder that this is valid C. typedef struct { int whatever; int lmao[0]; } some_struct_s; some_struct_s* s = malloc(sizeof(some_struct_s) + sizeof(int) * 5); for (int k = 0; k < 5; ++k) { (*s).lmao[k] = k; }
Isaiah Fisher
woops, it's an &, not %
Isaac Phillips
I don't think bitfield is what you think it is. C does not guarantee the members are allocated next to each other and can add padding if it wants. Your instructor was dumb
Logan Phillips
B-but they told me that C is a l-low level language...
Isaac Mitchell
it is
Sebastian Martin
Sure, if your computer is a PDP-11.
Asher Smith
> C gives the freedom to do almost anything > I decide to shoot myself in the foot > It's C's fault Imagine jumping from a roof, hurting yourself badly, and later blaming the government because it didn't outlaw stupidity.
Ayden Peterson
You can tell the compiler to pack the structs
Grayson Peterson
Took me a while to notice.
Luke Hughes
it is as low as you can get (including assembler)
Jace Lopez
C makes it too easy to shoot yourself in the foot, so you do it constantly by accident. There need to be safety measures in place.
Grayson Bennett
>What is uintptr_t
Robert Ortiz
>look at me >i understand how memset works !!!!! fuck off cs101 babby
Brandon Murphy
#include main(){ long long a = 33055139558552902; puts(&a); }
Better syntax couldn't be abused at all. The most valid complaint against the C family is that it's ungodly redundant. That doesn't mean the language is bad but its ignorant to pretend like it doesn't have problems.
Jaxson Sullivan
C is a high level language, you're just retarded.
Luis Allen
t. nigger
Eli Robinson
Program in C
Jordan Phillips
memset(&some_struct, 0, sizeof(some_struct));
Phew, that was hard, OP!
Jackson Gutierrez
It also doesn't do the same thing
Luke Collins
It's just incrementing the variable by one, what's wrong with it?
William Miller
why not just hide this garbage behind a lib?
struct_init(&faggot);
theres libs that do this shit for you, even the kernel has one so retards don't implement their own shit.
David Foster
>(some_struct_s){ 0,1,2,3,4,5 } pretty much, i just do the same and it looks much cleaner
Isaiah Martinez
cant do that with C89 though
Kevin Taylor
1989 was 30 fucking years ago, though. Get a newer compiler.
Jackson Jenkins
i use gnuC anyways so it doesn't matter.
Asher Lopez
Have to use old version of XC8 compiler at uni labs for PIC microcontrollers, and it only supports C89.
Ended up having to rewrite all my struct initializations.