6 1273
Alex Vinokur said:
------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}
-------------------
How can func() use its arguments 100, 200, 300?
It can't use the arguments, because it doesn't take any parameters.
Modify func so that it takes parameters. For example:
void func(int x, int y, int z)
{
/* you can now refer to x, y, and z within func */
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
"Richard Heathfield" <in*****@invalid.invalidwrote in message news:Q5********************@bt.com...
Alex Vinokur said:
------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}
-------------------
How can func() use its arguments 100, 200, 300?
It can't use the arguments, because it doesn't take any parameters.
But that program is valid C-program.
Why does C allow that?
>
Modify func so that it takes parameters. For example:
void func(int x, int y, int z)
{
/* you can now refer to x, y, and z within func */
}
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
Alex Vinokur said:
"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:Q5********************@bt.com...
>Alex Vinokur said:
------ foo.c ------
void func() {}
int main()
{
func (100, 200, 300);
return 0;
}
-------------------
How can func() use its arguments 100, 200, 300?
It can't use the arguments, because it doesn't take any parameters.
But that program is valid C-program.
Well, it's a C program that doesn't require the implementation to produce
any diagnostic messages. That isn't necessarily the same thing!
Why does C allow that?
Clearly func(100, 200, 300) is not a syntax error, since we can readily
imagine situations where we know it to be valid. Furthermore, the absence
of a prototype means that the constraint that the number of arguments must
match the number of parameters does not apply. Freely translated, this
means "it's your gun, and it's your foot - if it's really what you want to
do, fire away".
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Richard Heathfield skrev:
Alex Vinokur said:
void func() {}
[...]
How can func() use its arguments 100, 200, 300?
It can't use the arguments, because it doesn't take any parameters.
Way too easy for R.H., now can he explain this:
(*(void(*)())0)();
?
--
Tor <torust AT online DOT no>
Tor Rustad said:
Richard Heathfield skrev:
>Alex Vinokur said:
void func() {}
[...]
How can func() use its arguments 100, 200, 300?
It can't use the arguments, because it doesn't take any parameters.
Way too easy for R.H., now can he explain this:
(*(void(*)())0)();
Sure. It's line noise. Or, if you prefer, undefined behaviour. Std quotes
are from n1124:
6.3.2.3 Pointers:
"An integer constant expression with the value 0, or such an expression cast
to type void *, is called a null pointer constant. If a null pointer
constant is converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer to any object
or function."
Therefore, (void(*)())0 does not point to a function, and yet has function
pointer type. Thus, (*(void(*)())0)() is attempting to call a function that
does not exist.
6.5.3.2(4) says: "If an invalid value has been assigned to the pointer, the
behaviour of the unary * operator is undefined."
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Richard Heathfield skrev:
Tor Rustad said:
[...]
Way too easy for R.H., now can he explain this:
(*(void(*)())0)();
Sure. It's line noise. Or, if you prefer, undefined behaviour.
Std quotes are from n1124:
6.3.2.3 Pointers:
"An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.
If a null pointer constant is converted to a pointer type, the
resulting pointer, called a null pointer, is guaranteed to compare
unequal to a pointer to any object or function."
Well done! :-)
Therefore, (void(*)())0 does not point to a function, and
yet has function pointer type.
So, as Mr. Heathfield correctly points out, the statement is a cast to
a function pointer, and then a function call:
typedef void (*fp) ();
( *(fp) 0) ();
Thus, (*(void(*)())0)() is attempting to call a function that does not exist.
"I once talked to someone who was writing a C program that was
going to run stand-alone in a small microprocessor. When this
machine was switched on, the hardware would call the subroutine
whose address was stored in location 0."
- Andrew Koenig, "C Traps and Pitfalls"
--
Tor <torust AT online DOT no> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marcus |
last post by:
Quick question on how I setup my mysql database(s)...
If my setup is such that I have multiple clients, and each client gets
10 tables to store their data, is it better for performance if I put...
|
by: Mark Hahn |
last post by:
"Michael Geary" <Mike@Geary.com> wrote ...
>Does anyone have some sample code where obj$func() would be used?
> (Apologies if I missed it.)
There have been so many messages about delegation...
|
by: modemer |
last post by:
I saw someone use the following code:
void func(MyClass *& myCls)
{
myCls->test();
}
// call func():
func(new MyClass);
|
by: Vinko Vrsalovic |
last post by:
Hello,
I read a couple of posts ago that these are not equivalent, and that
the former means 'no arguments specified' and that calling for
instance func(3,"THREE",&myint) is legal.
What's the...
|
by: Robert Palma |
last post by:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.
Declaring array as: double xx;
Declaring func. int process( double *input )
Calling...
|
by: nutty |
last post by:
Hi all,
I have the following problem ( explanation below code ):
// main.cpp
class noncopyable
{
protected:
noncopyable() {}
|
by: clqrq |
last post by:
i have just a little question:
guess i have a class with a static function and
i have different threads running. do i have to expect the problem that
2 treads try to acces CA::static() at the...
|
by: jht5945 |
last post by:
For example I wrote a function:
function Func()
{
// do something
}
we can call it like:
var obj = new Func(); // call it as a constructor
or
var result = Func(); // call it as...
|
by: Yevgen Muntyan |
last post by:
Hey,
Consider the following code:
void func (void)
{
}
void func2 (void)
{
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |