C-大小为1的无效读取

时间:2018-08-22 08:41:46

标签: c strcmp

好的,已经过去了15个多小时,我仍然不知道发生了什么! 这是hackerrank中时间转换问题的代码,一个函数接受一个字符串(时间为12小时AM / PM格式)并将其转换为军事(24小时)时间(返回字符串)

问题出在功能char* timeConversion(char* s)

中 在此代码行中的

b = strcmp(ampm,"PM");

它总是给我我听不懂的错误

  

“错误:大小为1的读取无效”

有人可以帮助我吗?!

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();

/*
 * Complete the timeConversion function below.
 */

/*
 * Please either make the string static or allocate on the heap. For example,
 * static char str[] = "hello world";
 * return str;
 *
 * OR
 *
 * char* str = "hello world";
 * return str;
 *
 */

/* Swaps strings by swapping pointers */
void swap(char **str1_ptr, char **str2_ptr)
{
  char *temp = *str1_ptr;
  *str1_ptr = *str2_ptr;
  *str2_ptr = temp;
}  

void reverse(char str[], int length)
{
    int start = 0;
    int end = length -1;
    while (start < end)
    {
        swap(*(str+start), *(str+end));
        start++;
        end--;
    }
}
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
    int i = 0;
    bool isNegative = false;

    /* Handle 0 explicitely, otherwise empty string is printed for 0 */
    if (num == 0)
    {
        str[i++] = '0';
        str[i] = '\0';
        return str;
    }

    // In standard itoa(), negative numbers are handled only with 
    // base 10. Otherwise numbers are considered unsigned.
    if (num < 0 && base == 10)
    {
        isNegative = true;
        num = -num;
    }

    // Process individual digits
    while (num != 0)
    {
        int rem = num % base;
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
        num = num/base;
    }

    // If number is negative, append '-'
    if (isNegative)
        str[i++] = '-';

    str[i] = '\0'; // Append string terminator

    // Reverse the string
    reverse(str, i);

    return str;
}

char* timeConversion(char* s) {
    /*
     * Write your code here.
     */
    char *result = (char*)calloc(8,sizeof(char)) ;
    char *ampm = (char*)calloc(2,sizeof(char)) ;    
    char *hh = (char*)calloc(2,sizeof(char)) ;

    int a = 0, b = 0 ,c = 0,i;

    long int dec = 0;
    int len = strlen(s);

    // substring     hh:mm:ssAM
    while ( c < 2)      // 2 : LENGTH
    {
        ampm[c] = s[9+c-1];    // 9 : position
        hh[c] = s[1+c-1];      // 1 : position
        c++ ; 
    } 

    // string to int 
    //len = strlen(ampm);
    for(i = 0; i < 2 ; i++)
    {
        dec = dec * 10 + (hh[i] - '0');
    }

    b = strcmp(ampm,"PM");
    a = strcmp(ampm,"AM"); 
    printf("%d\n",a);
    printf("%d\n",b);

    // processing
    if (!strcmp(ampm,"AM") && dec==12)  dec = 0;
    if (!strcmp(ampm,"PM") && dec!=12) dec += 12; 

    //if (strcmp(s[9],'A') && dec==12)  dec = 0;
    //if (strcmp(s[9],'P') && dec!=12)  dec += 12;

    // convert int back to string 
    char* hhh = itoa(dec, hh, 10);    
    //dec = atol(hh);
    // hh = itoa(dec,10);
    // snprintf(result,9,"%d", dec);


    //printf("%s\n",hh);
    c = 0;
    char* sub;
    while (c < 9)
    {
        sub[c] = s[3+c-1];
        c++ ;
    }

    strcat(result,hhh);
    strcat(result,sub);

    return result;

}

int main()
{
    char *s = "07:05:45PM"; 

    char* result = timeConversion(s);

    printf("%s\n", result);

    return 0;
}

2 个答案:

答案 0 :(得分:3)

就像提到的评论者一样,您似乎缺少NULL终止符,例如:

char *ampm = (char*)calloc(2,sizeof(char)) ;

两个字符('am'/'pm')加上NULL终止符将是 3个字符,而不是2。 您必须确保所有字符串的长度均为len +1,并且正确地以'\ 0'结尾。

答案 1 :(得分:2)

您的代码中有4个问题。

  1. 您没有为NULL的{​​{1}} char分配内存

    apmp

  2. 您将收到char *ampm = (char*)calloc(3,sizeof(char)) ;函数的双指针并传递swap

    char

    应该是

    void swap(char **str1_ptr, char **str2_ptr)

    然后调用如下的交换功能

    void swap(char *str1_ptr, char *str2_ptr)

  3. 您没有为swap((str+start), (str+end));指针分配内存

    sub

  4. 您没有为char* sub = malloc (9 * sizeof(char));hhsub分配内存。

    ampm