{'a','b','\0'} not equivalent to "ab"??

{'a','b','\0'} not equivalent to "ab"??

Post by Martin Fabia » Fri, 14 Nov 1997 04:00:00



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
-----------------------------------------------------------

     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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by Peder H. Pederse » Fri, 14 Nov 1997 04:00:00


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


> 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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]


 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by Jean-Louis Lero » Fri, 14 Nov 1997 04:00:00



> 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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by nos.. » Fri, 14 Nov 1997 04:00:00



> 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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by Otmar Schw » Fri, 14 Nov 1997 04:00:00



> 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);

Quote:}        

int main(void)
{
        funcA("Hello");
        char ca[] = {'a', 'b', '\0'};
        funcA(ca);

        return 0;

Quote:}

Output:  
Hello
ab

regards,

Otmar

--
****Otmar Schwan  TyBrain Consulting  Heidenheim/Germany ***
*** Web:    www.t-online.de/home/otmar.schwan **********

************************************************************
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by marty Holcom » Fri, 14 Nov 1997 04:00:00



>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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by Joe Woodbu » Sat, 15 Nov 1997 04:00:00


Quote:>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

(make cine-bit one word to email me)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by J. Kan » Sat, 15 Nov 1997 04:00:00


|>  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 ) ;

--

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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

{'a','b','\0'} not equivalent to "ab"??

Post by Mark Wilde » Tue, 18 Nov 1997 04:00:00



> > 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      ]

[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]

 
 
 

1. "error C2582: 'SubClass' : 'operator =' function is unavailable"

        This is probably the weirdest thing that's happened to me since I
started programming. I've been using the same code with no problems
for about a week and then this morning I get the above error. I've
obviously changed something in my sleep!

I've managed to get the code down to these few lines. Can anyone
explain what's going on?

#include <vector>

class BaseClass
{
private:

  const int      m_ID;

protected:

  BaseClass(int id):m_ID(id){}

class SubClass : public BaseClass
{
public:

  SubClass():BaseClass(1) {}

int main()
{

  std::vector<SubClass> subs;

  subs.push_back(SubClass());

  return 1;

2. Publishing Problem! Need your help!!!!!!

3. Two "identical" PC's one GPF's one doesn't

4. How: case insensitivity with Apache 1.3.3 ?

5. "Don't discuss 'loving knowledge'" - Itself a Philosophy -> Logical Positivism

6. How can I secure local hard drives?

7. 'const_iterator' : is not a member of '`global namespace''

8. PAL UUNET on Linux

9. Compiler error: "'Method' is not a member" when dynamically creating __com_ptr_t

10. 'initializing' : truncation from 'const double' to 'float' warning

11. Q:'default' before 'case' in 'switch'

12. A Solution to 'Error C2660 'new' : function does not take 3 parameters'

13. why I can not use getprocAddress(loadModule('gdi32'),'textouta')?