Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

int* p vs. int *p

Name: Anonymous 2009-10-29 0:18

What do you think about the difference between the following declarations? Which do you prefer?

int* p;
int *p;

It's often said the first is C++ style and the second is C style. I prefer C but conceptually the first declaration makes a lot more sense to me. You're defining a variable p of type int*. p is an int*. You dereference it, *p, when you want the int. Makes sense. The second example to me seems like gobbledygook, but it's the way the C authors thought of it, because of this bullshit:

int *p, *q;

So what the fuck is the deal? Can someone who uses "int *p" syntax explain to me what goes through your head when you write that garbage?

Name: Anonymous 2009-10-29 15:56

When I see "int *p;" i think "when i dereference p I get an int" or more generally "p gets dereferenced to an int"

Analogously "int factorial(int)" means "factorial gets evaluated with the arguments (int) and becomes int"

And by the same reasoning something like "int *(*q)[5](char *)" (which would confuse 99% of the "HURR, int* p" people) means "when I dereference q I get an array of 5 functions which when evaluated with an 'char *' argument, I will be left with a pointer to an int"

Name: Anonymous 2009-10-29 15:59

int* 4 lyfe.

Name: Anonymous 2009-10-29 16:01

>>35
C is about reification of memory layout, register specifics etc.

Please stop using words you don't know the meaning of. Thank you.

Name: Anonymous 2009-10-29 16:12

>>5
http://www.antlr.org/wiki/display/CS652/How+To+Read+C+Declarations

By the way! This is a nice explanation, but shit like this:
int *func_returning_ptr_to_int();
Makes me FUQIN RAGE! You forgot your void, moron! It is C, not C++! You never, ever, ever declare a function with empty arglist, that's just asking for a well-deserved trouble, bitch!

Also, he could have mentioned whence the "look right, then look left" rule comes: it's because the only two operators allowed in declarations are '*' and '[]', and '[]' has higher precedence and goes to the right.

Name: Anonymous 2009-10-29 16:26

>>44
int *func_returning_ptr_to_int();

Invalid C, the function body is missing.

Name: Anonymous 2009-10-29 16:27

>>44
Get with the times, using the empty lists to specify no arguments is valid C99.

Name: Anonymous 2009-10-29 16:30

>>45
your moran
>>46
You missed the point. Empty arglist is the original way for doing varargs and so calling a function thus declared with any number of any parameters is valid C99, and even -Wall won't say anything.

Name: Anonymous 2009-10-29 16:51

HAXF MY ANUS

Name: Anonymous 2009-10-29 17:27

>>44

ENTERPRISE QUALITY

Name: Anonymous 2009-10-29 17:28

>>44
You never, ever, ever declare a function with empty arglist, that's just asking for a well-deserved trouble, bitch!

What trouble could that possibly cause?  pls explain

Name: Anonymous 2009-10-29 18:06

>>50 - >>47
The trouble? I had a function with one argument, I got rid of it, patched the calls I knew of and it compiled with no errors or warnings. That's what static typing is for: you refactor, recompile and if it goes through, then you have this warm fuzzy feeling that you didn't break anything, right? Not in this case though.

Bonus OMG WTF IS HAPPENING points for trying to import this function from a dynamically linked library, not being sure that there aren't name clashes, changing the signature and... well, you don't want to experience this, that's why you should ALWAYS WRITE FUQIN (void)!!1

Name: Expert C/C++ Programmer 2009-10-29 18:08

>>50
It leaves the argument list unspecified. To actually make it explicit that your function takes no args, you should instead write foo(void).

Name: Anonymous 2009-10-29 18:14

>>50
#include "VALID_C_CODE.h"

int i_am_bleeding();

int main(int argc, char **argv)
{
    printf("I'd rather be surfing\n");
   
    /* Unrelated, but some tutorials say that it's okay to write void main(),
    which lets you leave out the return statement. Don't do that, either. */
    return 0;
}

