problem with pointer to pointer to void 
Author Message
 problem with pointer to pointer to void

Hi All,
I have a buffer allocation function that was declared as follows:
 int MyBuffAlloc(size_t size, void **pBuff);
I called this function
1 typedef struct MyStruct
2 {
3    int A;
4    int B;
5 } MyStruct;
6 int *pMyBuff;
7 int RetCode;
//cast int* into void*
8 RetCode = MyBuffAlloc(sizeof(MyStruct), &((void *)(pBuff)));
......

My compiler didn't complain about the call in line 8. But when I run
a lint on this file, the lint complained:
Error 50: Attempted to take the address of a non-lvalue.
I made a cast so that I could call function MyBuffAlloc properly. When
I removed the cast I got no error from neither the compiler nor lint.
My question is: Why I got an error when trying to be syntactically
correct, and didn't get an error the other way around?
Thanks.

Sent via Deja.com
http://www.*-*-*.com/



Sat, 14 Jun 2003 06:07:40 GMT  
 problem with pointer to pointer to void

Quote:

>  int MyBuffAlloc(size_t size, void **pBuff);
> 6 int *pMyBuff;
> 8 RetCode = MyBuffAlloc(sizeof(MyStruct), &((void *)(pBuff)));
> ......

> My compiler didn't complain about the call in line 8. But when I run
> a lint on this file, the lint complained:
> Error 50: Attempted to take the address of a non-lvalue.

The result of a cast is not an lvalue.  The & operator must have
an lvalue as its argument.

Quote:
> I made a cast so that I could call function MyBuffAlloc properly. When
> I removed the cast I got no error from neither the compiler nor lint.

Are you sure?  void ** is not compatible with int **.  What
compiler are you using?

Quote:
> My question is: Why I got an error when trying to be syntactically
> correct, and didn't get an error the other way around?

You should have received an error both ways.
--
"I hope, some day, to learn to read.
 It seems to be even harder than writing."
--Richard Heathfield


Sat, 14 Jun 2003 06:42:32 GMT  
 problem with pointer to pointer to void
I think you want line 8 to read:
  RetCode = MyBuffAlloc(sizeof(MyStruct), (void **)&pBuff);

Or if the function prototype is in scope, it can take care of the casting:
  RetCode = MyBuffAlloc(sizeof(MyStruct), &pBuff);

BTW, you declare pMyBuff to be a pointer to an int......shouldn't it be a
pointer to a MyStruct?

~Glynne

Hi All,
I have a buffer allocation function that was declared as follows:
 int MyBuffAlloc(size_t size, void **pBuff);
I called this function
1 typedef struct MyStruct
2 {
3    int A;
4    int B;
5 } MyStruct;
6 int *pMyBuff;
7 int RetCode;
//cast int* into void*
8 RetCode = MyBuffAlloc(sizeof(MyStruct), &((void *)(pBuff)));
......

My compiler didn't complain about the call in line 8. But when I run
a lint on this file, the lint complained:
Error 50: Attempted to take the address of a non-lvalue.
I made a cast so that I could call function MyBuffAlloc properly. When
I removed the cast I got no error from neither the compiler nor lint.
My question is: Why I got an error when trying to be syntactically
correct, and didn't get an error the other way around?
Thanks.

Sent via Deja.com
http://www.deja.com/



Sat, 14 Jun 2003 07:14:24 GMT  
 problem with pointer to pointer to void

Quote:


> >  int MyBuffAlloc(size_t size, void **pBuff);

> > 6 int *pMyBuff;
> > 8 RetCode = MyBuffAlloc(sizeof(MyStruct), &((void *)(pBuff)));
> > ......

> > My compiler didn't complain about the call in line 8. But when I run
> > a lint on this file, the lint complained:
> > Error 50: Attempted to take the address of a non-lvalue.

> The result of a cast is not an lvalue.  The & operator must have
> an lvalue as its argument.

> > I made a cast so that I could call function MyBuffAlloc properly.
When
> > I removed the cast I got no error from neither the compiler nor
lint.

> Are you sure?  void ** is not compatible with int **.  What
> compiler are you using?

Thanks for the answer. I'm using CAD-UL CXX386 C++ Cross Optimizing
Compiler.

Quote:
> > My question is: Why I got an error when trying to be syntactically
> > correct, and didn't get an error the other way around?

> You should have received an error both ways.
> --
> "I hope, some day, to learn to read.
>  It seems to be even harder than writing."
> --Richard Heathfield

Sent via Deja.com
http://www.deja.com/


Sat, 14 Jun 2003 07:58:45 GMT  
 problem with pointer to pointer to void

Quote:

>Hi All,
>I have a buffer allocation function that was declared as follows:
> int MyBuffAlloc(size_t size, void **pBuff);

This type of interface is a sign of trouble. You cannot use the type void ** as
a generic pointer-to-any-pointer. This issue is covered in the comp.lang.c FAQ.

Quote:
>8 RetCode = MyBuffAlloc(sizeof(MyStruct), &((void *)(pBuff)));

The result of a cast expression is not an lvalue and so has no address.
The expression (void *) pBuff means to convert the lvalue pBuff into
the value stored in the object that it designates, and then to convert
that value to (void *). By the time this happens, the lvalue property
has been stripped away.

Quote:
>......

>My compiler didn't complain about the call in line 8.

Your compiler is broken or is being operated in some non-conforming mode in
which it is possible for the results of cast expressions to behave as lvalues.
Find out how to make your compiler conform to ANSI C as closely as it can. In
the case of the GNU C compiler, for instance, the -ansi -pedantic options do
this.


Sat, 14 Jun 2003 11:57:41 GMT  
 problem with pointer to pointer to void
On Mon, 25 Dec 2000 23:14:24 GMT, "Glynne"

Quote:

>I think you want line 8 to read:
>  RetCode = MyBuffAlloc(sizeof(MyStruct), (void **)&pBuff);

That won't cure anything. void** is not some kind of generic pointer.

Quote:

>Or if the function prototype is in scope, it can take care of the casting:
>  RetCode = MyBuffAlloc(sizeof(MyStruct), &pBuff);

The compiler should start complaining if there is a prototype in
scope. You can not implicitly convert some pointer to void**.

Quote:

>BTW, you declare pMyBuff to be a pointer to an int......shouldn't it be a
>pointer to a MyStruct?

>~Glynne

Bart v Ingen Schenau
--
FAQ for clc: http://www.eskimo.com/~scs/C-faq/top.html


Tue, 17 Jun 2003 00:52:55 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Casting function pointer to void pointer

2. pointers to functions and pointers to void.

3. pointer arithmetic with a pointer to void

4. Can void-pointers be casted to function-pointers?

5. Pointer-arithmatic and void pointers

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

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

8. void pointer (void *) : how to determine type?

9. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

10. void pointer assignment problem

11. Problem with sizeof of a struct void pointer

12. void pointers problem

 

 
Powered by phpBB® Forum Software