Topic: typedef differences ? ?


Author: "Navin Pai" <navin_pai_nospam@aereous.com>
Date: Tue, 4 Dec 2001 18:44:10 GMT
Raw View
please go through the following code fragment

typedef char CHARRY[20];

template <class T>
void fun(T& ch0) {
    CHARRY ch1;
    ch1 = ch0;

    for (int i =0; i<20; i++)
        cout << ch1[i];
}

int main() {
    CHARRY ch0;

    for (int i = 0; i < 20; i++)
        ch0[i] = i;

    fun (ch0);
}

the code compiles and runs on gcc under linux. the same code does not
compiles under VC++. here i assume that gcc is taking typedef's as struct
and doing member copy of all the array elements.
which behaviour is correct??? gcc or msvc

regds
navin




---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Tue, 4 Dec 2001 19:26:45 GMT
Raw View

Navin Pai wrote:
>

>     CHARRY ch1;
>     ch1 = ch0;
>
>
> the code compiles and runs on gcc under linux. the same code does not
> compiles under VC++. here i assume that gcc is taking typedef's as struct
> and doing member copy of all the array elements.
> which behaviour is correct??? gcc or msvc
>

No, the problem is has nothing to do with the typedef.
you rewrite the lines:

 char ch0[20];
 char ch1[20];
 ch1 = ch0;

You'll see the same behavior differences between G++ and VC++.

In real C and C++ arrays can not be assigned or returned from functions,
and they pass as arguments in a way different than any other type in the
language.  It's perhaps one of the most braindead features of C.

G++ has an extension (which used to be enabled by default, but I think they
finally set the standard behavior as the defualt), that allows assignment
of arrays (there's no syntactical reason to disallow it, since it's not allowed
otherwise).

MSVC gets a cookie for behaving right.

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]