Jump to content

const casting in CIN


Recommended Posts

i have a const double array, and when i want to copy a value, for example:

typedef struct { double x, y, z} array3;

const array3* v;

double my_value;

my_value = v->x;

and labview crashes, and the debug window shows this line as the cause. with some access violation error messages....

anybody encountered something like this? please help me... thank you!

Link to comment

OK, it's been awhile since I even HAD a C compiler installed on my PC, but here goes:

i have a const double array, and when i want to copy a value, for example:

typedef struct { double x, y, z} array3;  // you've defined 'array3' to mean a structure containing three double-precision floating-point elementsconst array3* v; // you've instanciated a pointer to a constant 'array3' and named the pointer 'v'double my_value;  // you've instanciated a double-precision floating-point named 'my_value'my_value = v->x;  // you tried to assign to 'my_value' the value of the 'x' element of the struct pointed to by 'v'// ... but you never assigned 'v' a value (i.e., reserved memory for an 'array3' and pointed 'v' at it)

and labview crashes, (looks like you tried to dereference a null pointer) and the debug window shows this line as the cause. with some access violation error messages....

anybody encountered something like this? please help me... thank you!

Did I get it right??

Hope this helps,

Dave

Link to comment
  • 2 weeks later...
i have a const double array, and when i want to copy a value, for example:

typedef struct { double x, y, z} array3;

const array3* v;

double my_value;

my_value = v->x;

and labview crashes, and the debug window shows this line as the cause. with some access violation error messages....

anybody encountered something like this? please help me... thank you!

By declaring "const array3* v;" you only declare a const pointer to the struct but do not allocate any storage for that structure at all. So when you try to dereference that pointer it points into nirwana and crashes.

What you want to do is more along the lines of:

typedef struct { double x, y, z} array3;

const array3 v;

double my_value;

my_value = v.x;

Which of course is still not very useful since v.x will contain some random data (usually 0 but not all compilers will initialize const data to 0).

Rolf Kalbermatter

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.