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

Posted on 2006-11-14
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?
Question by:bbcac
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

Author Comment

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


        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.
LVL 16

Accepted Solution

PaulCaswell earned 32 total points
ID: 17938778
You need:

strcpy ( filename, "testing" );


char * filename = "testing";

LVL 86

Assisted Solution

jkr earned 31 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, e.g.

char* get_file_name () {

  return "test.txt";

if (get_file_name() = some_buf) {



"I am not a lvalue" = get_file_name();

Assisted Solution

_iskywalker_ earned 31 total points
ID: 17948819
the problem is:
char filename[20];
isnt the same as:
char *filename;
so if you say:
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++){
LVL 22

Assisted Solution

NovaDenizen earned 31 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' };

        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.

Featured Post

Independent Software Vendors: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

732 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