hi,
i would like to evaluate some value in C.

ex

#define OK 1

int main()
{
  char *ptr;
  ptr=(char *)fun();
  if(*ptr)
   printf("SUCCESS");
}

here fun() returns char * and stored in ptr. i would like to check weather ptr is OK or not, i do't like to use strcmp() since, i would like do strcmp() for 100 message. But i would like to check weather its in #define or not. how to do? please help me.

Recommended Answers

All 4 Replies

If the pointer is not okay, it should be NULL. Otherwise you have no choice but to always return a valid pointer and test the contents, which you've already stated isn't desirable:

#include <stdio.h>

char *fun(void)
{
    char *result = NULL;

    /* Allocate and populate result, or leave NULL on error... */

    return result;
}

int main(void)
{
    char *ptr = fun();

    if (ptr != NULL)
        puts("SUCCESS");

    return 0;
}

hi, i think u did't understand my problem, the fun() retrun char *. the char * contain the string for example
*ptr = "OK"

i would like to check OK is present in #define or not. if its present then print success.

how to compare the string with #define...

:)

>i think u did't understand my problem
I understood your problem to be nonsensical and impossible with the given restrictions. My previous post gave you the answer you needed rather than the one you wanted.

>how to compare the string with #define...
#define a string literal and use strcmp, the macro symbol itself is lost after compilation, so you can't use it at runtime:

#define OK "OK"

int main(void)
{
    char *ptr = fun();

    if (strcmp(ptr, OK) == 0)
        puts("SUCCESS");

    return 0;
}

Oh wait, you don't want to use strcmp. I guess you're SOL. :icon_rolleyes:

hi r u got idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.