C 在返回时是否有任何问题;“字符指针”;从功能

C 在返回时是否有任何问题;“字符指针”;从功能,c,memory-management,memory-leaks,scope,c-strings,C,Memory Management,Memory Leaks,Scope,C Strings,下面的代码将返回一个字符串,该字符串仅包含用户输入字符串中的数字 此外,返回的字符串应将数字分组为三位数字,并在它们之间加一个“-” 一切运行正常,代码编译没有任何错误,但函数没有返回char* #include<stdio.h> #include<string.h> #include<stdlib.h> char* phoneNo(char*); void main(){ char str[100]; char *strpass =

下面的代码将返回一个字符串,该字符串仅包含用户输入字符串中的数字

此外,返回的字符串应将数字分组为三位数字,并在它们之间加一个“-”

一切运行正常,代码编译没有任何错误,但函数没有返回
char*

#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;
}
#包括
#包括
#包括
字符*电话号码(字符*);
void main(){
char-str[100];
char*strpass=str;
printf(“输入字符串:”);
fgets(str,100,stdin);
printf(“输入的字符串:%s\n”,str);
char*result=phoneNo(strpass);
printf(“返回的字符*为:%s\n”,结果);
}
char*phoneNo(char*strpass){
char-str[100];
strcpy(str,strpass);
printf(“函数:%s中的字符[]”,str);
答案[100];
字符*结果;
结果=(char*)malloc(100*sizeof(char));
结果=答案;
//printf(“Char*指向Char[]:%s\n”,结果);
int i=0;
int j=0;
int k=3;
while(str[i]!='\0'){
如果(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”)
{
如果(j==0){
答案[j]=str[i];
答案[j+1]='\0';
j++;
i++;
继续;
}
如果(j==k){
答案[j]='-';
答案[j+1]='\0';
j++;
k+=4;
}否则{
答案[j]=str[i];
答案[j+1]='\0';
j++;
i++;
}
}
其他的
i++;
}
printf(“要返回的字符数:%s\n”,结果);
返回(char*)结果;
}
此代码段

char answer[100];
char * result;
result = ( char* ) malloc(100*sizeof(char));
result=answer;
由于此语句导致分配内存的地址丢失,因此发生内存泄漏

result=answer;
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')
现在,指针
result
指向本地数组
answer
,并从函数返回,该函数导致未定义的行为,因为退出函数后数组将不活动

#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;
}
使用动态分配的数组进行处理,而不是本地数组
answer

注意这个,而不是这个复合if语句

result=answer;
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 ( isdigit( ( unsigned char )str[i] ) )
函数的声明如下

char* phoneNo(const char *strpass);
这就是它的参数必须具有限定符
const

我将按照演示程序中显示的方式编写函数

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

首先为
result
分配内存,然后在下一行
result=answer
它立即指向其他地方,在指向局部变量的同时造成内存泄漏。这就是错误。

“函数没有返回字符*。”您根据什么证据得出这个结论?您认为
result=answer
是否在
phoneNo
中?如果您澄清“使用动态分配的数组而不是本地数组应答进行处理”,或者发布更正后的code@vikasdamdhare只需删除str的声明并回答。