int i_am_bleeding(int this_function, int actually_takes, int several_arguments, int you_talentless_hack)
{
    if (you_actually_knew(C)) {
        you_would_already_understand_this();
    }
   
    return to("/b/", PLEASE);
}

Name: Anonymous 2009-10-29 18:22

>>53
int main(int argc, char** argv) { return 0; }

FIXED

Name: Anonymous 2009-10-29 18:22

>>53
STACK MY ANUS

Name: Anonymous 2009-10-29 18:32

The left-to-right-readable separated type concept is cleaner and easier to read, but that's not how C does it, and you're doing everyone a disservice if you try to pretend it is. It breaks completely for anything more complicated than pointers.

C defines 'em as it dereferences 'em, and if ya fancy yerself to be a C pror'mmer, that's the way ye'll do it and ye'll like it!

Name: Anonymous 2009-10-29 18:44

★That was VIP quality!★

Name: Anonymous 2009-10-29 18:50

>>37
You have been told?
Must resist the urge to scream at you for being an idiot, and resultantly BT.

Name: Anonymous 2009-10-29 18:57

You have binch rolled

Name: Anonymous 2009-10-29 19:32

>>38
>>58
ok, so i googled it and found out it meant you have been trolled. and guess which site was number 1? enyclopedia dramatica (a site run by 4chan).  obviously no one else uses it that way.  it should be YHBTr at least to avoid confusion.

i honestly dont get why you have to use secret acronyms.  anonymous web board were invented so they would be all inclusive, making up acronyms makes it exclusive because i dont know what you mean.  this is the reason i left reddit, and anonymous boards are supposed to avoid that

Name: Anonymous 2009-10-29 19:34

>>60
This is just a graveyard.  4chan died as soon as the "Anonymous is legion" shit started.

Name: Anonymous 2009-10-29 19:55

I honestly dont get why you have to use secret acronyms.
YHBT is a very old and well known acronym and could hardly be classified as secret.
making up acronyms makes it exclusive because i dont know what you mean
Firstly, making up acronyms is fun and has led to many interesting diversions of topic on /prog/
Secondly, it is traditional to lurk and become acquainted with a board's culture (memes,acronyms,use of text for artistic purposes, taboo themes) before posting and the current trend of just ignoring all the board culture and expecting everyboard to behave the same is the reason that the majority of 4chans boards have went down the shitter.
this is the reason i left reddit, and anonymous boards are supposed to avoid that
While I'm glad you've came to your senses about reddit and similar sites (I'm guessing from the way you post you've read shii's essay?), it is, unfortunately, folly to assume that these ideals still hold (see /b/ for details)

Name: Anonymous 2009-10-29 20:08

>>62
link to said essay

Name: Anonymous 2009-10-29 20:12

>>2
>>33
OP here, and that's the problem, you haven't defined anything named *p. *p is a compound expression. You've defined p.

You certainly haven't defined any int; the int it points to need not even exist.

>>4
Obviously I know variable declarations are read right-to-left. It doesn't magically explain why the space is in that arbitrary position. What about "p is a pointer to an int" says that the space goes between the * and int? It's not the "to a", because then why would you write "int **p"?

However if you write "int* p", then it does make sense, because in "p is a..." the "is" is the explanation for the space; it separates the name you're defining from its complete type.

>>3
What the fuck is your post supposed to mean? Do you mean "int* p, q;" is confusing? When you know the standards there's nothing confusing about it, but writing that on purpose in my opinion is deliberately misleading; it's like using the wrong indentation. I'm obviously not suggesting you use the C++ style in defining multiple variables in the same declaration; you are pretty much forced to declare them separately in this case, otherwise the declaration looks wrong (as in >>10).

Did you mean "int* p,q" would be confusing if it worked as you would expect from the spacing? Because if it did, there would be nothing confusing about it (and even less so when you add type modifiers; if it worked this way, stupid expressions like "const int* p, *const q = r;" would actually be invalid. As much as I like C, this is another good reason why the C convention is fucking stupid.)

>>16
Um, this has the exact same problem. Did you know the style "typedef int *PINT" is pervasive throughout the Win32 headers?

>>21
It's not duplicate syntax; it's just whitespace. You can write int*x if you want to.

>>23
Yes, the convention makes this possible, but the compound declaration rules were made because the convention existed in the first place. What was the reasoning behind them then?

>>30
Are you going to explain then why "int *p" makes sense? Because I have yet to see a rational explanation for it.

Name: Anonymous 2009-10-29 20:17

>>63
esr, jargon file, google.

Name: Anonymous 2009-10-29 20:19

Did you know the style "typedef int *PINT" is pervasive throughout the Win32 headers?

This ugly bullshit is the singular reason I loathe working with the Win32 API.

lptstrRetardedVarNames

Name: Anonymous 2009-10-29 20:20

>>65
Which essay are you thinking off? How to be a hacker (or whatever it's called)?
I thought he meant this http://wakaba.c3.cx/shii/shiichan (I think there is a longer one, but I can't really remember)

Name: Anonymous 2009-10-29 20:21

>>60
fuck you, lazy faggot.
YHBT & IHBT are very very common.
we have much more esoteric ones, ones that you will only ever see on /prog/ such as SPAWHBTC and NYJMUA.
but regardless of how secret their meaning seems to outsiders, it can easily be found out by just lurking or with a little googlefu.
IHBT

Name: Anonymous 2009-10-29 20:22

OP here again, I didn't notice there was a page two...

>>41
When I see "int *p;" i think "when i dereference p I get an int" or more generally "p gets dereferenced to an int"
This is the best explanation I've seen, but it still seems silly to me. Isn't this hard to visualize when you have to do pointer arithmetic? Why not abstract away the dereferencing of it?

It also breaks down if you write "int **p". Do you think "When I dereference p I get something that when I dereference it I get an int"? That seems a very convoluted way of understanding p.

>>56
The complexity is irrelevant; it doesn't break at all unless you try to define multiple variables in one declaration.

Name: Anonymous 2009-10-29 20:23

>>1-
NYJMUA & YHABT & IHBT fuck

Name: Anonymous 2009-10-29 20:24

This thread makes me mad inside my head. Why do we have to discuss this again.

Name: Anonymous 2009-10-29 20:24

>>67
Essay confirmed on "Everything Shii knows"

Name: Anonymous 2009-10-29 20:26

>>71
The same reason we have every other thread at least three times, we're boring people who like to rehash the same garbage

Name: Anonymous 2009-10-29 20:26

>>69
Why not abstract away the dereferencing of it?
if you like abstraction, then don't use C.
pointer arithmetic is fun.

Name: Anonymous 2009-10-29 20:29

POINTERS ARE HARD

Name: Anonymous 2009-10-29 20:30


#include <stdio.h>
#define a(b) char*(b)(){return #b ;}
#define k z[(int)(x++)]
#define g(r) z[x-(r)]
char x=0,*(*z[0xa])();
a(d)
a(r)
a(w)
a(o)
a(l)
a(e)
a(h)
int main() { k=d;k=l;k=r;k=o;k=w;k=g(2);k=g(5);k=g(1);k=e;k=h;for(;--x>-1;x-1=\
=5?putchar(2<<(--x-1)):x--)printf(k());putchar(sizeof(z)/sizeof(void*));return\
 0;}

Name: Anonymous 2009-10-29 20:36

>>76

#include <stdio.h>
int main() {
printf("hello world");
return 0;
}

Name: Anonymous 2009-10-29 20:39

>>77
gtfo

Name: Anonymous 2009-10-29 20:42

>>76
Pretty sad. I could compile and run that mentally.

Try harder.

Name: Anonymous 2009-10-29 20:46

>>79
COMPILE MY ANUS

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List