Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer-to-pointer (invalid lvalue in unary `&)

Reply
Thread Tools

pointer-to-pointer (invalid lvalue in unary `&)

 
 
Lucas Machado
Guest
Posts: n/a
 
      04-03-2004
i'm doing some Linux Kernel hacking for a course i'm currently taking.
there is a pointer to a struct (struct example_struct *ex_ptr) in a .c
that i want to access in a system call. i defined a pointer to a
pointer in the .c:

extern struct example_struct **pointer;

and somewhere in the code i tried:

pointer = &ex_ptr;

and i get this error when trying to compile the kernel:

invalid lvalue in unary `&'

and it points to the line: pointer = &ex_ptr;....any idea why and what
i should do to fix it?
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-03-2004
On 3 Apr 2004 09:49:41 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Lucas Machado)
wrote:

>i'm doing some Linux Kernel hacking for a course i'm currently taking.
>there is a pointer to a struct (struct example_struct *ex_ptr) in a .c
>that i want to access in a system call. i defined a pointer to a
>pointer in the .c:
>
>extern struct example_struct **pointer;
>
>and somewhere in the code i tried:
>
>pointer = &ex_ptr;
>
>and i get this error when trying to compile the kernel:
>
>invalid lvalue in unary `&'
>
>and it points to the line: pointer = &ex_ptr;....any idea why and what
>i should do to fix it?


From what you've posted, all looks fine. Can you show /exactly/ what the
definition of ex_ptr looks like? Whatever ex_ptr is, it doesn't seem to
have an address associated with it. If it were just the wrong type for
"pointer", I'd have expected the message to spell that out. So my guess is
that "ex_ptr" is actually something like a constant, as weird as that
sounds...
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-03-2004
Lucas Machado wrote:
> i'm doing some Linux Kernel hacking for a course i'm currently taking.
> there is a pointer to a struct (struct example_struct *ex_ptr) in a .c
> that i want to access in a system call. i defined a pointer to a
> pointer in the .c:
>
> extern struct example_struct **pointer;
>
> and somewhere in the code i tried:
>
> pointer = &ex_ptr;
>
> and i get this error when trying to compile the kernel:
>
> invalid lvalue in unary `&'
>
> and it points to the line: pointer = &ex_ptr;....any idea why and what
> i should do to fix it?



You have almost certainly misdiagnosed the problem. Look to the lines
before the one for which the error is reported. Simple things like
missing a ';' on the line before may be such a culprit. Notice the use
of your assignment below, which is fine. (If you had an incorrect use
of 'extern', that would probably lead to a linking error instead).

#include <stdio.h>

struct example_struct
{
int f;
} basestruct = { 0};
struct example_struct *ex_ptr = &basestruct;

int main(void)
{
struct example_struct **pointer;
pointer = &ex_ptr;
(*pointer)->f = 3;
printf("basestruct.f = %d\n", basestruct.f);
printf("ex_ptr->f = %d\n", ex_ptr->f);
printf("(*pointer)->f = %d\n", (*pointer)->f);
return 0;
}


basestruct.f = 3
ex_ptr->f = 3
(*pointer)->f = 3
 
Reply With Quote
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-04-2004
"Leor Zolman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 3 Apr 2004 09:49:41 -0800, (E-Mail Removed) (Lucas Machado)
> wrote:
>
> >i'm doing some Linux Kernel hacking for a course i'm currently taking.
> >there is a pointer to a struct (struct example_struct *ex_ptr) in a .c
> >that i want to access in a system call. i defined a pointer to a
> >pointer in the .c:
> >
> >extern struct example_struct **pointer;
> >
> >and somewhere in the code i tried:
> >
> >pointer = &ex_ptr;
> >
> >and i get this error when trying to compile the kernel:
> >
> >invalid lvalue in unary `&'
> >
> >and it points to the line: pointer = &ex_ptr;....any idea why and what
> >i should do to fix it?

>
> From what you've posted, all looks fine. Can you show /exactly/ what the
> definition of ex_ptr looks like? Whatever ex_ptr is, it doesn't seem to
> have an address associated with it. If it were just the wrong type for
> "pointer", I'd have expected the message to spell that out. So my guess is
> that "ex_ptr" is actually something like a constant, as weird as that
> sounds...
> -leor
>
> --
> Leor Zolman --- BD Software --- www.bdsoft.com
> On-Site Training in C/C++, Java, Perl and Unix
> C++ users: Download BD Software's free STL Error Message Decryptor at:
> www.bdsoft.com/tools/stlfilt.html


Ditto! How is ex_ptr defined?

Should be something like:
struct example_struct ex_st;
struct example_struct *ex_ptr;
struct example_struct **pointer;

If you simply want the problem to go away to see if it is a compiler error,
try casting it. That would hide any errors that might occur due to the
compiler only... changing your debugging process from finding a syntax error
to figuring out why your computer is crashing

pointer = (struct example_struct **pointer)&ex_ptr;

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-04-2004
Chris Fogelklou wrote:

<snip>

> If you simply want the problem to go away to see if it is a compiler
> error,
> try casting it.


That doesn't seem very fruitful advice. He'd be better off trying to
understand the type system than trying to learn how to circumvent it.

> That would hide any errors that might occur due to the
> compiler only... changing your debugging process from finding a syntax
> error to figuring out why your computer is crashing


Finding the syntax error is much easier.

> pointer = (struct example_struct **pointer)&ex_ptr;


That cast is badly-formed, and requires a diagnostic.

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-04-2004
> That doesn't seem very fruitful advice. He'd be better off trying to
> understand the type system than trying to learn how to circumvent it.
>

Yep.

>>changing your debugging process from finding a syntax
> > error to figuring out why your computer is crashing

>
> Finding the syntax error is much easier


Hence the smiley and the statement that he would have to figure out why the
computer is crashing (sarcasm... oops... my bad

> > pointer = (struct example_struct **pointer)&ex_ptr;

>
> That cast is badly-formed, and requires a diagnostic.


Hmm... It compiles on my system... but from the other posts/responses I've
been participating in, I think my system might be broken. Could you please
elaborate?

typedef struct ui_tag {
uint16 i;
uint16 j;
uint16 k;
uint16 l;
} ui_t, *pui_t;

void main(void)
{
struct ui_tag * ptr_ui;
struct ui_tag ** pptr_ui;
struct ui_tag ui = {0,0,0,0};

ptr_ui = &ui;
pptr_ui = (struct ui_tag **)&ptr_ui;
while(1);

}



 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      04-04-2004
Chris Fogelklou wrote:

>> That doesn't seem very fruitful advice. He'd be better off trying to
>> understand the type system than trying to learn how to circumvent it.
>>

> Yep.
>
>>>changing your debugging process from finding a syntax
>> > error to figuring out why your computer is crashing

>>
>> Finding the syntax error is much easier

>
> Hence the smiley and the statement that he would have to figure out why
> the computer is crashing (sarcasm... oops... my bad
>
>> > pointer = (struct example_struct **pointer)&ex_ptr;

>>
>> That cast is badly-formed, and requires a diagnostic.

>
> Hmm... It compiles on my system...


Really? I'm surprised.

> but from the other posts/responses I've
> been participating in, I think my system might be broken. Could you
> please elaborate?
>
> typedef struct ui_tag {
> uint16 i;
> uint16 j;
> uint16 k;
> uint16 l;
> } ui_t, *pui_t;
>
> void main(void)


int main(void)

> {
> struct ui_tag * ptr_ui;
> struct ui_tag ** pptr_ui;
> struct ui_tag ui = {0,0,0,0};
>
> ptr_ui = &ui;
> pptr_ui = (struct ui_tag **)&ptr_ui;


This isn't the same cast.

The one I said was badly-formed was:

>> > pointer = (struct example_struct **pointer)&ex_ptr;


struct example_struct ** is a type, so (struct example_struct **) is a
well-formed cast, but (struct example_struct **pointer) is not.

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-04-2004
Chris Fogelklou wrote:

[Chris Fogelklou wrote, although he suppressed attribution]
>>>pointer = (struct example_struct **pointer)&ex_ptr;


[Richard Heathfield wrote, although Chris Fogelklou suppressed attribution]
>>That cast is badly-formed, and requires a diagnostic.



> Hmm... It compiles on my system... but from the other posts/responses I've
> been participating in, I think my system might be broken.


Either your system or you coding is broken, if not both. After, you
write code like:

> void main(void)


and your compiler seems not to mind. Turn you damn diagnostics back on.
And learn how to quote properly.
 
Reply With Quote
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-05-2004

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:c4ptsb$2kchtg$(E-Mail Removed)-berlin.de...
> Chris Fogelklou wrote:
>
> [Chris Fogelklou wrote, although he suppressed attribution]
> >>>pointer = (struct example_struct **pointer)&ex_ptr;

>
> [Richard Heathfield wrote, although Chris Fogelklou suppressed

attribution]
> >>That cast is badly-formed, and requires a diagnostic.

>
>
> > Hmm... It compiles on my system... but from the other posts/responses

I've
> > been participating in, I think my system might be broken.

>
> Either your system or you coding is broken, if not both. After, you
> write code like:
>
> > void main(void)

>
> and your compiler seems not to mind. Turn you damn diagnostics back on.
> And learn how to quote properly.


I have found that this is the wrong group for me to post in.

Richard, now I see the problem. Of course that was a bad cast. I guess I
left that error out when I wrote the code.

Martin, most of my C programming is for an embedded system... if the main
function were ever to return (return value or not), I would be running out
of uninitialized FLASH or RAM... probably a more serious problem, no? I can
see your point, however, if running inside an OS, where the OS checks the
return value.

In any case, I think I will drop completely out of this NG. I am obviously
not familiar enough with the hard and fast ANSI rules...

It's been real.

Chris

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-05-2004
Chris Fogelklou wrote:
>

.... snip ...
>
> I have found that this is the wrong group for me to post in.
>
> Richard, now I see the problem. Of course that was a bad cast.
> I guess I left that error out when I wrote the code.
>
> Martin, most of my C programming is for an embedded system...
> if the main function were ever to return (return value or not),
> I would be running out of uninitialized FLASH or RAM... probably
> a more serious problem, no? I can see your point, however, if
> running inside an OS, where the OS checks the return value.
>
> In any case, I think I will drop completely out of this NG. I am
> obviously not familiar enough with the hard and fast ANSI rules...


On the contrary, lurking and/or participating will make you a
better embedded programmer, because you will write more portable
code that doesn't require revision for every new situation.
However, do grow a set of scabs.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using a method as an lvalue and "invalid lvalue in assignment"compilation error markryde@gmail.com C Programming 11 09-22-2008 10:42 AM
operators requiring lvalue/rvalue operands and resulting in rvalue/lvalue Kavya C Programming 9 10-28-2006 01:45 AM
invalid lvalue in unary '&' .... why LKM bob C Programming 4 01-26-2006 09:29 AM
invalid lvalue in unary '&' .... why LKM bob C Programming 0 01-26-2006 04:09 AM
Ordinary unary function to STL adaptable unary predicate how? SpOiLeR C++ 10 10-19-2005 01:18 PM



Advertisments