Topic: {'a','b','\0'} not equivalent to "ab"??
Author: Martin Fabian <fabian@control.chalmers.se>
Date: 1997/11/13 Raw View
Given a function like
void f(char *);
we all know we can make the call
f("ab");
Now, since "ab" is equivalent to the static
structure {'a','b','\0'}, I claim we would
also be able to call the function like
f({'a','b','\0'});
My compiler (MSVC++4.2, btw) does not agree.
What do you say?
--
Martin Fabian
-----------------------------------------------------------
email: fabian@control.chalmers.se | Control Engineering Laboratory
tel: +46 (0)31 772 37 16 | Chalmers University of Technology
fax: +46 (0)31 772 37 30 | S-412 96 Gothenburg
| Sweden
Homepage: http://www.control.chalmers.se/~fabian
------------------------------------------------------------------------
Everyone is talking about real-time, but how real is time, really?
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Peder H. Pedersen" <di970667@diku.dk>
Date: 1997/11/13 Raw View
I believe the problem is that, as you point out, {'a','b','\0'} is not
equivalent to "ab". "ab" is constant expression and {'a', 'b', '\0'} is
an initializer list. Thus I think this would work:
Given f(char* foostring) these two should be (approximately) equivalent:
f("ab")
f(char* barstring = {'a', 'b', '\0'}
I may be wrong, though. I am certain, however, that {'a','b','\0'} is
not equivalent to "ab". I started learning C++ less than a year ago, so
I am far from an expert.
Peder Holdgaard Pedersen
Student of Computer Science at The University of Copenhagen
Martin Fabian wrote:
>
> Given a function like
> void f(char *);
> we all know we can make the call
> f("ab");
> Now, since "ab" is equivalent to the static
> structure {'a','b','\0'}, I claim we would
> also be able to call the function like
> f({'a','b','\0'});
> My compiler (MSVC++4.2, btw) does not agree.
> What do you say?
> --
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Jean-Louis Leroy <jll@skynet.be>
Date: 1997/11/13 Raw View
In article <346AAF0E.34CE@control.chalmers.se>, Martin Fabian wrote:
> Now, since "ab" is equivalent to the static
> structure {'a','b','\0'}, I claim we would
> also be able to call the function like
> f({'a','b','\0'});
Nope. {'a','b','\0'} is an initializer, not an expression.
Jean-Louis Leroy
http://ourworld.compuserve.com/homepages/jl_leroy
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: nospam@nospam.com
Date: 1997/11/13 Raw View
Martin Fabian <fabian@control.chalmers.se> writes:
>
> Given a function like
> void f(char *);
> we all know we can make the call
> f("ab");
> Now, since "ab" is equivalent to the static
> structure {'a','b','\0'}, I claim we would
> also be able to call the function like
> f({'a','b','\0'});
> My compiler (MSVC++4.2, btw) does not agree.
> What do you say?
A wild shot in the dark... "ab" is of type char* (actually, const char*,
but that is another discusssion). {'a','b','\0'} is of type char[3] which
is technically not the same thing. You can pass a char[] to a char* (most
of the time) but I guess the compiler doesn't allow the reverse without
an explicit cast.
See if:
f(static_cast<char*>({'a','b','\0'}))
works.
/Mike
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Otmar.Schwan@t-online.de (Otmar Schwan)
Date: 1997/11/13 Raw View
Martin Fabian wrote:
>
> Given a function like
> void f(char *);
> we all know we can make the call
> f("ab");
> Now, since "ab" is equivalent to the static
> structure {'a','b','\0'}, I claim we would
> also be able to call the function like
> f({'a','b','\0'});
> My compiler (MSVC++4.2, btw) does not agree.
> What do you say?
> --
> Martin Fabian
Hi Martin,
"ab" is really the same as {'a','b','\0'} but you could not
take these in a function-
It4s not a expression - it4s an iniatilizer-List!
One chance:
Mr. Stroustrup rewrite the c++-compiler :)
what you could do is that:
#include <stdio.h>
void funcA(char *cp)
{
printf("cp = %s \n", cp);
}
int main(void)
{
funcA("Hello");
char ca[] = {'a', 'b', '\0'};
funcA(ca);
return 0;
}
Output:
Hello
ab
regards,
Otmar
--
****Otmar Schwan TyBrain Consulting Heidenheim/Germany ***
*** Web: www.t-online.de/home/otmar.schwan **********
*** EMail: otmar.schwan@t-online.de **********
************************************************************
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: marty Holcomb <mholcomb@BIX.com>
Date: 1997/11/13 Raw View
Martin Fabian wrote:
>Now, since "ab" is equivalent to the static
>structure {'a','b','\0'}, I claim we would
>
I don't think they are equal are they? "ab" is very much like a character
array while {'a', 'b', '\0'} is a structure. Arrays and structures are not
handled the same in memory in every case. You can't walk down the pointer
chain to the next character in a structure, etc.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: cine-bit@fiber.net (Joe Woodbury)
Date: 1997/11/14 Raw View
>Given a function like
> void f(char *);
>we all know we can make the call
> f("ab");
>Now, since "ab" is equivalent to the static
>structure {'a','b','\0'}, I claim we would
>also be able to call the function like
> f({'a','b','\0'});
>My compiler (MSVC++4.2, btw) does not agree.
>What do you say?
>--
>Martin Fabian
The compiler is clearly correct. You are creating two different types.
Joseph Woodbury
cine-bit@fiber.net
(make cine-bit one word to email me)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/11/14 Raw View
Martin Fabian <fabian@control.chalmers.se> writes:
|> Given a function like
|> void f(char *);
|> we all know we can make the call
|> f("ab");
No you can't. The type of f must be "f( char const* )" for this to be
legal.
|> Now, since "ab" is equivalent to the static
|> structure {'a','b','\0'}, I claim we would
|> also be able to call the function like
|> f({'a','b','\0'});
The {...} syntax is only legal for initializing variables (arrays and
structs), not for passing as an argument. At least today; there is a
proposal for C9x to allow compound literals of the type:
f( (char []){ 'a' , 'b' , '\0' } ) ;
Of course, you'd still have to specify the type. Your {...} list could
be an initializer for a char[3], but just as easily for a struct with
three char elements -- the compiler has no way of knowing. (Note that
this extension makes it possible in C to obtain the address of a
temporary, and thus introduces C programmers to the lifetime of
temporary issue that we so know and love in C++. With a different
resolution than C++.)
Anyway, at present, this is only a proposal for the next C standard.
Presumably, if adopted in C, it will rapidly find its way into C++
compilers as an extension, and probably be adopted in the next version
of C++ as well. For the moment, however, you still have to write:
char tmp[] = { 'a' , 'b' , '\0' } ;
f( tmp ) ;
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
I'm looking for a job -- Je recherche du travail
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Mark Wilden <Mark@mwilden.com>
Date: 1997/11/17 Raw View
> Martin Fabian wrote:
> >
> > Given a function like
> > void f(char *);
> > we all know we can make the call
> > f("ab");
> > Now, since "ab" is equivalent to the static
> > structure {'a','b','\0'}, I claim we would
> > also be able to call the function like
> > f({'a','b','\0'});
> > My compiler (MSVC++4.2, btw) does not agree.
> > What do you say?
The language defines the semantics of argument passing to be the
equivalent to initialization, but I guess not the syntax. What no one
here who has been so quick to quote chapter and verse has said, however,
is _why_ this is so. I think it's just an oversight, though not a
terribly important one.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]