A.length与A->length有区别吗?
//设A=(a1,a2...,an)和B=(b1,b2...,bn)均为顺序表,比较A与B大小#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW 0
#define LEN sizeof(Sqlist)
typedef struct {
char elem[LIST_INIT_SIZE];
int length;
int listsize;
}Sqlist;
void InitList_Sq(Sqlist **L) {
/*构造一个空的线性表*/
*L=(Sqlist*)malloc(LEN);
if (!*L) exit(OVERFLOW);
(*L)->length = 0;
(*L)->listsize = LIST_INIT_SIZE;
} /*InitList_Sq*/
void Comparison_Sq(Sqlist *A, Sqlist *B) {
int i = 0;
while( i < A->length && i < B->length )
{
if( A->elem[i] < B->elem[i] )
{
printf("A < B\n");
return ;
}
else if( A->elem[i] > B->elem[i] )
{
printf("A > B\n");
return ;
}
else
{
i ++;
}
}
if(A->length < B->length )
{
printf("A < B\n");
return ;
}
else if ( A->length > B->length )
{
printf("A > B");
return ;
}
else
{
printf("A == B");
return ;
}
}
void main()
{
Sqlist *A=NULL,*B=NULL;
int i=0, j=0;
char flag;
InitList_Sq(&A); //构造空表A
InitList_Sq(&B); //构造空表B
printf("请输入A的字母序列:\n");
while(scanf("%c",&flag),A->listsize>= i) {
if (flag == '\n') break;
else {
A->elem[i]=flag;
flag = NULL;
A->length++;
i++;
}
}
printf("请输入B的字母序列:\n");
while(scanf("%c",&flag),B->listsize>=j) {
if (flag == '\n') break;
else {
B->elem[j]=flag;
flag = NULL;
B->length++;
j++;} }
/*比较:*/
Comparison_Sq(A,B);
free(A); /*释放A*/
free(B); /*释放B*/
}
以上是我同学发给我的程序,他原来所有的->符号都是.的,但编译器则认为是错误的:如在while( i < A->length && i < B->length )
{
if( A->elem[i] < B->elem[i] )
{
printf("A < B\n");
return ;
}
改为:while( i < A.length && i < B.length )
{
if( A.elem[i] < B.elem[i] )
{
printf("A < B\n");
return ;
}
vc编译器则提示:
error C2228: left of '.length' must have class/struct/union type
D:\My Documents\My QQ Files\2.12.cpp(28) : error C2228: left of '.length' must have class/struct/union type
D:\My Documents\My QQ Files\2.12.cpp(28) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
于是我就想到A.length与A->length有区别吗?在谭浩强的c中的第269页则说:
1、结构体变量.成员名;
2、(*p).成员名
3、p->成员名
是等价的;可为什么在这个程序中却又区别呢????
难道真的有区别?如果有,区别有是什么??
在这里,小弟虚心向各位大侠请教咯~
谢谢~