Thread: :523: error: invalid lvalue in assignment

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    20

    Post :523: error: invalid lvalue in assignment

    hello,
    how can i solve this problem ........

    error: invalid lvalue in assignment
    Code:
    if(c==0)
    		{
    			(char **)sTmp= &sd->source;
    			nl=sd->sbound;
    		}
    this is my structure
    Code:
    struct _sd
    {
    	char source[MAXHOSTIP][MAXHOSTLENGHT];
    	char   dest[MAXHOSTIP][MAXHOSTLENGHT];
    	int sbound;
    	int dbound;
    };
    
    struct _tcp_udp
    {
    	struct _sd sd;
    	int ports[MAXPORTS];
    };
    
    struct _tcprst
    {
    	struct _sd sd;
    	int seq;
    	int source_port;
    	int dest_port;
    };
    
    struct _icmp_igmp
    {
    	struct _sd sd;
    };
    
    struct listofip
    {
    	struct _tcp_udp		tcp;
    	struct _tcp_udp		udp;
    	struct _icmp_igmp	icmp;
    	struct _icmp_igmp	igmp;
    	struct _tcprst		trst;
    }ltp;
    but when i am using like this

    Code:
    if(c==0)
    		{
    			sTmp= &sd->source;
    			nl=sd->sbound;
    		}
    Code:
    int ResolveHostIP(char* argv, struct _sd *sd)
    {
    int i,c=0,nC,nl;
    struct hostent*hp;
     struct _sTmp
     {
    	char szTmp[MAXHOSTIP][MAXHOSTLENGHT];
     }*sTmp;
    Getting this warning
    warning: assignment from incompatible pointer type
    what will be the right syntax.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if that last bit of code is to be believed sTmp points to a struct _sTmp? If so, the obvious (and, fortunately, correct) way to do this would be to memcpy sd->source to sTmp->szTmp. They're both two-dimensional arrays of chars, which means that the memory is already there (arrays allocate their own memory) and guaranteed to be both (a) in one continuous piece and (b) of the same size (assuming of course that sTmp and sd both point to legitimate structures and not to Hoboken).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, as for your errors...
    It's illegal to do a cast on the left side of the type you're assigning to. You must cast the source (what you're assigning) to the correct destination type (what you're assigning to). Note that in this case, as tabstop so kindly points out, it may not be the right thing to do.

    Example:
    Code:
    int myint = 0;
    float myfloat = 0.0f;
    int* pMyInt = NULL;
    pMyInt = (int*)&myfloat;
    /* Not (float*)pMyInt = &myfloat; */
    By an explicit cast, it works. But this example is not recommended practice.

    And the warning means that you're trying to assign a pointer of type Y* to type X*, or in other words, you're trying to assign a pointer pointing to a different type than the pointer you're trying to assign to.

    Example:
    Code:
    int myint = 0;
    float myfloat = 0.0f;
    int* pMyInt = NULL;
    pMyInt = &myfloat;
    This, for some reason, compiles in C, but is bad, because &myfloat is float* and pMyInt is int*, thus different types, hence the warning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You CAN use casts on the left-side, but not direct type conversions, e.g:
    Code:
    int *pInt;
    float f = 3.13;
    
    *(float **)&pInt = &f;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The standard is just full of loopholes and exceptions everywhere, isn't it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    The standard is just full of loopholes and exceptions everywhere, isn't it?
    Well, it's really about the rule of what a lvalue is: It is something that can be assigned to, and (float)blah is not a valid lvalue, but *(float *) is, for example [then, to make valid code, executable code, you have to make sure that casted value points to a valid memory location of the correct size for a float - but this construct is OFTEN used for addressing directly to hardware memory locations - rarely with float, but with specificly sized integers].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, the whole l-value vs r-value and rules and regulations are becoming so extremely complicated with all exceptions and rules it's impossible to tell what's possible and not.
    If a cast can be done before the l-value and it produces a correct l-value, then it should be possible. This also means a cast to a non-pointer type. Technically, it might create a new, temporary variable as a result of the operation, but then again, it might not - it could be implemented either way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Unfortunately, the whole l-value vs r-value and rules and regulations are becoming so extremely complicated with all exceptions and rules it's impossible to tell what's possible and not.
    If a cast can be done before the l-value and it produces a correct l-value, then it should be possible. This also means a cast to a non-pointer type. Technically, it might create a new, temporary variable as a result of the operation, but then again, it might not - it could be implemented either way.
    The rule is actually simple: an lvalue is anything that can be assigned to. A type-case of something can not be assigned to, a dereference of something that is a typecast of a pointer can.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That makes no sense. A cast changes the type of something to another, so changing from int to float should still be valid, without the use of pointers and dereferencing them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    That makes no sense. A cast changes the type of something to another, so changing from int to float should still be valid, without the use of pointers and dereferencing them.
    Yes, but remember: you're assigning to things. If you have char b and you try to assign something to (float) b, where are you going to put it? There's no memory address there. But assigning to a dereferenced pointer always has a memory address available (to wit, the pointer).

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's what I figured and which is also what I asked why the implementation cannot simply change the type of the variable assigned to first and then assign. Perhaps it wouldn't work if the type is too large, though.
    But in any case, as with any temporaries, the compiler could create a temporary to assign to, then assign to the destination.
    But I'm guessing this is not allowed in the standard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid lvalue in assignment
    By saipkjai in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2007, 03:19 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. invalid lvalue in assignment
    By antonis in forum C Programming
    Replies: 8
    Last Post: 10-09-2005, 12:00 PM