casting of struct element void pointer casting 
Author Message
 casting of struct element void pointer casting

Hi

I am having trouble with a following line of C code:

((char *)structure_name.void_pointer)-=3;

Error: lvalue required

Thanks for any help,
Dan



Tue, 27 Sep 2005 04:45:49 GMT  
 casting of struct element void pointer casting

Quote:
> Hi

> I am having trouble with a following line of C code:

> ((char *)structure_name.void_pointer)-=3;

Before this statement, does 'void_pointer' point
at least 3 bytes past the address of the object
or array to which it intitially pointed?  And
does that object or array have a size of at
least 3? If not, I believe that's UB.

That cast should also imo probably be to 'unsigned char*'

Quote:

> Error: lvalue required

(cast-to-type)(object)

.. creates a temporary object of the casted-to type.
Temporaries are not lvalues. Try something like:

unsigned char *p = structure_name.void_pointer;
p -= 3;
structure_name.void_pointer = p;

Also you should probably try to change that literal
3 to a macro or the result of calculating an object
or array size.

What specifically is the statement you posted
supposed to do?

-Mike



Tue, 27 Sep 2005 05:10:32 GMT  
 casting of struct element void pointer casting

Quote:

> Hi

> I am having trouble with a following line of C code:

> ((char *)structure_name.void_pointer)-=3;

> Error: lvalue required

An lvalue means that an expression which denotes an object
in memory is needed. If you have a variable called var then
in

var = 1; /*for suitable types of var*/

var denotes an object in memory.

You cannot do this however

1 = 3;

For obvious reasons.

The cast operator returns a values, not something that denotes an
object so what you are doing above is somewhat analogous to 1 = 3;
(it is probably closer to (var) = 3;, but the idea is the same).
If you are really wanting to do pointer arithmetic then something
more fundamental is wrong with your design I would say. If you are
trying to subtract 3 from a char then you are missing a * operator
in there.

*((char *)structure_name.void_pointer)-=3;

Might or might not be what you want. You aren't exactly giving much
information so it is hard to tell.

HTH.

--
Thomas.



Tue, 27 Sep 2005 05:39:30 GMT  
 casting of struct element void pointer casting

Quote:



>>Error: lvalue required

> (cast-to-type)(object)

> .. creates a temporary object of the casted-to type.

It just returns a value surely. No objects are created.

--
Thomas.



Tue, 27 Sep 2005 05:44:11 GMT  
 casting of struct element void pointer casting

Quote:

> ((char *)structure_name.void_pointer)-=3;

> Error: lvalue required

The cast operator does not yield an lvalue.  You can only assign
to an lvalue.  If structure_name.void_pointer has type `void *',
then you can do something equivalent with
        structure_name.void_pointer
                = ((char *) structure_name.void_pointer) - 3;
I can't recall right now whether the parentheses around the cast
expression are necessary, but they won't hurt anything.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


Tue, 27 Sep 2005 06:48:53 GMT  
 casting of struct element void pointer casting


Quote:



> >>Error: lvalue required

> > (cast-to-type)(object)

> > .. creates a temporary object of the casted-to type.

> It just returns a value surely. No objects are created.

I'm using the term 'temporary object' (perhaps
incorrectly) where you're using 'value'.

-Mike



Tue, 27 Sep 2005 08:00:44 GMT  
 casting of struct element void pointer casting

Quote:


>> ((char *)structure_name.void_pointer)-=3;

>> Error: lvalue required

> The cast operator does not yield an lvalue.  You can only assign
> to an lvalue.  If structure_name.void_pointer has type `void *',
> then you can do something equivalent with
>        structure_name.void_pointer
>                = ((char *) structure_name.void_pointer) - 3;
> I can't recall right now whether the parentheses around the cast
> expression are necessary, but they won't hurt anything.

They're not - all the unary operators (cast included) have higher
precedence than the mathematical operators (a fairly easy rule of thumb
to remember).

        - Kevin.



Tue, 27 Sep 2005 10:16:58 GMT  
 casting of struct element void pointer casting

Quote:
> Hi

> I am having trouble with a following line of C code:

> ((char *)structure_name.void_pointer)-=3;

> Error: lvalue required

> Thanks for any help,
> Dan

it would help a lot to see the declaration for structure_name.void_pointer
I assume it is something like:

struct test
{
void *void_pointer;

Quote:
};

struct test structure_name;

  ( (char *) structure_name.void_point)-=3;

--
Alex Russell



Tue, 27 Sep 2005 14:04:31 GMT  
 casting of struct element void pointer casting

Quote:

> it would help a lot to see the declaration for structure_name.void_pointer
> I assume it is something like:

> struct test
> {
> void *void_pointer;
> };

> struct test structure_name;

>   ( (char *) structure_name.void_point)-=3;

