93

I have seen in many posts that "in most of the cases array names decay into pointers".
Can I know in what cases/expressions the array name doesn't decay into a pointer to its first elements?

5
  • 2
    More context is required: Are you working in a specific language? Do you have an example?
    – abiessu
    Jul 19, 2013 at 18:23
  • consider C language. And i'm looking for an example where array names doesn't decay into pointer.
    – nj-ath
    Jul 19, 2013 at 18:25
  • 1
    @TheJoker I given here an answer in which I show this cases Jul 19, 2013 at 18:57
  • Re H2Co3's second point, i.e. with sizeof, I'm reading Head First C, and it first illustrates pointer decay using sizeof(msg) inside a function where msg was passed in as an argument. They had a little box explaining that an array variable decays to a pointer when it's passed into a function as an argument (paraphrasing) so you get 4 or 8 (bytes), not array size. I got confused because in the next chapter on the string library, they introduce strlen() and use it the same way they'd used sizeof(). I came here to straighten my head out and now you twisted it up a little more. :P
    – punstress
    Aug 6, 2013 at 0:36
  • This answer has all the exceptions with examples.
    – legends2k
    Jul 9, 2014 at 8:27

1 Answer 1

78

Sure.

In C99 there are three fundamental cases, namely:

  1. when it's the argument of the & (address-of) operator.

  2. when it's the argument of the sizeof operator.

  3. When it's a string literal of type char [N + 1] or a wide string literal of type wchar_t [N + 1] (N is the length of the string) which is used to initialize an array, as in char str[] = "foo"; or wchar_t wstr[] = L"foo";.

Furthermore, in C11, the newly introduced alignof operator doesn't let its array argument decay into a pointer either.

In C++, there are additional rules, for example, when it's passed by reference.

16
  • 2
    I'm sorry, but could you explain the 3rd case more clearly? I mean isn't the str in the third case used to refer to the memory location of the string? Hence a pointer again?
    – nj-ath
    Jul 19, 2013 at 18:38
  • 4
    @TheJoker No, that would be const char *str_ptr = "literal";. In my example, str is declared as an array, hence it is an array.
    – user529758
    Jul 19, 2013 at 18:39
  • 1
    3rd case is not a case. It's just one of the cases for array initialization syntax when it's an array of char. The right side of an array initialization statement doesn't even take an expression, so it doesn't make sense as an answer to this question.
    – newacct
    Jul 21, 2013 at 9:25
  • 1
    @newacct I suggest you read the relevant section of the C standard - literally these cases are enumerated. Furthermore, in an array initialization, if and only if the RHS is a string literal, then it is an expression (of array type), not an initializer list.
    – user529758
    Jul 21, 2013 at 9:53
  • 8
    @H2CO3: First of all, for initialization it specifically says "string literal". It does not say anything about "array expression" or "expression" at all. So the string literal syntax just happens to be used in two places: as the right side of an initialization, and also as an expression. That does not mean the right side of initialization is an expression. Plus, the question specifically asked about "array name", and a string literal is not an array name, and you cannot put an array name (or any other array expression except a string literal) on the right side of an initialization for array.
    – newacct
    Jul 22, 2013 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.