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 6182607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:13:42+00:00 2026-05-24T01:13:42+00:00

I have a variable called names which is a two dimensional array. I declared

  • 0

I have a variable called names which is a two dimensional array.
I declared it like below so that there will be maximum of 5 names and each name will consist of 256 characters.

int main(){
    char names[5][256];
    strcpy(names[0],"John");
    strcpy(names[1],"Matt");
    printf("%d\n",sizeof(names)/256);        //this prints out 5 which is correct
    passit(names);             
}

void passit(char names[][256]){
    printf("%d\n",sizeof(names)/256);        //this prints out 8 which seems to be a size of pointer
} 

How should I change this so that in passit function, number of elements will be printed correctly?

  • 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-24T01:13:42+00:00Added an answer on May 24, 2026 at 1:13 am

    Your code is C-style. If you’re really writing C++ then I suggest using std::cout, std::vector and std::string:

    void passit(std::vector<std::string> const& names) {
        std::cout << names.size();
    } 
    
    int main() {
        std::vector<std::string> names(5);
        names[0] = "John";
        names[1] = "Matt";
    
        std::cout << names.size();
        passit(names);             
    }
    

    Otherwise, you can perform an intermediate fix to your existing code.

    As you’ve spotted, when you try to pass an array by value it actually decays to a pointer (char(*)[256] in this case) and you lose all information on the size of the first dimension.

    Passing the array by reference avoids this, except that you now have to specify the full array type including all array dimensions; template argument deduction then makes this automatic:

    template <size_t N>
    void passit(char (&names)[N][256]) {
        printf("%d\n", N);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a global names variable, which looks like that char* names[NAMES_CAP];
I have a huge dictionary of blank values in a variable called current like
I have a paragraph of text in a javascript variable called 'input_content' and that
I have a variable that is built in loop. Something like: $str = ;
Summary: Say I have a project in Django called devsite which will be deployed
I have a class Topic and Group which both have a variable called 'name',
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have a variable of type Number, and i like to obtain the sign
I have a variable that contains a 4 byte, network-order IPv4 address (this was
If I have a variable in C# that needs to be checked to determine

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.