Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我有这段代码,其中complexp是指向表示复杂数字的struct的指针。第一个printf命令工作正常,并且complexp的内容已打印。但是,第二个printf无法正常工作,并且打印0两次(不正确)。

这两行之间没有代码。

int main()
    {
        ComplexP  complexp =  (ComplexP) malloc(sizeof(ComplexP));
        complexp = fromCharFunc(s);

        printf("complexp: real: %f, imaginary: %f\n", complexp->real, complexp->imaginary);

         printf("1complexp: real: %f, imaginary: %f\n", complexp->real, complexp->imaginary);

    return 0;
    }
 typedef struct Complex {
   double real;
   double imaginary;
 } Complex;

typedef struct Complex* ComplexP;
ComplexP fromCharFunc(char * s)
{
    if(s == NULL)
    {
        return NULL;
    }


char* sreal;
char* simaginary;
double real;
double imaginary;

char str [DEFAULT_SIZE];

strcpy(str, s);
sreal = strtok(str, REALL_PART_DELIMITER);

simaginary = strtok(NULL, IMAGINARY_PART_DELIMITER);

int len1 = strlen(sreal) + strlen(simaginary);
int len2 = strlen(s) - strlen(IMAGINARY_PART_DELIMITER);

int diff = len1 == len2 ? 0 : 1;


if(diff)
{
    return NULL;
}


if(verifyIsNumber(sreal))
{
    real = atof(sreal);
}
else
{
    return NULL;
}

if(verifyIsNumber(simaginary))
{
    imaginary = atof(simaginary);
}
else
{
    return NULL;
}

Complex complex = {real, imaginary};
ComplexP complexp = &complex;

return complexp;

}
/**
 * @brief determines whether a string represents a number
 * @param char *s poiter to a string
 * #retrun 0 if not a number, 1 if is a number
 */
int verifyIsNumber(char *s)
{
char c;
int i = 0;
while( *(s+i) != '\0')
{
    c = *(s+i);
    if ((c >= MIN_DIGIT && c <= MAX_DIGIT) || c == POINT || c == MINUS)
        i++;
    else
    {
        return 0;
    }
}

return 1;

}

最佳答案

您正在返回一个指向局部变量的指针。该变量将在作用域末尾删除。您应该考虑返回一个Complex变量而不是ComplexP

ComplexP fromCharFunc(char * s)
{

    // ...

    // This variable is deleted after the function ends
    Complex complex = {real, imaginary};

    // After deletion, this pointer points to invalid memory
    ComplexP complexp = &complex;

    return complexp;
}


您的第一个printf调用有效,因为complex中的值仍然碰巧在指针指向的内存位置。这是未定义的行为。使用不同的编译器或不同的系统,可能是两个printf命令都失败,或者都成功。

如果要返回ComplexP,则应使用malloc保留内存。

ComplexP fromCharFunc(char * s)
{

    // ...

    // Create a temporary variable
    Complex complex = {real, imaginary};

    // Reserve memory for your return variable
    ComplexP complexp = malloc(sizeof(Complex));

    // Copy your temporary variable to the reserved memory location
    *complexp = complex;

    return complexp;
}

09-12 18:20