Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

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

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
bbcac
Asked:
bbcac
4 Solutions
 
bbcacAuthor Commented:
#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
 
PaulCaswellCommented:
You need:

strcpy ( filename, "testing" );

or

char * filename = "testing";

Paul
0
 
jkrCommented:
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
 
_iskywalker_Commented:
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
 
NovaDenizenCommented:
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

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now