• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C programming problem need help

kimagurealex

Senior member
Hi, I am trying to write a C program. However, I am having a problem right now. First, I am trying to use a struct pointer in the left hand side of the = sign. like this:
void apes (INPUT *studentptr, OUTPUT *resultptr)
{
int i;

for (i=0; i<MAX; i++)
{
(resultptr)->id = (studentptr+i)->id; //LINE 74
(resultptr)->average = ((studentptr+i)->totalpoints) / ((studentptr+i)->teststaken);
resultptr++ ;
}
}//C:\cpp\project4.cpp(74) : error C2106: '=' : left operand must be l-value
How can I make this work? by the way, how can i rewind the pointer back to the original place (resultptr) .
Thanks.
oh, the resultptr is a pointer of an array
 
Thanks to reply my posts, guys.(if u are guys... 🙂)
1. do they point to the same value?

student.average //Dot notation
(*studentptr).average //Indirection notation
studentptr->average? //Selection Notation

//INPUT STRUCT
typedef struct input
{
char id[20];
int totalpoints;
int teststaken;
} INPUT;

//OUTPUT Struct
typedef struct output
{
char *id; //at this part, when I use *id instead of id[20], the compiler won't give me problem.
// char id[20];
float average;
char letter;
float gpa;
} OUTPUT;

the ERROR message is:
"C:\cpp\project4.cpp(74) : error C2106: '=' : left operand must be l-value" //see the top message.

I have checked the l-value and "studentptr->average" seems like within the standard lvalue expressions.
Can someone explain the different and advantage of using *id and id[20]?
Thanks
 
Can someone explain the different and advantage of using *id and id[20]?

Certainly.

Consider the following expression:

char id[20];
id = "Nope"; // this won't work

Your compiler was complaining about an l-value because in C et al, an array is a *non-modifiable* l-value. C (I'll no longer say et al at this point, so assume C++ et al 😉) makes the distinction between a modifiable and non-modifiable l-values. Consider the following expression:

char *id;
id = "Nope";

This works because a pointer is a modifiable l-value, so you can point it to another location. So, modifiable l-values you can reassign, non-modifiable l-values (arrays), you cannot. Have a look at the following...

char id[] = "One way of doing it";

char id[20] = "Another way";

char *id = "Yet another";

char id[20] = { 'A', 'n', 'o', 't', 'h', 'e', 'r' };
// note in the above declaration that although i didn't explicitly add the null terminating char, it would be superfluous to do so
// due to the fact that C will zero the memory of an initialized array...

char id[20];
strcpy(id, "Isn't this fun?");

The key point in all this mess is that arrays are non-modifiable l-values.

HTH
 
Back
Top