# Returning a value with pointer ( C program)

Ley Clo Source

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;
}
``````

Output

calgorithmfunctionwhile-loopdo-while

The both functions are invalid.

For starters it is better to set the parameters `num` and `digit` as having the type `unsigned int`. Otherwise the functions will be more complicated. That is you will need to check whether one of the parameters or both are negative.

Secondly the function `digitPos1` has undefined behavior because it returns nothing in case when the digit is not present in the number.

Moreover instead of the while loop you should use a do-while loop because `num` can be set by the user to `0` and `0` is a valid number.

This statement in the function `digitPos2`

``````*result = 0;
``````

does not make sense.

And you have to exut the function if the digit is found

``````while(num)
{
if(num%10 == digit)
{
pos = pos + 1;
*result = pos;
// exit the function
}
//...
``````

Also it is unclear how the user can determine whether the digit is not present in the number.

You can use an approach that returns for example `-1` in case the digit is not present in the number.

Pay attention to that according to the C Standard the function `main` without parameters shall be declared like

``````int main( void )
``````

Here is a demonstrative program

``````#include <stdio.h>

int digitPos1( unsigned int num, unsigned int digit);
void digitPos2( unsigned int num, unsigned int digit, int *result);

int main( void )
{
unsigned int number, digit;
int result;

printf( "Enter the number: " );
scanf( "%d", &number );

printf( "Enter the digit: " );
scanf( "%d", &digit );

printf("digitPos1(): %d\n", digitPos1( number, digit ) );

digitPos2(number, digit, &result);
printf( "digitPos2(): %d\n", result );

return 0;
}

int digitPos1( unsigned int num, unsigned int digit )
{
const unsigned int Base = 10;
int pos = -1;
int n = 0;

do
{
if ( num % Base == digit )
{
pos = n;
}
else
{
++n;
}

} while ( ( num /= Base ) && ( pos == -1 ) );

return pos;
}

void digitPos2( unsigned int num, unsigned int digit, int *result )
{
const unsigned int Base = 10;
int n = 0;

*result = -1;
do
{
if ( num % Base == digit )
{
*result = n;
}
else
{
++n;
}

} while ( ( num /= Base ) && ( *result == -1 ) );
}
``````

The program output might look for example like

``````Enter the number: 123456
Enter the digit: 2
digitPos1(): 4
digitPos2(): 4
``````

The position startrs from 0.

answered 6 months ago Saikat Das #2

First of all, In case the digit is not present at all you should return a index that is not possible in a real number, e.g. you can return a negative number in that case, may be `-1`. (In c/c++ indexing starts from 0 so 0 is always a valid index)

so for `digitPos1(int num, int digit)` the ending should look like

``````while(num){
//return pos + 1 in case digit found
//more code
}
return -1;
``````

similarly for `digitPos2(int num, int digit, int *result)` you should initialize `*result` with `-1`

``````int pos=0;
*result = -1;
``````

and the biggest mistake is to set `*result=0` at the end. To understand this you should understand that while dealing with pointers, every change you make to the pointers value is reflected in the callee function also, so if you set `*result=0` then all the previous computation is just wasted and the position is returned blindly as `0`. So you MUST remove this line for your computations to reflect back in the main function.

Secondly, I don't think you have considered the cases of duplicates, e.g.

``````num : 1232323
digit : 3
``````

Here, ideally you should break or return after you have found the match in case of `digitPos2`

``````*result = pos;
return; //return type is void
``````

This changes should give expected behaviour according to me.