0
struct val{
int first;
int second;
};
int maxChainLen(struct val p[],int n);
int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        val p[n];
        for(int i=0;i<n;i++) {
            cin>>p[i].first>>p[i].second;
        }
         cout<<maxChainLen(p,n);
    }
    return 0;
}


int maxChainLen(struct val p[],int n){
    int x=sizeof(p);
    cout<<x<<endl;
    return 0;
}

Input
1
5
5  24 39 60 15 28 27 40 50 90

Here I am simply finding the sizeof of array p inside maxChainLen() method, which ideally should be 5*size of p[0], ie- 5*8 = 40bytes. But sizeof(p) and sizeof(p[0]) both are displaying 8. I know this is very basic but I have no idea why sizeof() is giving wrong results here.

Olivia Pearls
  • 139
  • 1
  • 11

1 Answers1

1

The syntax int maxChainLen(val p[], int n) is equivalent to: int maxChainLen(val* p,int n) - the function actually accepts a pointer to val.
When you call this function, the array you pass decays to a pointer.

You can also see this answer for exact standard-backed wording.

Igor R.
  • 14,716
  • 2
  • 49
  • 83