Thread: modifiable lvalue error

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    modifiable lvalue error

    Fellows,

    Please help me to get thru this error and get it running:

    error:
    "filetolinkedlist.c", line 35: error #2137: expression must be a modifiable
    lvalue
    lista->line=lineinput;

    code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *fr;
    char filename[100];
    
    struct filecontents{
    	char line[200];
    	struct filecontents *next;
    };
    
    void printfilecontents(struct filecontents *lista)
    {
    	int contador=0;
    	
    	if (lista == NULL)
    	{
    		printf("0 registros\n");
    	}
    	while (lista != NULL)
    	{
    		printf("print %p %p %d\n", lista, lista->next, lista->line);
    		lista = lista->next;
    	}
    	printf("%d registros\n", contador);
    }
    
    struct filecontents *add(struct filecontents **p, char lineinput[20])
    {
    	struct filecontents *lista = malloc(sizeof(struct filecontents));
    	if (lista == NULL)
    		return NULL;
    	lista->next = *p;
    	*p = lista;
    	lista->line=lineinput;
    	return *p;
    }
    
    main(){
    	char line[200];
    	struct filecontents *lista = NULL;
    
    	printf("Informe o nome do arquivo que deseja importar para a lista: ");
    	scanf("%s", filename);
    	fflush(stdin);
    	fr = fopen(filename, "rt");
    	while(fgets(line, 200, fr)){
    		add(&lista, line);
    	}
    	fclose(fr);
    	printfilecontents(lista);
    }
    Please someone help me fix it. I just need to get it running, no best practices tips are needed.

    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't assign to an array. If you want to copy a string, you need to use strcpy.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    On a side note...

    Quote Originally Posted by tsantana View Post
    Code:
    scanf("%s", filename);
    fflush(stdin);
    fr = fopen(filename, "rt");
    First line could lead to seg faults, as it's an unsafe way of reading a string. You know how fgets works, so why are you using scanf() here?

    Second line is not valid in pure, standard C, but only allowed in some compilers by extension. It may or may not do what you think it will.

    The third line, while perhaps redundant with the 't' parameter, is never checked to see if the operation succeeded. You should probably be checking to see if fr is NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM