LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c: define constants (https://www.linuxquestions.org/questions/programming-9/c-define-constants-475335/)

kpachopoulos 08-19-2006 05:36 AM

c: define constants
 
Hi,
i would to declare the return values as constants. However, the problem is that i have to return pointers to the constants. How do i do that? Adding the "&" operator on the return values causes warnings, too. And "#define *IS_PROC_ID 1" and similar don't seem to be solutions.
Ideas?

Thanks,
Kostas

Code:

#define IS_PROC_ID        1
#define NOT_PROC_ID        0

int *selector(const struct dirent* d)
{               
        char* dir_name=d->d_name;               

        if ((d->d_type==DT_DIR) && (is_number(dir_name)==1))
                return IS_PROC_ID;
        else               
                return NOT_PROC_ID;       
}

Code:

res.c: In function ‘selector’:
res.c:77: warning: return makes pointer from integer without a cast


cupubboy 08-19-2006 06:35 AM

Umm .. you can't do that

Also I can't understand why you're returning a pointer in that example .. but perhaps that is just for example's sake

Theese defines simply get substitutet in the code .. so when you write

Code:

return IS_PROC_ID;
The preporccesor translates that to

Code:

return 1;
So one wa you could do that would be

Code:

int * x()
{
    //Some stuff

    int * p = new int(IS_PROC_ID);
    return p;
}

Don't forget to delete p

Also I hope that the code you posted is just an example .. since it makes no sense returning a pointer in there

introuble 08-19-2006 07:53 AM

Code:

    int * p = new int(IS_PROC_ID);
That's C++. The thread title is: "c: define constants".

Code:

#define IS_PROC_ID        1
#define NOT_PROC_ID        0

int *selector(const struct dirent* d)
{               
        char* dir_name=d->d_name;               

        if ((d->d_type==DT_DIR) && (is_number(dir_name)==1))
                return (int *)IS_PROC_ID;
        else               
                return (int *)NOT_PROC_ID;
}

Try that.

demon_vox 08-19-2006 10:44 AM

Hi,
I would just erase the little astericks at the begginning of the function, and just return an int value. It would be much much happier (as mentioned before).

Cheers!

kpachopoulos 08-19-2006 11:58 AM

Thanks everybody. I must use the specific method declaration.
introuble: this works OK.

xhi 08-19-2006 03:31 PM

Quote:

Originally Posted by nocturna_gr
Thanks everybody. I must use the specific method declaration.
introuble: this works OK.

do you realize that
Code:

if ((d->d_type==DT_DIR) && (is_number(dir_name)==1))
                return (int *)IS_PROC_ID;
        else               
                return (int *)NOT_PROC_ID;

is returning a ptr from the function that is either going to be null, or is going to point to address 1 and segfault?

as cupubboy said, the #define'd name is getting replaced with the value. so

return (int *)IS_PROC_ID;

is the same as saying

return (int *)1;

which does not segfault there, or give warnings.. but try to print the value of the returned pointer.

printf("%d", *(selector(...)));

segfault because you are trying to access memory at address 1. cant do it. and if you are going to use the actual value of the ptr (which would work here) why not just use an int value and have the code be readable.


All times are GMT -5. The time now is 04:49 PM.