Re: conversion from array to reference to pointer
#include <iostream>
int main(void)
{
char array[] = {'a', 'b', 'c'};
char *ptr = "def";
char* const &p = reinterpret_cast<char* const &>(array);
char* const &q = reinterpret_cast<char* const &>(ptr);
char *& p1 = reinterpret_cast<char*&>(array);
char *& q1 = reinterpret_cast<char*&>(ptr);
// If p is a reference to a pointer, then it should be used
// just as a normal pointer.
printf("%c %c %c\n", p, p+1, p+2);
// If p is a reference to a pointer, then it should be used
// just as a normal pointer.
printf("%c %c %c\n", q[0], q[1], q[2]);
// If p is a reference to a pointer, then it should be used
// just as a normal pointer.
printf("%c %c %c\n", p1, p1+1, p1+2);
// If p is a reference to a pointer, then it should be used
// just as a normal pointer.
printf("%c %c %c\n", q1[0], q1[1], q1[2]);
// If p really is a reference, then this line shouldn't compile.
// p = 0;
// If q really is a reference, then this line shouldn't compile.
// q = 0;
// If p1 really is a reference, then this line shouldn't compile.
p1 = 0;
// If q1 really is a reference, then this line shouldn't compile.
q1 = 0;
// The previous lines compile, hence they aren't references!
return (int)p;
}
uvts_cvs@yahoo.com wrote:
Hi everybody do you think this little chunk of code is legal and leads
to defined results?
int main()
{
char v[] = "";
char *const& p = reinterpret_cast<char*const&>(v);
return *p;
}
And the next one?
int main()
{
char v[] = "";
char *& p = reinterpret_cast<char*&>(v);
return *p;
}
Bye, Daniele.
---
[ 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.comeaucomputing.com/csc/faq.html ]
---
[ 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.comeaucomputing.com/csc/faq.html ]