I think I've got the logic of the problem correctly(although a bit messy)

Initally, I had done this with static variable of size 10 but the challenge is to take very large inputs (upto 10^100) so I went the dynamic way and stuck again

So far I know, the error is lying after line no. 42 in the malloc of my struct num b. But I can't understand why.

Code:
#include<stdio.h>
#include<stdlib.h>
#define MAXDIGITS 10000000000
struct num
{
    int ndigits;
    char *d;
} a,b;
int main(void)
{
    a.ndigits=0;
    b.ndigits=0;
    a.d=(char*)malloc(MAXDIGITS*sizeof(char));
    b.d=(char*)malloc(MAXDIGITS*sizeof(char));
    int i,j,k,t,flag=0;
    scanf("%d",&t);
    fflush(stdin);
    while (t!=0)
    {
        char p,q,r,x;
        while(1)
        {
            scanf("%c",&x);
            if (x=='\n') break;
            //printf("%c",x);
            a.d[a.ndigits]=x;
            a.ndigits+=1;
        }
        //printf("\ndigits= %d",a.ndigits);
        //for(i=0;i<a.ndigits;i++)
        //printf("digit: \n%c",a.d[i]);
        for(i=0; i<a.ndigits; i++)
        {
            for(j=0; j<a.ndigits; j++)
            {
                printf("\ni= %d j= %d",i,j);
                if (i==j) continue;
                p=a.d[i];
                q=a.d[j];
                r=(p-'0')*10+(q-'0');
                printf("\np= %c q= %c r= %c",p,q,r);
                if (r>=65 && r<=90)
                {
                    flag=1;
                    int rf=0;
                    for(k=0; k<b.ndigits; k++){printf("%d",k);
                        if (r==b.d[k])
                        {
                            rf=1;
                            break;
                        }
                        }
                    if (rf!=1)
                    {
                        b.d[b.ndigits]=r;
                        b.ndigits+=1;
                        rf=0;
                    }
                }
            }
        }
        for(i=0; i<b.ndigits-1; i++)
            for(j=0; j<b.ndigits-i-1; j++)
                if (b.d[j] > b.d[j+1])
                {
                    x=b.d[j];
                    b.d[j]=b.d[j+1];
                    b.d[j+1]=x;
                }
        if(flag==1)
            for(i=0; i<b.ndigits; i++)
                printf("%c",b.d[i]);
        else printf("\n");
        t--;
    }
    return 0;
}