Live data from Hacker News

How to find size of an array in C without sizeof

arjunsreedharan.org

111–120 of 212 posts

Re: How to find size of an array in C without sizeof

#111

I'm surprised at all of the comments calling this stupid or pointless. The point is not that you should this trick in lieu of sizeof; the point is to shed light on a subtly of C arrays.

personally the issue i take with this article is that it displays an opinion that is counterproductive to learning (imo).

rather than calling out that pointer arithmetic implicitly relies on 'sizeof' in order to be useful, its treated like some kind of magic. i.e. i don't think it points out the not subtle but rather obvious connection, and instead distracts from it...

Re: How to find size of an array in C without sizeof

#112
post #82
post #15

Earlier quoted context omitted.

It's a complicated situation. There's a pointer to an array, and that pointer is dereferenced, resulting in an array (that then decays to a pointer). But that second array/pointer is not dereferenced. I'm not sure if it's legal.

Where is it dereferencing the array? *(&arr + 1) - arr That translates to taking the address one point past the array and subtracting the address of the array from it. It doesn't actually dereference the location past the end of the array. While: (&arr)[1] - arr might appear to be doing something different, it actually isn't.

&arr is a pointer to an array (it points to the existing array).

&arr + 1 is a pointer to an array that begins just after the existing array.

* is the dereference operator, so it seems to me that *(&arr + 1) dereferences the pointer to the array, resulting in an array (or a reference to an array), which then decays to a pointer.

Re: How to find size of an array in C without sizeof

#113
post #76

Earlier quoted context omitted.

>I think this article made a lot of people feel stupid I don't think so. Anyone with a solid understanding of C understands pointer arithmetic. I think the article isn't obvious only to those who have a weak understanding of the language.

There are many that have a weak understanding of the language. I got asked some years back why I defaulted to C in some interview questions -- I grew up with the language, understand the nuances and many of the implementations. It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem.

I don't think it /has/ to be C.

Having done even a semester of any type of assembly (not just part of a class) is probably enough. Other /low level/ languages like Forth (Imagine you /only/ had assembly, and wanted to build something a /little/ less painful) could probably work too.

Re: How to find size of an array in C without sizeof

#114
post #91

Earlier quoted context omitted.

> It's now possible to make your way through a university education in CS without ever touching or understanding C. This is a problem. I did not study CS, but I had a number of CS modules/classes. LaTeX was the only programming language I recall using. Students with better handwriting could probably get away with not doing any programming at all. It's not clear to me that this is a problem, but I imagine that the sys…

> I default to python in interviews even though it's my least favorite out of the languages that I use frequently. Why is that ? I would image that you'd use the language you are most comfortable with and trust the most during an interview ? What makes Python a good 'interview' language but a less good bread and butter language for you ?

I find that python requires the least boilerplate code out of the most common programming languages. I mostly dislike it on aesthetic grounds.

I really like the modern incarnations of C++ and statically typed ML-influenced, but not necessarily ML-derived, languages.

There are many criticisms that one could make about R, but I like some of its lispier features.

Re: How to find size of an array in C without sizeof

#115

I'm surprised at all of the comments calling this stupid or pointless. The point is not that you should this trick in lieu of sizeof; the point is to shed light on a subtly of C arrays.

personally the issue i take with this article is that it displays an opinion that is counterproductive to learning (imo). rather than calling out that pointer arithmetic implicitly relies on 'sizeof' in order to be useful, its treated like some kind of magic. i.e. i don't think it points out the not subtle but rather obvious connection, and instead distracts from it...

Your comment:

>rather than calling out that pointer arithmetic implicitly relies on 'sizeof'

Article:

>arr has the type int , where as &arr has the type int ()[size].

For me this is calling out the implicit use of sizeof by pointing out the type.

Re: How to find size of an array in C without sizeof

#116

Earlier quoted context omitted.

> I default to python in interviews even though it's my least favorite out of the languages that I use frequently. Why is that ? I would image that you'd use the language you are most comfortable with and trust the most during an interview ? What makes Python a good 'interview' language but a less good bread and butter language for you ?

If he's listing LaTeX as a programming language and talking about how little programming he had, I suspect he's stuck with R or Matlab.

TeX is a "real" programming language.

Re: How to find size of an array in C without sizeof

#118
post #96

Earlier quoted context omitted.

That's why it's called one in a million...

One in a million happens quite often if you're processing something like ~100k requests a second.

But it's extremely unlikely when you're processing 10 requests a week, such as might be the case for the web server in a consumer-grade router.

Re: How to find size of an array in C without sizeof

#119
post #30

Earlier quoted context omitted.

Quite. This exactly the sort of thing that makes C such a fun language.

I'm not sure if it's a praise for C though. Arcane design and lack of clarity might be fun to decipher, but it's not something that you'd want to see in the programming language.

For hobby programming I work the minimalism of C, but then I also enjoy assembler. If I'm doing something more task-oriented then I prefer to use languages like js or python.

Re: How to find size of an array in C without sizeof

#120
post #30

Earlier quoted context omitted.

I'm not sure if it's a praise for C though. Arcane design and lack of clarity might be fun to decipher, but it's not something that you'd want to see in the programming language.

For hobby programming I work the minimalism of C, but then I also enjoy assembler. If I'm doing something more task-oriented then I prefer to use languages like js or python.

I don't mean here to compare different classes of languages. Even within systems programming languages, C has a lot of things that could be (and are) done better today.
Post reply on HN