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

> eg: how many people believe that "all arrays are just pointers," in C? When is an array not like a pointer?

Ok I know this wasn't your point but I got stuck on this and am just curious to know: what you were thinking of here? One thing that occurs to me is that in a recursive function you will run out of stack space a lot faster if you are using arrays rather than allocating from the heap. I don't think that is what you were referring to though, hence the question.



Well my example was meant to support how programming languages can create a cognitive dissonance between what the programmer thinks will be executed vs. what is actually executed.

One area where I think there is a high dissonance is in how C uses the same syntax for defining arrays, indexing into arrays, and referencing pointer offsets. Other examples are the array decomposition rules, array function parameters, etc. Even experienced programmers get tripped up by them:

http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr


If you're asking about differences between pointers and arrays, one example is:

  int a[10] = { 7 };
  int* p = a;
  
  assert(a[0] == p[0]);
  assert(sizeof(a) == sizeof(p)); // FAILS




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

Search: