JKzzz
JKzzz

Reputation: 21

mpfr_t function declaration error

I'm trying to create a function that returns type mpfr_t but I get an error in its declaration. The header file declaration looks like (mpfr.h is local):

#include "include/mpfr.h"
mpfr_t calcWinProb(int x);

But when I try to compile I get the following error:

error: âcalcWinProbâ declared as function returning an array

Any ideas?

Upvotes: 2

Views: 319

Answers (2)

Javier Garcia
Javier Garcia

Reputation: 578

I'm not an expert, but what I've done instead is to create a function that takes a pointer to an mpfr_t as an argument, so you can return the values there. For example:

#include <stdio.h>
#include <mpfr.h>

void return_one(mpfr_t *num) {
    mpfr_set_ui(*num,1,MPFR_RNDN);
}

int main() {
    mpfr_t num;
    mpfr_init2(num,512);
    return_one(&num);
    mpfr_printf("%.5Rf\n",num);
    mpfr_clear(num);
    return 0;
}

Upvotes: 0

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37924

C language does not allow array as return type, which mpfr_t definitely is:

typedef __mpfr_struct mpfr_t[1];

Refering to N1570 (C11 draft) 6.7.6.3/1 Function declarators (including prototypes):

A function declarator shall not specify a return type that is a function type or an array type.

This is violation of constraint, thus your compiler is obligated for diagnostic (e.g. error on compilation).

Whay you may do about it is to replace mpfr_t with mpfr_ptr (pointer to struct) type or redesign your declaration, so mpfr_t is one of parameters (may be the first), rather then that return type, which might be void in such case. The latter solution seems to be more coherent with MPFR API.

Upvotes: 3

Related Questions