[英]Given a time in -hour AM/PM format, convert it to military (24-hour) time
I am trying to solve this task on hackerrank but having problem when submitting solution.我正在尝试在hackerrank 上解决此任务,但在提交解决方案时遇到问题。 Here is my solution and the error got, I would like someone to point out my mistakes and if possible give a better solution to this question.(timeConversion() is the only code the user have to complete all the other functions are inbuilt)
这是我的解决方案和错误,我希望有人指出我的错误,如果可能的话,为这个问题提供更好的解决方案。(timeConversion()是用户必须完成所有其他功能的唯一代码是内置的)
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char* timeConversion(char* s)
{
int hr;
char h[2],mr, hrs[2],m[6];
mr=s[8];
for(int i=0; i<2; i++)
{h[i]=s[i];}
for(int i=0; i<5; i++)
{m[i]=s[i+3];}
hr= atoi(h);
if((mr=="A")&&(hr==12))
{strcpy(hrs, "00");}
if((mr=='P')&&(hr!=12))
{
hr=hr+12;
itoa(hr,hrs,10);
}
return strcat(hrs,m);
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
char* s = readline();
char* result = timeConversion(s);
fprintf(fptr, "%s\n", result);
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
This is the error I got这是我得到的错误
Solution.c: In function ‘timeConversion’:
Solution.c:38:11: warning: comparison between pointer and integer
if((mr=="A")&&(hr==12))
^~
Solution.c:38:11: warning: comparison with string literal results in unspecified behavior [-Waddress]
Solution.c:44:13: warning: implicit declaration of function ‘itoa’; did you mean ‘atol’? [-Wimplicit-function-declaration]
itoa(hr,hrs,10);
^~~~
atol
In file included from /usr/include/string.h:494,
from Solution.c:7:
In function ‘strcpy’,
inlined from ‘timeConversion’ at Solution.c:39:10:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:90:10: warning: ‘__builtin___memcpy_chk’ forming offset 3 is out of the bounds [0, 2] of object ‘hrs’ with type ‘char[2]’ [-Warray-bounds]
return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Solution.c: In function ‘timeConversion’:
Solution.c:29:19: note: ‘hrs’ declared here
char h[2],mr, hrs[2],m[6];
^~~
/usr/bin/ld: ./cct5tsGb.o: in function `timeConversion':
/tmp/submission/20200802/11/40/hackerrank-a082dac4956371ebbffb5bdc5098ce57/code/Solution.c:44: undefined reference to `itoa'
collect2: error: ld returned 1 exit status
Without looking to what the code does, I can point:无需查看代码的作用,我可以指出:
Solution.c:38:11: warning: comparison between pointer and integer
if((mr=="A")&&(hr==12))
^~
You are comparing a char to a char*.您正在将 char 与 char* 进行比较。 You should compare like below.
你应该像下面这样比较。
if((mr=='A')&&(hr==12))
The other issue is here:另一个问题在这里:
{strcpy(hrs, "00");}
The hrs
variable has only 2 bytes size, but you are trying to write 3 bytes: 0, 0 and null terminator. hrs
变量只有 2 个字节大小,但您尝试写入 3 个字节:0、0 和 null 终止符。 You need to change the variable size to 3.您需要将变量大小更改为 3。
Regarding the ´itoa´ warning, please see this: Where is the itoa function in Linux?关于“itoa”警告,请参阅: Linux 中的 itoa function 在哪里?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.