I'm not sure if I'm asking the right question. lets say we have the following
我不确定我是否在问正确的问题。我们可以说有以下内容
typedef struct {
char month[2];
char day[2];
char year[4];
} dateT;
void dates(dateT* ptrDateList);
int main()
{
dateT* ptrList;
scanf("%d", &n);//number of date entries
ptrList = malloc(n*sizeof(dateT));
for (i=0; i<n; i++)
dates(&ptrList[i]);
}
void dates(dateT* ptrDateList);
{
char inputMonth[2];
char inputDay[2];
char inputYear[4];
scanf("%s",inputMonth);
strcpy(ptrDateList->month,inputMonth);
scanf("%s",inputDay);
strcpy(ptrDateList->day,inputDay);
scanf("%s",inputYear);
strcpy(ptrDateList->year,inputYear);
}
how you compare the value of day in ptrList[2] with lets say the value of day in ptrList[5]
你如何比较ptrList [2]中的day值与ptrList [5]中的day值相比较
I know that if I did
我知道如果我这样做了
dateT list2={5,10,2009};
dateT list5={7,10,2009};
I could do
我可以
list2.day == list5.day
but how would I do that with the arrays
但是如何使用数组做到这一点
2 个解决方案
#1
1
ptrList[2].day == ptrList[5].day
would work if the type was say integer, but as you store the characters you might want to use strcmp
, like so:
ptrList [2] .day == ptrList [5] .day可以工作,如果类型是整数,但是当你存储字符时你可能想要使用strcmp,如下所示:
if ((strcmp(ptrList[2].day,ptrList[5].day) == 0) // same day
note that you need an extra character for the end of string sentinel \0
, so it should be;
请注意,字符串sentinel \ 0的结尾需要一个额外的字符,所以它应该是;
typedef struct {
char month[3];
char day[3];
char year[5];
} dateT;
another issue is the way you handle input: can you be sure that the user will provide valid input? for instance you could use scanf("%2s", string);
to input the day (of max. length two).
另一个问题是你处理输入的方式:你能确定用户会提供有效的输入吗?例如,你可以使用scanf(“%2s”,string);输入日期(最大长度为2)。
#2
1
Here's pretty much what jev already explain. Just I thought I might as well post it, since it works.
这几乎是jev已经解释过的。只是我想我也可以发布它,因为它有效。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char month[3];
char day[3];
char year[5];
} dateT;
void dates(dateT* ptrDateList);
int main()
{
dateT* ptrList;
int i, n;
printf("Enter number of dates: ");
scanf("%d", &n);//number of date entries
ptrList = malloc(n*sizeof(dateT));
for (i=0; i<n; i++)
dates(&ptrList[i]);
if (n > 1) {
if (!strcmp(ptrList[0].day,ptrList[1].day)) {
printf("First two days match.\n");
} else {
printf("First two days don't match.\n");
}
}
return 0;
}
void dates(dateT* ptrDateList)
{
char inputMonth[3];
char inputDay[3];
char inputYear[5];
printf("Enter month: ");
scanf("%2s",inputMonth);
inputMonth[2] = '\0';
strcpy(ptrDateList->month,inputMonth);
printf("Enter Day: ");
scanf("%2s",inputDay);
inputDay[2] = '\0';
strcpy(ptrDateList->day,inputDay);
printf("Enter Year: ");
scanf("%4s",inputYear);
inputYear[4] = '\0';
strcpy(ptrDateList->year,inputYear);
}
#1
1
ptrList[2].day == ptrList[5].day
would work if the type was say integer, but as you store the characters you might want to use strcmp
, like so:
ptrList [2] .day == ptrList [5] .day可以工作,如果类型是整数,但是当你存储字符时你可能想要使用strcmp,如下所示:
if ((strcmp(ptrList[2].day,ptrList[5].day) == 0) // same day
note that you need an extra character for the end of string sentinel \0
, so it should be;
请注意,字符串sentinel \ 0的结尾需要一个额外的字符,所以它应该是;
typedef struct {
char month[3];
char day[3];
char year[5];
} dateT;
another issue is the way you handle input: can you be sure that the user will provide valid input? for instance you could use scanf("%2s", string);
to input the day (of max. length two).
另一个问题是你处理输入的方式:你能确定用户会提供有效的输入吗?例如,你可以使用scanf(“%2s”,string);输入日期(最大长度为2)。
#2
1
Here's pretty much what jev already explain. Just I thought I might as well post it, since it works.
这几乎是jev已经解释过的。只是我想我也可以发布它,因为它有效。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char month[3];
char day[3];
char year[5];
} dateT;
void dates(dateT* ptrDateList);
int main()
{
dateT* ptrList;
int i, n;
printf("Enter number of dates: ");
scanf("%d", &n);//number of date entries
ptrList = malloc(n*sizeof(dateT));
for (i=0; i<n; i++)
dates(&ptrList[i]);
if (n > 1) {
if (!strcmp(ptrList[0].day,ptrList[1].day)) {
printf("First two days match.\n");
} else {
printf("First two days don't match.\n");
}
}
return 0;
}
void dates(dateT* ptrDateList)
{
char inputMonth[3];
char inputDay[3];
char inputYear[5];
printf("Enter month: ");
scanf("%2s",inputMonth);
inputMonth[2] = '\0';
strcpy(ptrDateList->month,inputMonth);
printf("Enter Day: ");
scanf("%2s",inputDay);
inputDay[2] = '\0';
strcpy(ptrDateList->day,inputDay);
printf("Enter Year: ");
scanf("%4s",inputYear);
inputYear[4] = '\0';
strcpy(ptrDateList->year,inputYear);
}