阅读背景:

即使在编译后也会出现分段错误?

来源:互联网 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>

struct Result{
    int reversen;
    unsigned short count;
};

struct Result *myCount(int, int);
int main()
{
    struct Result *result;
    myCount(122333, 2);
    printf("\nReversed integer of 122333: %d", result->reversen);
    printf("\nOccurrence of 2 in 122333: %hu", result->count);  
    return 0;
}

struct Result * myCount(int n, int d)
{
    struct Result * result = (struct  Result *) malloc (sizeof (struct  Result));
    if (result == NULL)
    {
        fprintf(stderr, "\nNo storage space available\n");
        exit(0);
    }
    int rev_dig, last_dig, digit,  count = 0;
    while (n != 0)
    {
        last_dig = n % 10;
        rev_dig = rev_dig * 10 + last_dig;
        n = n / 10;
    }
    rev_dig = rev_dig * (-1);   
    result->reversen = rev_dig;

    while (n >= 1)
    {
        digit = n % 10;
        if (digit == d)
            count++;
        n = n / 10;
    }
    result->count = count;  
}
#include <stdio.h> 
#include <stdlib.h> 
#inclu



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: