Solved

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

Posted on 2006-11-14
7
510 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 32 total points
ID: 17938778
You need:

strcpy ( filename, "testing" );

or

char * filename = "testing";

Paul
0
 
LVL 86

Assisted Solution

by:jkr
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 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 31 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 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' };

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

Industry Leaders: 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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

756 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