Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"filename" is not an lvalue, but occurs in .... What does this mean?

Posted on 2006-11-14
7
Medium Priority
?
521 Views
Last Modified: 2010-08-05
I've written a little program that opens a file and writes to it after querying a database.


I am getting this error : ""filename" is not an lvalue, but occurs in a context that requires one"

What does that mean?
0
Comment
Question by:bbcac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Author Comment

by:bbcac
ID: 17938770
#include <stdio.h>
#include <string.h>

main()
{

        char filename[20];
        filename = "testing\0";

     
}

Even this simple program doesn't work... why not? Here is the error
"filename" is not an lvalue, but occurs in a context that requires one.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 128 total points
ID: 17938778
You need:

strcpy ( filename, "testing" );

or

char * filename = "testing";

Paul
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 124 total points
ID: 17938805
That happens when you try to assign a value to a something that is not a variable that can be assigned values aka an 'lvalue' (see http://en.wikipedia.org/wiki/Lvalue), e.g.

char* get_file_name () {

  return "test.txt";
}

if (get_file_name() = some_buf) {

}

or

"I am not a lvalue" = get_file_name();
0
 
LVL 6

Assisted Solution

by:_iskywalker_
_iskywalker_ earned 124 total points
ID: 17948819
the problem is:
char filename[20];
isnt the same as:
char *filename;
filename=malloc(20);
so if you say:
filename="testing";
it will try to assign testing to filename[0], so assigning char * to char.
the solution with string copy is better, since it does something like:
char filename[20];
char * f="testing\0";
for(i=0; i< strlen ("testing); i++){
filename[i]=f[i];
}
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 124 total points
ID: 17972108
The basic problem is that C treats strings in a more primitive way than I think you're used to.  Here is the way the c compiler sees your code:

#include <stdio.h>
#include <string.h>

static char invisible_testing_constant_array[8] = { 't', 'e', 's', 't', 'i', 'n', 'g', '\0' };

main()
{
        char filename[20];
        filename = invisible_testing_constant_array;
}

You logically see filename as a string type, but C only understands arrays.  So the compiler thinks you're trying to assign an array to another array, which is not possible in C.  C only allows copies of basic types, structs, and unions.  For arrays, you have to copy each member individually, or call a library function like strcpy that performs many such copies.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question