Thread: ???l-value???

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    ???l-value???

    I was just programming a cross-reference program incorporation linked lists into the program. Well, the line of code that gave me a problem was with a pointer to the struct:
    Code:
    list_ptr->data = line; /* line is gotten from command with fgets */
    Well, the error was that the left operand was not an l-value, or something like that. Something to do with "l-value". What is L-value? What is my error and how can I change it? Thanks.
    1978 Silver Anniversary Corvette

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    what kind of variables are you tring to assign? if it's a string, then stry using sprintf() or something. and L-Value is a number - an integer, double, float, etc.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, my variable 'line' is a string. Why is this a problem? How can I fix this? Thanks.
    1978 Silver Anniversary Corvette

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    So, how would I implement sprintf with my problem? Wouldn't that write to the output stream? Why would I need to do this? How would this fix my problem? Thanks.
    1978 Silver Anniversary Corvette

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    An L-value is any legal value that can be placed on the left side of the equal sign......

    For example:

    MyVariable = 5;

    Because you can assign a value to a variable in C, this is legal.

    5 = MyVariable;

    NOT LEGAL because you are trying to alter the value of the
    number 5, and you cannot do that. Let's say that MyVariable
    was hypothetically equal to 6 in the above statement, then
    you are trying to make the statement...

    5 = 6;

    and this is untrue.


    If you change your assignment statement to:

    line = list_ptr->data;

    it should compile with no problems, assuming all your other code
    is good, because the variable line IS a valid L-value;


    NOTE -- Review operator associativity if this is still unclear.

    Hope this helps!

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    But, why would I want to do this:

    > line = list_ptr->data;

    I don't want to assign the value of the data field from a struct to the char * line. I want to do the opposite. I want assign the char * line to list_ptr->data. These are two totally different statements. The first one (the one you said) changes line's value and the second changes list_ptr->data's value. I want to do this:

    > list_ptr->data = list;

    I want to assign data the value of list. Not the opposite. Is there a way to do this without getting the l-value error? Thanks.
    1978 Silver Anniversary Corvette

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Why wouldn't I be able to assign list_ptr->data the value of line? For the example you gave (with 5), I'm NOT changing a constant such as 5. I'm changing a pointer's field. Why is this not legal? Thanks again.
    1978 Silver Anniversary Corvette

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    sprintf(list_ptr->data,"%s",line);
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    And this sprintf function you gave me will just put the string into list_ptr->data as a string? I don't understand why this
    Code:
    list_ptr->data = line;
    is illegal. data isn't a constant, so why can't I change it? Thanks.
    1978 Silver Anniversary Corvette

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I tried the strcpy function and I tried sprintf. They both compiled with no errors, but then I had a runtime error. I think I tracked the error down to the functions strcpy (the first time) and sprintf (the second time). Here is the code. Tell me if you find the error.
    Code:
    /**************************************************
     ****	A cross-reference program illustrating	****
     ****	the use of linked lists			****
     ***************************************************/
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define NAMESIZE 50
    
    struct list {
    	char name[NAMESIZE];		/* Holds the name of reference */
    	struct list *next_list;	/* The next reference to name */
    };
    
    /***************************************************
     *	fndnm - searches through the linked list to
     *			find the given name
     *		
     *	parameters - pointer to first struct and name
     *						
     *	return - 0 if null, 1 if found
    ****************************************************/
    int fndnm(struct list *first_list, char *search)
    {
    	struct list *look_list;
    	/* start with the first struct */
    	look_list = first_list;
    
    	while (1)
    	{
    		if (look_list == NULL)
    			return 0;
    		else if (look_list->name == search)
    			return 1;
    		else
    			look_list = look_list->next_list;
    	}
    }
    
    int main()
    {
    	int counter, yesno;
    	struct list *init_list;
    	char line[100];
    
    	for (counter = 0; counter < 5; ++counter)
    	{
    		printf("Enter a name: ");
    		fgets(line, sizeof(line), stdin);
    		line[strlen(line)-1] = '\0';
    		sprintf(init_list->name, "%s", line);
    		/* goto next list */
    		init_list = init_list->next_list;
    	}
    	printf("Now, enter a name to search: ");
    	fgets(line, sizeof(line), stdin);
    	line[strlen(line)-1] = '\0';
    	yesno = fndnm(init_list, line);
    	if (yesno)
    		printf("Found = TRUE");
    	else
    		printf("Found = FALSE");
    	
    	return (0);
    }
    This is it. The first time I tried, I replaced sprintf(init_list->name, "%s", line) with strcpy(init_list->name, line). Thanks.
    1978 Silver Anniversary Corvette

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You need to use strcmp() to compare strings not the == operator.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Is this why I'm getting the runtime error? Why would I have to use strcmp instead of a boolean evaluation? Thanks.
    1978 Silver Anniversary Corvette

  13. #13
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I don't think that comparing with the == operator is the problem. The code didn't even get to that function. I tested (with printf) and I think the problem was with strcpy and sprintf. What do I do know? I'll try your answer anyway. It might work. I'm not really good at debugging...yet. Thanks.
    1978 Silver Anniversary Corvette

  14. #14
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You are getting runtime errors due to your list pointer (it doesn't point to anything). It is like trying to do

    int* a;
    a*=10;

    See my response in your other thread.

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Zen, this code

    > int* a;
    a*=10; <

    For it to be right (I understand you comparison) if should be

    int *a;
    a = 10;

    Right? Thanks.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed