LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 01-13-2004, 08:36 AM   #1
xailer
Member
 
Registered: Nov 2003
Posts: 77

Rep: Reputation: 15
Question about function with variable number of parameters


hi

I saw an example of variadic function with optional parameter list and its parameter list ended with (char *)0

here is function's prototype

int execl ( const char *path , const char *arg , ... , ( char *)0 ) ;

In practice

execl( "ps" , "ps" , "-ax" , 0 ) ;

Zero gets cast to char * and pointer to char points to nowhere .

But why do we need to give to pointer NULL value to terminate parameter list ?

And why does this have to be pointer to char ? Why not (int *)0 ?

thank yo
 
Old 01-13-2004, 08:46 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
by the man page for exec
The const char *arg and subsequent ellipses in the execl,
execlp, and execle functions can be thought of as arg0,
arg1, ..., argn. Together they describe a list of one or
more pointers to null-terminated strings that represent
the argument list available to the executed program. The
first argument, by convention, should point to the file
name associated with the file being executed. The list of
arguments must be terminated by a NULL pointer.


i think that makes it reasonably clear, its taking arguments of type char * and the last one must be null, you should technically use the constant NULL rather than 0 because its value isnt necessarily 0.
 
Old 01-13-2004, 09:36 AM   #3
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

I imagine yo have to end with NULL cos argv[] is an array of pointers ending with NULL

If I was to make my own variadic function I could end parameter list however I choosed to ?

thank you
 
Old 01-13-2004, 09:57 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
yes, you can end it how you like as long as you can identify the last argument, look at the man page for stdarg to see an example.
 
Old 01-13-2004, 11:37 AM   #5
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
hi

thank you for your help
 
  


Reply



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
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
C++ / Passing a variable by reference to a class member function. sepulture Programming 12 11-15-2005 10:23 PM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
PHP Variable = function? wh33t Programming 2 01-22-2005 06:28 AM
Fast random number function Creep Programming 2 03-07-2004 09:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration