Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Anytime I write C code, I try to keep the asterisk close to the type:

void* foo(void* bar);

I know that the asterisk technically belongs with the variable name when declaring it, but it helps to wrap my head around them, as a reminder that they're a pointer type.




I agree you based on fact that more in code we use

    void *s = then use it as 
    s=
But I agree more that the

    void* s = then use it as 
    s=
Is more understandable as the first set confuse me when I learn pointer. It still does. void* is a type like statement. And void is like a return type statement.


The only thing that sucks hard is when you do :

    int* s1, s2;
here s2 is an int, not an int* and you have to do

    int* s1, *s2;
this is braindead imho


As long as you're not using prehistoric versions if C, you don't have to define all variables up front, so there is much less justifiable reasons for that kind of multiple declaration.


Except nothing will save you from the "one true way to write C" when you try to write a declaration of a pointer to a function, for example.


You can adjust your function pointer typedefs if you like. e.g. signal(2) http://man7.org/linux/man-pages/man2/signal.2.html

  typedef void (*sighandler_t)(int);
  sighandler_t signal(int signum, sighandler_t handler);
can be rewritten as

  typedef void sighandler_t(int);
  sighandler_t *signal(int signum, sighandler_t *handler);


yep, I prefer the c++ way by far : using foo = void(int, float);


This totally destroyed me when I was learning C. The theory was:

    int *foo;
makes more sense, because to access the int, you write:

   *foo = ...;
But that's not an int, it's a crash! If you want an int, you have to make one.

Placing the asterisk next to the type made it all click for me.


    double foo;
    compute(foo);
That's not even a crash, just silent garbage.

I don't know how you expect to do better with an uninitialised variable (except ints that default to 0).

foo dereferences to an int, but what could you hope to happen if you never gave it some memory to point to?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: