Lvalue required? [C]

Getting a Lvalue required error in this line:

acct.pin = pina;

Both acct.pin and pina are string variables. Not sure why, but I didn't encounter that error when I tried this (from somewhere else in the program):

acct.bal = acct.bal - x;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define MAXAMT 9999999

typedef struct Account{
	char name[60];
	char num[20];
	char pin[10];
	long int bal;
} Account;

Account acct;

...

void changepin(){
	char pina[10];
	char pinb[10];

	printf("Please enter your current PIN: ");
	scanf("%7s", pina);

	if (strcmp (pina, acct.pin) != 0) {
		try += 1;
		printf("\nIncorrect PIN entered.");
		
		if (try > 3) {				
			printf("\nYou have exceeded the maximum attempts in\nchanging your PIN. This account will now close.");
			getch();
			exit(EXIT_SUCCESS);
		}
	} else {
		printf("Please key in your new 6-digit PIN: ");
		scanf("%7s", pina);
		
		if ((strlen(pina) > 6) || (strlen(pina) < 6)) {
			printf("\nPlease enter a 6-digit PIN.");
		} else if (strcmp(pina, acct.pin) == 0) {
			printf("\nYou cannot use the same PIN.");
		} else {
			printf("\nPlease re-enter your new PIN: ");
			scanf("%7s", pinb);
			
			if (strcmp(pina, pinb) != 0) {
				printf("\nEntered PINs do not match.");
			} else {
				acct.pin = pina;
				saveAcct();
				printf("\nPIN change successful.");
			}
		}
	}
	
	getch();
}
closed account (Dy7SLyTq)
i would just use strcpy(acct.pin, pina)
Thanks, that worked properly, but I'd still love to know why I got the lvalue required error...
closed account (Dy7SLyTq)
i dont think you can do = on char*s
I'd still love to know why I got the lvalue required error...

It's just how your compiler happened to phrase the error. Here are better phrasings:
clang
test.c:50:14: error: array type 'char [10]' is not assignable
                                acct.pin = pina;
                                ~~~~~~~~ ^

xlc:
"test.c", line 50.33: 1506-025 (S) Operand must be a modifiable lvalue.


The C language has this requirement for the assignment operator:

An assignment operator shall have a modifiable lvalue as its left operand


and it defines modifiable lvalue as

A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type
(emphasis mine)

In your case, acct.pin has array type, and therefore it is not a modifiable lvalue, and therefore it cannot be used on the left side of assignment

(the reasons why it is so go back to the B programming language)
Last edited on
Topic archived. No new replies allowed.