vikas damdhare
vikas damdhare

Reputation: 13

Is there any problem while returning the "char pointer" from function

The code below is expected to return a string containing only numbers from an user entered string.

Also the returned string should group the numbers in three digits and put a '-' between them.

Everything runs fine, code compiles without any error, but the char* is not being returned from function.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char* phoneNo(char*); 

void main(){

    char str[100];
    char *strpass = str;

    printf("Enter the string: ");
    fgets(str,100,stdin);

    printf("Entered stringis: %s\n",str);

    char *result = phoneNo(strpass);
    printf("Returned char* is: %s\n",result);
}

char* phoneNo(char *strpass){

    char str[100];
    strcpy(str,strpass);
    printf("Char[] in Function: %s",str);

    char answer[100];
    char * result;
    result = ( char* ) malloc(100*sizeof(char));
    result=answer;
    //printf("Char* pointed to Char[]: %s\n",result);

    int i=0;
    int j=0;
    int k=3;

    while(str[i]!='\0'){

        if(str[i]=='1'||str[i]=='2'||str[i]=='3'||str[i]=='4'||str[i]=='5'||str[i]=='6'||str[i]=='7'||str[i]=='8'||str[i]=='9'||str[i]=='0')
        {


            if(j==0){

                answer[j]=str[i];
                answer[j+1]='\0';
                j++;
                i++;
                continue;
            }

            if(j==k){
                answer[j]='-';
                answer[j+1]='\0';
                j++;
                k+=4;
            }else{
                answer[j]=str[i];
                answer[j+1]='\0';
                j++;
                i++;
            }
        }
        else
            i++;
    }
    printf("Char* to be returned: %s\n",result);

    return (char *)result;
}

Upvotes: 1

Views: 220

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

This code snippet

char answer[100];
char * result;
result = ( char* ) malloc(100*sizeof(char));
result=answer;

has a memory leak because the address of the allocated memory is lost due to this statement

result=answer;

Now the pointer result points to the local array answer and returned from the function that results in undefined behavior because the array will not be alive after exiting the function.

Use the allocated dynamically array for processing instead of the local array answer.

Pay attention to that instead of this compound if statement

if(str[i]=='1'||str[i]=='2'||str[i]=='3'||str[i]=='4'||str[i]=='5'||str[i]=='6'||str[i]=='7'||str[i]=='8'||str[i]=='9'||str[i]=='0')

it is much better to write

if ( isdigit( ( unsigned char )str[i] ) )

And the function shall be declared like

char* phoneNo(const char *strpass);

that is its parameter must have the qualifier const.

I would write the function the following way as it is shown in the demonstrative program.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char * phoneNo( const char *s )
{
    const size_t GROUP_SIZE = 3;

    size_t digits_count = 0;

    for ( const char *p = s; *p; ++p )
    {
        if ( isdigit( ( unsigned char )*p ) ) ++digits_count;
    }

    char *result = malloc( digits_count + digits_count / GROUP_SIZE + sizeof( ( char )'\0' ) );

    size_t i = 0;

    for ( size_t k = 0; *s; ++s )
    {
        if ( isdigit( ( unsigned char )*s ) )
        {
            if ( k == GROUP_SIZE )
            {
                if ( i != 0 )
                {
                    result[i++] = '-';
                }
                k = 0;
            }

            result[i++] = *s;
            ++k;
        }
    }

    result[i] = '\0';

    return result;
}

int main(void) 
{
    const char *s = "123456789";

    char *result = phoneNo( s );

    puts( result );

    free( result );

    s = "12\t34567\t89";

    result = phoneNo( s );

    puts( result );

    free( result );

    return 0;
}

The program output is

123-456-789
123-456-789

Upvotes: 1

Lundin
Lundin

Reputation: 214475

First you allocate memory for result, then in the next line result=answer; you immediately have it point elsewhere, creating a memory leak while instead pointing at a local variable. This is the bug.

Upvotes: 0

Related Questions