3

When you create an array and pass it into a function, the array decays into a pointer. But what happens if I create a class like this one:

class A
{
public:
    int array[5];
};

Now if I pass an instance of class A into a function, will A.array decay into a pointer or not?

void f(A a)
{
    //has a.array decayed?
}

That brings me to my next question: Are there any difference swith the array inside A when passing a reference to class A or a pointer to class A?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
MivVG
  • 679
  • 4
  • 16
  • *Not*; and I'm almost curious why you think it would. Regarding your second question, there are *many* questions on SO concerning the differences of pointers and references, including parameter differences. I'm surprised you didn't find even one ([like this one](https://stackoverflow.com/questions/620604/difference-between-a-pointer-and-reference-parameter)). – WhozCraig Mar 18 '18 at 08:08
  • One question per question please. Just adding the second (not directly related) one makes your post too broad. And it's even a duplicate question in and of itself. – StoryTeller - Unslander Monica Mar 18 '18 at 08:08
  • Arrays only decay to pointers in contexts where pointers are expected or an array is not valid and a pointer is. For example, assigning an array to a pointer, or saying `array++`. – juanchopanza Mar 18 '18 at 08:12
  • About then second question, I meant "are there any differences with the array inside A when..." I'm sorry if that wans't clear. @WhozCraig Why wouldn't I think so? In it's essence, it's still an array that is passed into a function, even though it is wrapped in a class. – MivVG Mar 18 '18 at 08:15
  • No, it's a class object that's passed into a function. There's no "mere wrapping". It's a different type altogether than an array. – StoryTeller - Unslander Monica Mar 18 '18 at 08:19
  • Ok. I guess that answered my question, thanks. – MivVG Mar 18 '18 at 08:20
  • 2
    An *expression* of type "array of T" 'decays' to "pointer to T". The technical term is not decay but type adjustment. A is not an expression of an array type, there's nothing to adjust here. – n. m. could be an AI Mar 18 '18 at 08:32
  • @n.m. Type adjustment is the technical term for type adjustment, which is not the same as decay. Not sure type adjustment applies to expressions. – juanchopanza Mar 18 '18 at 08:46
  • @juanchopanza my bad, this is not type adjustment but array-to-pointer implicit conversion. – n. m. could be an AI Mar 18 '18 at 09:44
  • Arrays do not decay into pointers. The **name** of an array decays into a pointer to its first element in most contexts. If you don't use the name there's nothing that can decay. – Pete Becker Mar 18 '18 at 16:22

0 Answers0