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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:16:38+00:00 2026-06-15T06:16:38+00:00

So what I have is a file main.c which is including a header file

  • 0

So what I have is a file main.c which is including a header file I made utils.h, containing forward references to functions in my source file utils.c

In utils.c:

I have a function that accepts an array of string as an argument, and prints it out as a menu:

void showMenu(const char *menu[])
{
    int menulen = sizeof(menu)/sizeof(*menu);

    int i;
    for(i = 0; i < menulen; i++)
    {
        printf("[%d] .. %s\n", (i+1), menu[i]);
    }
}

In main.c:

I simply call this function:

const char *menu[] =
{
        "Customers",
        "Orders",
        "Products"
};

int main(void)
{
    showTitle("Customer Orders System");
    int menulen = sizeof(menu)/sizeof(*menu);
    showMenu(menu);
    getch();
}

My Problem:

My showMenu function is calculating the length of the array, and then iterating through it, printing the strings. This used to work when the function was in main.c, but I am required to organize this project in separate files.

The length is now being calculated as 1. After doing some debugging, I think this is a pointer-related problem, but I seem to resolve it. The argument for showMenu after the call is of type

const char** menu

having only the first element of my original array.

I tried deferencing the argument, passing it a pointer of the array, and both at the same time.
Strangely enough, the same line of code works in the main function.
I really don’t want to have to resolve this problem by adding a length of array argument to the function.

Any help is greatly appreciated.

  • 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-06-15T06:16:39+00:00Added an answer on June 15, 2026 at 6:16 am

    This is because arrays decay into pointers to their first element when passed to a function like yours, and there is no information retained about the number of elements. In the scope where the array is declared, this decay hasn’t happened, so sizeof works.

    You must either add length of array as an extra argument, or make sure the array is terminated by an appropriate sentinel value. One popular such value is NULL, i.e. you make sure the last valid index holds a string pointer whose value is NULL, which then indicates “no more data, stop”:

    const char *menu[] =
    {
        "Customers",
        "Orders",
        "Products",
        NULL /* This is a sentinel, to mark the end of the array. */
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an object file ( source.o ) without function main . a
I have a page (main.php) which loads content from an external PHP file (rpc.php).
I have a code that looks like this: int main () { fstream file;
I have implemented below function in a separate .js script file which I load
I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html I want to
I have a xml file main.xml with following markup and data. main.xml <xml> <content>
In my main file I have an array of character strings, char names[320][30], and
Suppose I have a Javascript file function js_main(args){ /* some code */ var x
I have a situation where my main SWF file loads many external SWF files.
So I have a main.py file inside /home/richard/projects/hello-python directory: import sys sys.path.append('/home/richard/projects/hello-python') from Encode

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.