I'm a beginner in C and below is a program to find the position of a given digit. My first function works but i can't say the same for the 2nd one(digitPos2) that returns the value from a pointer. I'm not sure what is wrong and why.
#include <stdio.h>
int digitPos1(int num, int digit);
void digitPos2(int num, int digit, int *result);
int main()
{
int number, digit, result=0;
printf("Enter the number: \n");
scanf("%d", &number);
printf("Enter the digit: \n");
scanf("%d", &digit);
printf("digitPos1(): %d\n", digitPos1(number, digit));
digitPos2(number, digit, &result);
printf("digitPos2(): %d\n", result);
main();//return 0;
}
int digitPos1(int num, int digit)
{
int pos=0;
while(num)
{
if(num%10 == digit)
{
return pos = pos + 1;
}
else
{
pos++;
num = num/10;
}
}
}
void digitPos2(int num, int digit, int *result)
{
int pos=0;
while(num)
{
if(num%10 == digit)
{
pos = pos + 1;
*result = pos;
}
else
{
pos++;
num = num/10;
}
}
*result = 0;
}