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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:57:34+00:00 2026-05-27T18:57:34+00:00

I have defined a custom assert macro. This works fine for all other comparisons.

  • 0

I have defined a custom assert macro. This works fine for all other comparisons. However, I get the compiler error:

ISO C++ forbids comparison between pointer and integer

when using the macro shown below (DWASSERT) to compare pointers as in the code below.

#define DWASSERT(condition,printstatement)  if(!condition){ printf(printstatement); assert(condition); }

#include <stdio.h>

int main()
{
    int target = 0;
    int* ptr1 = &target;
    int* ptr2 = &target;

    //Normal comparison works fine
    if(ptr1 == ptr2)
        printf("Equal");

    //Comparison using Macro generates compiler
    //error on the next line
    DWASSERT(ptr1 == ptr2, "Pointers not equal!\n");
    return 0;
}

While I can simply avoid using DWASSERT for this case, I am curious as to why this compiler error is generated.

  • 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-27T18:57:34+00:00Added an answer on May 27, 2026 at 6:57 pm

    The problem is that DWASSERT(ptr1 == ptr2, ... gets expanded to
    if(!ptr1 == ptr2){ printf(...

    Do you see what’s going on? !ptr1 == ptr2 is equivalent to (!ptr1) == (ptr2), and since !ptr1 is an integer type and ptr2 is a pointer type, you get your error.

    What you need to do to fix this is to change your macro definition to:

    #define DWASSERT(condition,printstatement)  if(!(condition)){ printf...
    

    Also, keep in mind that it is a bad idea to use printf the way you have, with an arbitrary string as a format. At some point somebody will give you a string with a % in it and things will break. You should use something like puts(x) or printf("%s", x).

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

Sidebar

Related Questions

I have defined a custom user type that works fine when used properties of
Early, I have defined custom section in Console application and it works fine. But
I have defined a custom error but if I test if custom error gets
In my ASP.NET web application, I have defined custom error pages in my web.config
I have defined a custom section in the App.config file and all the Configuration
I have defined 2 custom routes. One for threads/:id/:name and the other for threads/tags/:tagName
I have a defined a custom error domain that I want to publish in
I have defined a custom Authorization Attribute and it is automaticly applied to all
I have defined a delegate protocol in my custom class this way: #import <Foundation/Foundation.h>
I have defined a custom Sharepoint list for special attributes related to a software

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.