指针:表达式不可赋值
Pointers: Expression is not assignable
我正在编写一个函数,其目的是更改结构变量的值。我收到错误消息,指出第一个函数末尾的表达式不可赋值。那么,如何让函数更改 daate 结构的值?
这是我的代码:
#include <stdio.h>
#include <stdbool.h>
struct date
{
int month;
int day;
int year;
};
struct date dateUpdate (struct date *dait) {
struct date tomorrow;
int numberOfDays (struct date d);
if (dait->day != numberOfDays (*dait) ) {
tomorrow.day = dait->day + 1;
tomorrow.month = dait->month;
tomorrow.year = dait->year;
}
else if ( dait->month == 12 ) { //end of year
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = dait->year + 1;
}
else { //end of month
tomorrow.day = 1;
tomorrow.month = dait->month + 1;
tomorrow.year = dait->year;
}
&dait->day = tomorrow.day;
&dait->month = tomorrow.month;
&dait->year = tomorrow.year;
return *dait;
}
//Function to find the number of days in a month;
int numberOfDays (struct date d)
{
int days;
bool isLeapYear (struct date d);
const int daysPerMonth[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( isLeapYear (d) && d.month == 2 )
days = 29;
else
days = daysPerMonth[d.month - 1];
return days;
}
}
int main (void) {
struct date daate;
daate.day = 21;
daate.month = 6;
daate.year = 2013;
dateUpdate(&daate);
printf("%d", daate.day);
}
正确的语法(您已经在函数中使用过)如下
dait->day = tomorrow.day;
dait->month = tomorrow.month;
dait->year = tomorrow.year;
请注意,您可以简单地编写
而不是这三个语句
*dait = tomorrow;
dait
已经是指针,所以 &dait
是指向指针的指针,因此它没有要取消引用的字段。
解决办法:把dait
前的&
去掉。
我正在编写一个函数,其目的是更改结构变量的值。我收到错误消息,指出第一个函数末尾的表达式不可赋值。那么,如何让函数更改 daate 结构的值?
这是我的代码:
#include <stdio.h>
#include <stdbool.h>
struct date
{
int month;
int day;
int year;
};
struct date dateUpdate (struct date *dait) {
struct date tomorrow;
int numberOfDays (struct date d);
if (dait->day != numberOfDays (*dait) ) {
tomorrow.day = dait->day + 1;
tomorrow.month = dait->month;
tomorrow.year = dait->year;
}
else if ( dait->month == 12 ) { //end of year
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = dait->year + 1;
}
else { //end of month
tomorrow.day = 1;
tomorrow.month = dait->month + 1;
tomorrow.year = dait->year;
}
&dait->day = tomorrow.day;
&dait->month = tomorrow.month;
&dait->year = tomorrow.year;
return *dait;
}
//Function to find the number of days in a month;
int numberOfDays (struct date d)
{
int days;
bool isLeapYear (struct date d);
const int daysPerMonth[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( isLeapYear (d) && d.month == 2 )
days = 29;
else
days = daysPerMonth[d.month - 1];
return days;
}
}
int main (void) {
struct date daate;
daate.day = 21;
daate.month = 6;
daate.year = 2013;
dateUpdate(&daate);
printf("%d", daate.day);
}
正确的语法(您已经在函数中使用过)如下
dait->day = tomorrow.day;
dait->month = tomorrow.month;
dait->year = tomorrow.year;
请注意,您可以简单地编写
而不是这三个语句*dait = tomorrow;
dait
已经是指针,所以 &dait
是指向指针的指针,因此它没有要取消引用的字段。
解决办法:把dait
前的&
去掉。