C 无法创建字符串数组。我做错了什么?
我正试图编写一个程序,将日期转换为一周中的某一天 我认为我遇到的主要问题是,我没有正确地为周名称创建数组,因此,我无法将其传递回主函数进行打印 这是我的密码。我提前道歉,如果它是混乱的,我正在自学通过书籍编码,并没有这样做太久 任何帮助都将不胜感激,非常感谢:)C 无法创建字符串数组。我做错了什么?,c,arrays,C,Arrays,我正试图编写一个程序,将日期转换为一周中的某一天 我认为我遇到的主要问题是,我没有正确地为周名称创建数组,因此,我无法将其传递回主函数进行打印 这是我的密码。我提前道歉,如果它是混乱的,我正在自学通过书籍编码,并没有这样做太久 任何帮助都将不胜感激,非常感谢:) #包括 结构日期 { 国际日; 整月; 国际年; }; 内部主(空) { int N; 焦日; 结构日期; int nConvert(结构日期); 字符日查找器(int N); printf(“好的,选择您的日期:\n”); scanf
#包括
结构日期
{
国际日;
整月;
国际年;
};
内部主(空)
{
int N;
焦日;
结构日期;
int nConvert(结构日期);
字符日查找器(int N);
printf(“好的,选择您的日期:\n”);
scanf(“%i:%i:%i”、&date.day、&date.month、&date.year);//获取日期并使用date结构创建变量
N=nConvert(date);//将日期转换为整数
day=dayFinder(N);//将N转换为一周中的一天
//现在,我们可以打印出在dayFinder中创建的一周中的哪一天
printf(“一周中%i的哪一天,%i是:%i%i%i\n”,date.day,date.month,date.year,day[0],day[1],day[3]);
}
int nConvert(结构日期N)
{
int gee(struct DATE);//声明函数以获取gee和eff(转换公式所需)
int eff(结构日期);
int f=eff(N);//调用函数,以便将输出放入“result”的等式中
int g=gee(N);
int result=(1461*f)/4+(153*g)/5+N.day;//将方程的结果存储到“result”中
返回结果;//将结果放回main,将被调用'N'
}
int eff(结构日期工作)
{
如果(work.month您的代码有3个重要问题,这表明在理解基本概念方面存在严重问题
你的char day[7]
声明不是你想的那样
这种语言中的char
既不是字符串也不是数组。您的char day[7]
只是一个7字节的数组,您不能按照自己的意愿存储7个字符串
您需要char*day[7]
这是一个由7个指针组成的数组,但是看看函数的其余部分,绝对不需要数组
您可以使用数组来避免使用字符串文本的许多if
语句,因为字符串文本存储在内存中的某个位置,允许从函数返回它们,即使它们被声明为函数的局部变量,但通常不能返回地址(或指针)局部变量,因为它们仅在声明它们的函数的堆栈框架内分配,并且当函数返回堆栈指针时,堆栈指针被重置到函数调用之前的位置,因此,函数的所有局部数据不再可访问,尽管它可能仍然存在,但无法保证,因此你不应该这样做
一个工作良好的函数来做你想做的事情
const char *get_day_name(int week_day)
{
const char *days[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
return days[week_day];
}
返回一个字符的值,然后尝试像访问数组一样访问它,编译器不允许这样做,因此程序肯定不会编译
您的“字符串”都缺少终止null
字节,中的每个字符串都必须以null
字节'\0'
终止,所有需要字符串的函数都需要该字节来告诉字符串的结尾在哪里,如果它不存在,则将其传递给类似printf()的函数
将导致未定义的行为,尽管您的目的似乎是打印日字符串的每个字符,在这种情况下,您不需要终止的null
字节,但您应该习惯字符串需要这个字节。这是该语言新程序员常见的问题
你的代码中有很多问题,我解决了其中一些问题
#include <stdio.h>
typedef struct
{
int day;
int month;
int year;
} DATE;
int nConvert(DATE N);
int eff(DATE work);
int gee(DATE work);
char* getName(int week_day);
int main (void)
{
int N;
char* day;
DATE date;
printf("Okay, choose your day:\n");
scanf("%i:%i:%i", &date.day, &date.month, &date.year );
N = nConvert(date); //go convert the date into an integer
day = getName(N); //go convert N and turn it into a day of the week
// now we can print out the day of the week that we created in dayFinder
printf("The day of the week of %i, %i, %i is: %s\n", date.day, date.month, date.year, day);
return 0;
}
int nConvert(DATE N)
{
int f = eff(N); //call the functions so the outputs can be put into the equation for 'result'
int g = gee(N);
int result = (1461 * f) / 4 + (153 * g) / 5 + N.day; //store the result of the equation into a 'result'
return result; //go put result back into main, will be called 'N'
}
int eff(DATE work)
{
if(work.month <= 2)
{
work.year = work.year - 1;
}
else
{
work.year = work.year;
}
return work.year;
}
int gee( DATE work)
{
if(work.month <= 2)
{
work.month = work.month + 13;
}
else
{
work.month = work.month + 1;
}
return work.month;
}
char* getName(int day)
{
const char* days[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
return days[day%7];
}
但是,您仍然在使用指针,并且无法真正避免它们。我对这本书很熟悉:Stephen Kochan编写的第四版C编程
下面是一种不直接使用指针或字符串来完成练习的方法。它使用switch语句来打印文本(字符串)
/*第4版C语言编程练习8.4
书中说2004年8月8日是星期二;
实际上那是一个星期天
本练习所依据的书中练习8.2也包含
一个错误--在8月8日之间有198天(不是202天)过去了,
二○○四年及二○○五年二月二十二日
*/
#包括
结构日期
{
国际日;
整月;
国际年;
};
整数f(整数年,整数月)
{
int值;
如果(请不要在函数中声明函数,在全局范围内声明它们是正常的,因为函数意味着从任何地方调用。查看字符串和char
之间的区别。您有一个char
数组,而不是字符串数组或char
数组。感谢您如此快速地回复,这实际上是“C语言编程”中的一个练习,在“字符串”一章之前,所以我遇到了很多麻烦。我知道我以后会详细了解这方面的内容,我只是不确定我是否能够在没有所有这些的情况下完成它。除非我应该在不使用字符串的情况下完成它。我也没有去注意指针,这样我就不能使用它们了!不,你不能回避它们。不管怎样,你都会使用指针。不管你知道它们是指针还是不是指针。哈哈,我知道我最终必须使用它们!我只是假设这本书希望我在没有他们看到的情况下解决这些练习,因为它还没有教给我是的,我以前对它们一无所知。否则就不可能了吗?@MattCleary你有没有见过像prin这样的“静态字符串文字”
#include <stdio.h>
typedef struct
{
int day;
int month;
int year;
} DATE;
int nConvert(DATE N);
int eff(DATE work);
int gee(DATE work);
char* getName(int week_day);
int main (void)
{
int N;
char* day;
DATE date;
printf("Okay, choose your day:\n");
scanf("%i:%i:%i", &date.day, &date.month, &date.year );
N = nConvert(date); //go convert the date into an integer
day = getName(N); //go convert N and turn it into a day of the week
// now we can print out the day of the week that we created in dayFinder
printf("The day of the week of %i, %i, %i is: %s\n", date.day, date.month, date.year, day);
return 0;
}
int nConvert(DATE N)
{
int f = eff(N); //call the functions so the outputs can be put into the equation for 'result'
int g = gee(N);
int result = (1461 * f) / 4 + (153 * g) / 5 + N.day; //store the result of the equation into a 'result'
return result; //go put result back into main, will be called 'N'
}
int eff(DATE work)
{
if(work.month <= 2)
{
work.year = work.year - 1;
}
else
{
work.year = work.year;
}
return work.year;
}
int gee( DATE work)
{
if(work.month <= 2)
{
work.month = work.month + 13;
}
else
{
work.month = work.month + 1;
}
return work.month;
}
char* getName(int day)
{
const char* days[7] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
return days[day%7];
}
int main (void)
{
int N;
DATE date;
printf("Okay, choose your day:\n");
scanf("%i:%i:%i", &date.day, &date.month, &date.year );
N = nConvert(date); //go convert the date into an integer
char names[7][3] = {
{'S', 'u', 'n'},
{'M', 'o', 'n'},
{'T', 'u', 'e'},
{'W', 'e', 'd'},
{'T', 'h', 'u'},
{'F', 'r', 'i'},
{'S', 'a', 't'}
};
int dayNum = N%7;
// now we can print out the day of the week that we created in dayFinder
printf("The day of the week of %i, %i, %i is: %c%c%c\n", date.day, date.month, date.year, names[dayNum][0], names[dayNum][1], names[dayNum][2]);
return 0;
}
/* exercise 8.4 from Programming in C, 4th edition
the book states in this exercise that August 8, 2004 was a Tuesday;
actually it was a Sunday
exercise 8.2 in the book, on which this exercise is based, also contains
an error -- there are 198 elapsed days (not 202 days) between August 8,
2004 and February 22, 2005
*/
#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
int f (int year, int month)
{
int value;
if ( month <= 2 )
value = year - 1;
else
value = year;
return value;
}
int g (int month)
{
int value;
if ( month <= 2 )
value = month + 13;
else
value = month + 1;
return value;
}
int calculateN (struct date d)
{
return 1461 * f (d.year, d.month) / 4 + 153 * g (d.month) / 5 + d.day;
}
int main (void)
{
struct date date1;
printf("\nOkay, choose your date (day, month, year): ");
scanf("%i%i%i", &date1.day, &date1.month, &date1.year);
printf ("\nday of the week for %i/%i/%i is ", date1.day, date1.month, date1.year);
switch ((calculateN (date1) - 621049) % 7)
{
case 0:
printf ("Sunday");
break;
case 1:
printf ("Monday");
break;
case 2:
printf ("Tuesday");
break;
case 3:
printf ("Wednesday");
break;
case 4:
printf ("Thursday");
break;
case 5:
printf ("Friday");
break;
case 6:
printf ("Saturday");
break;
default:
printf ("error ");
break;
}
printf ("\n");
return 0;
}