Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:18:32+00:00 2026-05-30T00:18:32+00:00

here is some code class DengkleTryingToSleep{ public: int minDucks(int ducks[]); int temp(int ducks[]){ int

  • 0

here is some code

class DengkleTryingToSleep{
public:
int minDucks(int ducks[]);
int temp(int ducks[]){
int size=sizeof(ducks);
cout<<"sizeof="<<size<<"\n";
}
};


int main (int argc, const char * argv[])
{
DengkleTryingToSleep dt;
int arr[]={9,3,6,4};

cout<<"sizeof "<<sizeof(arr);
cout<<"\nsizeof from function "<<dt.temp(arr);

return 0; 
}

and output of this is

sizeof 16
sizeof from function sizeof=8

and i have no idea how this is working because it returns 16 (as expected when called inside main)
and returns 8 when called from the function

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:18:33+00:00Added an answer on May 30, 2026 at 12:18 am

    Actually this function:

    int temp(int ducks[])
    

    is exactly equivalent this function:

    int temp(int *ducks)
    

    There is NO DIFFERENCE at all. No difference. So no matter what you pass, whether an array or a pointer, it will become a pointer inside the function.

    That means, when you write sizeof(ducks) in your function, it is exactly equivalent to sizeof(int*), which returns 8 on your machine (I guess, your machine has 64-bit OS where the size of pointer is 8 bytes).

    If you want to pass an array, and don’t it decay into pointer type, then do this:

    template<size_t N>
    int temp(int (&ducks)[N])
    {
        int size=sizeof(ducks);
        cout<<"sizeof="<<size<<"\n";
    }
    

    Now it will print 16. Note that inside the function N represents the count of items in the array. So in your case, it would be 4, as there are 4 elements in the array. It means, if you need the length of the array, you don’t need to calculate it as sizeof(bucks)/sizeof(int), as you already know the length of the array which is N.

    Also note that there is a limitation in this approach: now you cannot pass dynamically allocated array:

    int *a = new int[10];
    dt.temp(a); //compilation error
    
    //but you can pass any statically declared array
    int b[100], c[200];
    dt.temp(b); //ok - N becomes 100
    dt.temp(c); //ok - N becomes 200
    

    But in C++, you’ve a better option here: use std::vector<int>.

    int temp(std::vector<int> & ducks)
    {
         std::cout << ducks.size() << std::endl;
    }
    
    //call it as
    std::vector<int> v = {1,2,3,4,5,6}; //C++11 only, or else : use .push_back()
    dt.temp(v);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's some code: class containerA {}; class containerB : public containerA { public: containerB
Here's some working code: class A { public $Foo; public function GetFoo() { $this->Foo
Here is some code: class Person def initialize(age) @age = age end def age
Here's some example code: class Obj attr :c, true def == that p '=='
Here's some code: class myclass { enum STAT { IDLE=0; READING, WRITING, PAINTING, SKATTING
I'm wondering the best form for my constructors. Here is some sample code: class
here is some php code: class A { private function action(){ echo 1; }
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
Here is some code I could not get to format properly in markdown, this
Here's some code I saw once. Can you see what's wrong with it? [updated]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.