Returning From Array Function in C

As I am learning I didn’t find the solution yet when I was trying to make a reusable code for mydigit extracts code to make it functinal.

So here is the code I tried but it’s not working;

#include<stdio.h>
//int main()
//{
//    int num,first;
//    int i,n;
//    int ary[10];
//    scanf("%d",&n);
//
//    while(n>0)
//    {
//       n=n%10;
//    for(i=0;i<n;i++){
//           ary[i]=n;
//        }
//       n=n/10;
//    }
//
//    for(i=0;i<n;i++){
//    printf("%d\n",ary[i]);
//    }
//    return 0;
//}
char *getDigit(int number){
    char digits[2000000];
    int i=0,d;
    while(number>0)
    {
        d=number%10;
        digits[i]=d;
        //printf("%s",digits);
        number=number/10;
        ++i;
    }
    digits[i]='\0';
    return digits;
}

int main()
{
    int number;
    scanf("%d",&number);
    char val=getDigit(number);
    printf("%d",val[0]);
//    int t,sum=0;
//    for(t=i-1; t>=0; t--)
//    {
//        printf("%d\t",digits[t]);
//        sum=sum+digits[t];
//    }
//    printf("\n Sum of all digits = %d",sum);

    return 0;
}


 

Solution can be foound from this link:
http://stackoverflow.com/questions/11656532/returning-an-array-using-c

https://www.tutorialspoint.com/cprogramming/c_return_arrays_from_function.htm

http://stackoverflow.com/questions/14297169/how-to-make-an-array-return-type-from-c-function

http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap7/section2.1.2.html

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.