473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

conversion from void*

Hi,

the following snippet of code does not compile with gcc 3.3. Why? It
works when I take "typedef test test_t" instead, but this is not what
I want.
I admit that I do not fully understand this "typedef test test_t[1]"
trick which I copied from the GNU MP library. Could somebody please
explain?

TIA,
Tobias

================================================== ===================
typedef struct
{
int x;
} test;

typedef test test_t[1];

int main()
{
void* a;
test_t* b;

b = (test_t*)a; /* this does work */
(*b) = *(test_t*)a; /* this does not work: "incompatible types
in assignment" */

return 0;
}
Nov 14 '05 #1
3 1212
saibot wrote:

typedef struct
{
int x;
} test;

typedef test test_t[1];
An object of type test_t is an array of one test (i.e. a struct
with one int, x, in it - we'll use your typedef of test to describe
such a struct).
int main()
{
void* a;
test_t* b;
b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */
b = (test_t*)a; /* this does work */
This is equivalent to: b = (test (*)[1])a;

and is fine.
(*b) = *(test_t*)a; /* this does not work: "incompatible types
in assignment" */


*b is an array of one test object. You can't assign to arrays,
because they are not modifiable lvalues.
Nov 14 '05 #2
On Fri, 11 Feb 2005 13:19:43 +0000, infobahn wrote:

....
b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */


Or rather

test (*b)[1]; /* i.e. a pointer to an array of 1 test */

Lawrence
Nov 14 '05 #3
Lawrence Kirby wrote:

On Fri, 11 Feb 2005 13:19:43 +0000, infobahn wrote:

...
b is a pointer to an array of 1 test. That is, it could also be
defined as:

test (*)b[1]; /* i.e. a pointer to an array of 1 test */


Or rather

test (*b)[1]; /* i.e. a pointer to an array of 1 test */


Oops. My apologies. I didn't have my thinking head on.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
4
by: tom | last post by:
I have a code segment list below, for the function call "calc()" in the main function, a standard conversion from "double"->"int" is made while "double"->"LongDouble" is also viable, does anyone...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.