I hope you're not suggesting that that could be valid code.  It's
not: the cast operator does not yield an lvalue, so its result
cannot be used as the left side of an assignment operator.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Tue, 27 Sep 2005 14:06:41 GMT  
 casting of struct element void pointer casting


Quote:

>> ((char *)structure_name.void_pointer)-=3;

>> Error: lvalue required

> The cast operator does not yield an lvalue.  You can only assign
> to an lvalue.  If structure_name.void_pointer has type `void *',
> then you can do something equivalent with
>         structure_name.void_pointer
>                 = ((char *) structure_name.void_pointer) - 3;
> I can't recall right now whether the parentheses around the cast
> expression are necessary, but they won't hurt anything.

Thank you, I did not know that casting could not make lvalue


Tue, 27 Sep 2005 16:39:10 GMT  
 casting of struct element void pointer casting

Quote:

> *((char *)structure_name.void_pointer)-=3;

> Might or might not be what you want. You aren't exactly giving much
> information so it is hard to tell.

> HTH.

Thanks for reply,
I am trying to get void * to point to 3 character sizes below its current
pointing postion, not take 3 from the character at the current pointer
position.


Tue, 27 Sep 2005 16:43:08 GMT  
 casting of struct element void pointer casting

Quote:


> > *((char *)structure_name.void_pointer)-=3;

> > Might or might not be what you want. You aren't exactly giving
> > much information so it is hard to tell.

> I am trying to get void * to point to 3 character sizes below its
> current pointing postion, not take 3 from the character at the
> current pointer position.

   char *p;
...
   p = structure_name.void_pointer;  /* no cast */
   p -= 3;

assuming the void_pointer points somewhere within a suitable
entity.  If it is the result of a malloc, for example, this is not
necessarily valid, but it is the kind of thing done in system code
in accessing system fields during malloc/new/realloc operations.  

You can see an example (for djgpp at least) in:

   <http://cbfalconer.home.att.net/download/nmalloc.zip>

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 27 Sep 2005 18:32:37 GMT  
 casting of struct element void pointer casting

Quote:
> The cast operator returns a values, not something that denotes an
> object so what you are doing above is somewhat analogous to 1 = 3;
> (it is probably closer to (var) = 3;, but the idea is the same).
> If you are really wanting to do pointer arithmetic then something
> more fundamental is wrong with your design I would say. If you are
> trying to subtract 3 from a char then you are missing a * operator
> in there.

> *((char *)structure_name.void_pointer)-=3;

> Might or might not be what you want. You aren't exactly giving much
> information so it is hard to tell.

> HTH.

Hi

I am trying to get the pointer to point to 3 character spaces below
its current value.

P.S.
Is there a previous C standard where the originally posted line would
have worked? I am updating C code to a new compiler and this is one
line that would not compile.



Tue, 27 Sep 2005 18:57:34 GMT  
 casting of struct element void pointer casting

Quote:
> The cast operator returns a values, not something that denotes an
> object so what you are doing above is somewhat analogous to 1 = 3;
> (it is probably closer to (var) = 3;, but the idea is the same).
> If you are really wanting to do pointer arithmetic then something
> more fundamental is wrong with your design I would say. If you are
> trying to subtract 3 from a char then you are missing a * operator
> in there.

> *((char *)structure_name.void_pointer)-=3;

> Might or might not be what you want. You aren't exactly giving much
> information so it is hard to tell.

> HTH.

Hi

I am trying to get the pointer to point to 3 character spaces below
its current address.

P.S.
Is there a previous C standard where the originally posted line would
have worked? I am updating C code to a new compiler and this is one
line that would not compile.



Tue, 27 Sep 2005 18:57:36 GMT  
 casting of struct element void pointer casting

Quote:
> The cast operator does not yield an lvalue.  You can only assign
> to an lvalue.  If structure_name.void_pointer has type `void *',
> then you can do something equivalent with
>         structure_name.void_pointer
>                 = ((char *) structure_name.void_pointer) - 3;
> I can't recall right now whether the parentheses around the cast
> expression are necessary, but they won't hurt anything.

Thank you,
 I did not know that cast operator not make lvalue.


Tue, 27 Sep 2005 18:58:08 GMT  
 
 [ 22 post ]  Go to page: [1] [2]

 Relevant Pages 

1. cast pointer to struct to pointer to type of first member of struct

2. Casting function pointer to void pointer

3. casting void pointer to be a pointer to a function

4. casting void pointer to be a pointer to a function

5. Casting pinning pointer to void *

6. Casting from a void pointer

7. Casting void pointers

8. Casting function pointer to void*

9. Is explicit casting of void * pointer ever necessary?

10. casting void pointers

11. casting void pointers

12. casting void pointer

 

 
Powered by phpBB® Forum Software