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

  • SEARCH
  • Home
  • 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 3322342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:10:14+00:00 2026-05-17T23:10:14+00:00

Not sure what I’m doing wrong here. I have a struct that is used

  • 0

Not sure what I’m doing wrong here. I have a struct that is used heavily through my program.

typedef struct _MyStruct {
  // ... handful of non-trivial fields ...
} MyStruct;

I expect (read, intend) for lots of parts of the program to return one of these structs, but many of them should be able to return a “null” struct, which is a singleton/global. The exact use case is for the implementing function to say “I can’t find what you asked me to return”.

I assumed this would be a simple case of defining a variable in a header file, and initializing it in the .c file.

// MyStruct.h

// ... Snip ...

MyStruct NotFoundStruct;

–

// MyStruct.c

NotFoundStruct.x = 0;
NotFoundStruct.y = 0;
// etc etc

But the compiler complains that the initialization is not constant.

Since I don’t care about what this global actually references in memory, I only care that everything uses the same global, I tried just removing the initialization and simply leaving the definition in the header.

But when I do this:

MyStruct thing = give_me_a_struct(some_input);
if (thing == NotFoundStruct) {
  // ... do something special
}

Th compiler complains that the operands to the binary operator “==” (or “!=”) are invalid.

How does one define such as globally re-usable (always the same memory address) struct?

  • 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-17T23:10:15+00:00Added an answer on May 17, 2026 at 11:10 pm
    // MyStruct.h
    
    typedef struct _MyStruct {
      // fields
    } MyStruct;
    
    extern MyStruct NotFoundStruct;
    
    // MyStruct.c
    
    #include "my_struct.h"
    MyStruct NotFoundStruct = {0};
    

    But since you can’t use the == operator, you will have to find another way to distinguish it. One (not ideal) way is to have a bool flag reserved to indicate validity. That way, only that must be checked to determine if it’s a valid instance.

    But I think you should consider James’s proposed solution instead

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

Sidebar

Related Questions

Not sure what I'm doing wrong, but here is the code 1: var currDoc:NotesDocument
Not sure what I'm doing wrong here. Just trying to make it a modal
Not sure what I am doing wrong. The results I get from the Accelerate
Not sure if anyone has experience this issue. I have a TextBox that I
not sure what i'm missing, but here ti go's: i have a floating left
Not sure what parallel programing means... but I have two thoughts of that Process
Not sure what's wrong here. I'm using the built-in comment form. The form tag
Not sure I'm in the correct here. I have a stored procedure in MySQL,
Not sure if I can word this right but here we go. I have
Not sure whats going on here, or what could be the integer in this

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.