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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:59:14+00:00 2026-06-12T17:59:14+00:00

I am trying to organize my project by splitting commands up into separate files

  • 0

I am trying to organize my project by splitting commands up into separate files for easier maintenance. The issue I am having is trying to iterate over the array of commands defined at compile time. I have created a dumbed down example that reproduces the error I am getting.

.
├── CMakeLists.txt
├── commands
│   ├── CMakeLists.txt
│   ├── command.c
│   ├── command.h
│   ├── help_command.c
│   └── help_command.h
└── main.c

./CMakeLists.txt

PROJECT(COMMAND_EXAMPLE)

SET(SRCS main.c)
ADD_SUBDIRECTORY(commands)

ADD_EXECUTABLE(test ${SRCS})

commands/CMakeLists.txt

SET(SRCS ${SRCS} command.c help_command.c)

commands/command.h

#ifndef COMMAND_H
#define COMMAND_H

struct command {
    char* name;
    int   (*init)(int argc, char** argv);
    int   (*exec)(void);
};

extern struct command command_table[];

#endif

commands/command.c

#include "command.h"
#include "help_command.h"

struct command command_table[] = {
    {"help", help_init, help_exec},
};

commands/help_command.h

#ifndef HELP_COMMAND_H
#define HELP_COMMAND_H

int help_command_init(int argc, char** argv);
int help_command_exec(void);

#endif

commands/help_command.c

#include "help_command.h"

int help_command_init(int argc, char** argv)
{
    return 0;
}

int help_command_exec(void)
{
    return 0;
}

./main.c

#include <stdio.h>
#include "commands/command.h"

int main(int argc, char** argv)
{
    printf("num of commands: %d\n", sizeof(command_table) / sizeof(command_table[0]));
    return 0;
}

If you run this

mkdir build && cd build && cmake .. && make

the following error occurs

path/to/main.c:6:40: error: invalid application of 'sizeof' to incomplete type 'struct command[]'

So, how do I iterate over command_table if I can’t even determine the number of commands in the array?

I realize there are other posts out there with this same error, but I’ve spent a while now trying to figure out why this doesn’t work and continue to fail:

  • invalid-application-of-sizeof-to-incomplete-type-with-a-struct
  • invalid-application-of-sizeof-to-incomplete-type-int-when-accessing-intege
  • 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-12T17:59:15+00:00Added an answer on June 12, 2026 at 5:59 pm

    For your sizeof(command_table) to work, it needs to see this:

    static struct command command_table[] = {
        {"help", help_init, help_exec},
    };
    

    But it only sees this:

    extern struct command command_table[];
    

    Seeing that sizeof() can never figure out how many elements are actually in there.

    Btw, there’s another problem. static makes the array invisible in all other modules. You have to remove it or workaround it.

    Your options (after removing static) are:

    1. hard-coding the number of elements, e.g.

      extern struct command command_table[3];

    2. defining an extra variable to hold the number of elements:

    commands/command.c

    #include "command.h"
    #include "help_command.h"
    
    struct command command_table[] = {
        {"help", help_init, help_exec},
    };
    
    size_t command_count = sizeof(command_table)/sizeof(command_table[0]);
    

    commands/command.h

    ...
    extern struct command command_table[];
    extern size_t command_count;
    ...
    

    And then you just use command_count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to organize my php-project by puting some files into folders. When I
I am trying to organize a Zip Archive into an array so that I
I'm trying to organize a larger PHP project with more than 50 files in
Im trying to organize this kind of array array 0 => array 'id' =>
I'm trying to organize classes in a project in a fashion similar to: $TheRoberts
I'm trying to keep my Xcode project organized. I want to maintain separate folders
I have a bunch of divs I'm trying to organize here. The ones I'm
I have Python nested list that I'm trying to organize and eventually count number
I'm trying to jump through some hoops to organize data in a special way.
I am trying to find some information how to organize permissions for silverlight application

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.