LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-19-2006, 06:36 AM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Rep: Reputation: 30
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
 
Old 08-19-2006, 07:35 AM   #2
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
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
 
Old 08-19-2006, 08:53 AM   #3
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
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.
 
Old 08-19-2006, 11:44 AM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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!
 
Old 08-19-2006, 12:58 PM   #5
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 704

Original Poster
Rep: Reputation: 30
Thanks everybody. I must use the specific method declaration.
introuble: this works OK.
 
Old 08-19-2006, 04:31 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Variables Won't Constants Aren't: LXer Syndicated Linux News 0 07-26-2006 05:33 PM
define jhon Linux - Networking 2 08-30-2004 08:42 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 02:08 PM
How do you write binary literal constants in C? vasudevadas Programming 5 01-18-2004 08:39 AM
using #define Jo_Nak Programming 4 06-11-2003 02:46 PM


All times are GMT -5. The time now is 08:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration