Int *foo = malloc(sizeof(int) * 4);

int *foo = malloc(sizeof(int) * 4);
int bar[5];

What are the types of foo and bar, Sup Forums? You should know this.

Other urls found in this thread:

en.wikipedia.org/wiki/PDP-8
en.wikipedia.org/wiki/CDC_6600
twitter.com/NSFWRedditVideo

malloc returns void* so you need to cast it to an int* retard
and fuck off with your homework

>typical C++ programmer

Int pointer

int

Look at my portable and robust code
#include
#include

int main (void) {
char *p = malloc (4 * sizeof (int));
if (p == NULL) {
return 1;
}
p[0] = 'h';
p[1] = 'e';
p[2] = 'l';
p[3] = 'l';
p[4] = 'o';
p[5] = 0;
if (printf ("%s\n", p) < 0) {
free (p);
return 1;
}
free (p);
return 0;
}

Bullshit code like this and especially this braindead style of identation is something that I unironically encounter every day at work. I don't even have the energy to refactor this junk anymore, every day I want to kill myself more.

>char *p = malloc (4 * sizeof (int));

int * and int [5]

Triggered?

the thing that makes me the angriest about this is the indentation.

i mean yes it's also completely retarded to not cast the return value of .getLabel(), decrement, and pass it directly to GUI.cards

but god damn, why is every successive else if block further indented? that's what really rustles up the old jimjams for me

foo: int*
bar: int[]

Nice dangling pointer you have there, faggot.

>portable and robust code
Only works on implementations with sizeof(int) > 1

No. char has exactly 256 values (otherwise it means that you use something that ain't ascii or utf8, but it's impossible there is nothing more than those two). So sizeof (int) >= 2.

Deal with it my code is portable.

It's right there in the type signature, dumbfuck.

This
Wrong

>implying java is any better overall

Nope, never said anything like that.

You laugh, but the textbook for one of my courses had code samples just like that.
The worst part is that the book was written by my professor.

This, come on guys

this

heap
stack

Read the relevant part of the standard again. A char just has to have at least 255 values, but it could be more. An int has to have at least 65,535 values. Of course sizeof(char) is always 1, but 1 byte doesn't necessarily mean 8 bits. You could have a 16-bit char, a 16-bit short and a 16-bit int, in which case sizeof(int) is 1 and p[4] is outside the array.

The indentation kind of makes sense. In a sense there's no such thing as elseif in C, it's just nested ifs inside the elses.

An if statement has the form (roughly)
if (expression)
statement

or
if (expression)
statement
else
statement

To do else-if, you just replace the second statement (the one after "else") with a whole new if statement. So they're kind of all tail-nested or something.

But I agree that it looks bad to actually indent it that way.

Implying a char can be something else than ascii or utf8 part. Shut the fuck up.

>No. char has exactly 256 values
retard

Bastard encoding are a plague that must disappear. No code must acknowledge their existence.

this has nothing to do with encoding but the architecture.

A char os for text, text are ascii or utf. If you have strange numeric type, use int. It's da best.

Case closed.

read the fucking standard and learn about different cpu architectures.

This is correct.

and every char is 8 bit long, right ?

int by stadard must be least 2 bytes wide

You don't get it. I'm sorry for you.

>but 1 byte doesn't necessarily mean 8 bits

Citation needed.

seriously ? do you only know x86 ?

Can you name a platform where it isn't 8 bit?

and something you just made on an fpga or asic doesn't count.

en.wikipedia.org/wiki/PDP-8

or pretty much everything there a parity bit is added

I meant something made in this century, or at least something made in the last 50 years?

A byte is always 8 bits on any machine.

That's like saying "I ran a race where one yard was equal to two feet"

i have always been an ardent follower of the "only indent after an forces you to do it. it keeps the scopes of conditional blocks and loop bodies consistent.

woah something got fucked right before i posted... what i mean to say was:

i have always been an ardent follower of the "only indent after an opening brace, and dedent for the closing brace". bsaically the way python forces you to do it. it keeps the scopes of conditional blocks and loop bodies consistent.

funny, i just posted a link to a machine where a byte is 12 bits.

>or at least something made in the last 50 years
en.wikipedia.org/wiki/CDC_6600
+ there are still some embedded architectures

>malloc

1965 was more than 50 years ago

>int* foo = new int[4];

C fags will attack this.

>naked new
>C arrays
Not idiomatic enough.
std::vector foo(4);

std::array foo;

nah that's int foo[4]; senpai

Please, you're old grandpa. Use modern method.

I think you misunderstand senpai.
std::array foo; is the i d i o m a t i c way to do int foo[4];
Likewise std::vector foo(4); for int* foo = new int[4]; or int *foo = malloc(sizeof(int) * 4);

OK. We agree. I just misunderstood your laset post. Yes array and vector are not the same thing.

they are both pointers

this is why C was a mistake

>tfw to smart to understand pointers

int* ptr = 0;
ptr++;


What is the value of ptr after these two statements?

we just don't know

platform dependent

But typically 4

1. You incremented 0.

4 [spoiler]on my machine xd[/spoiler]

1. No he didn't

depends on the architecture, most likely 4 on 64bit systems and 2 on 32bit systems

extern int* ptr = new int();


What does this line of code do? Don't disappoint me, Sup Forums, you can do it!

no he incremented 0 by ptr

No, he incremented the fucking pointer, you retard.

>most likely 4 on 64bit systems and 2 on 32bit systems

Fails to compile.

Yeah, the pointer was 0 and he incremented it by ptr, which is of an unknown size to us

Sup Forums btfo

>the pointer was 0
The pointer can't be an int, because the pointer is a pointer, you fucking retard. Go read up on what pointers actually are and do.

its a pointer to an int which is at least 2 bytes. other than that, you are right.

POO

value of prt is sizeof(int)

It depends of the pov. For me it's 1.

Probably, but not necessarily, 4 or 8.

Is the following statement legal?

int test[] {1, 2, 3};


The answer may surprise you!

Language?

C++

It depends of the compiler. I think g++ on OpenBSD refuse it but not on Linux. It's probably a new syntax.

Yes, it is valid, but it's not surprising, considering int a{4}; is also valid and a syntax that (for some stupid reason I don't understand) I see pretty often.

in c++11

They try to unify C++ syntax by introducng that stupid {} notation for constructors. C++ is a pile of poor design choices.

Hardcore version of the previous question:

#include
myFunc(std::vector test) { };
main ( )
{
myFunc( { 1, 2, 3 } );
};


DOES
IT
WORK?

>myFunc lacks return type

What do you expect when you have a language that wants to remain backwards compatible with a 70s programming language, while also declaring any use of those features evil and insisting you make a whole bunch of other constructs your main tools instead?

>main without return type
no senpai, how would this shit work

No. That's not C. It;s just C++ is totally crazy. Please leave C alone.

But templates

C is OK enough.
Maintaining C compatibility while doing all this other crazy shit is a large part of the problem.

main without return type is legit though

int* ptr = 0;
ptr = (int)(ptr)+(int)(ptr+(int)(ptr));

what is the value of ptr?

No. Here we're just talking about pure C++ syntax. It's pure C++ notation.

Illegal code.

And that notation is shit, because all of C++'s new syntactical features need to squeeze around the requirements of C compatibility. Get it?

the compiler didn't stop me

No. Initialization without = is pure C++. It has nothing to do with C, nothing. Dirtiness always come from C++, not C.

Your compiler should. The compiler in my head refuse to compile that illegal code.

I thought we were talking about initializer lists.