Solved

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

Posted on 2006-11-14
7
476 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
7 Comments
 

Author Comment

by:bbcac
Comment Utility
#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
Comment Utility
You need:

strcpy ( filename, "testing" );

or

char * filename = "testing";

Paul
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 31 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now