Re: Using a structure as return value for functions

Started by pkparchure, May 31, 2025, 09:32:07 AM

Previous topic - Next topic

pkparchure

Dear Forum
I am facing a peculiar problem with Pelles C IDE. I have tried using a structure as return value for function. For example my function for computing the Statistcs is
// Function to calculate all statistics
Statistics calculate_statistics(DataStruct gsData, int nRows, int vCol) where Statistics is a structure:
typedef struct {
    int count;
    double min;
    double max;
    double mean;
    double median;
    double mode;
    double std_dev;
    double coeff_variation;
    double skewness;
    double kurtosis;
} Statistics;
The program works very well but the problem is that this function is not shown in side panel.
In fact any function with return variable not given as void, int, float, double, etc, is not shown in side panel.

I have put the screenshot of IDE. I am using Version 13 now. Is it a bug in IDE or I am missing something in IDE


TimoVJL

Not sure what you mean.
This example shows {} calculate_statistics in project panel.
typedef struct {
    int count;
    double min;
    double max;
    double mean;
    double median;
    double mode;
    double std_dev;
    double coeff_variation;
    double skewness;
    double kurtosis;
} Statistics;

Statistics calculate_statistics(void* gsData, int nRows, int vCol)
{
}
May the source be with you

John Z

Hi pkparchure,

It seems to work ok for me as well.  As TimoVJL shows.

One thing in your procedure definition that I noticed, I think
   Statistics calculate_statistics(DataStruct gsData, int nRows, int vCol)
should be
   Statistics calculate_statistics(Statistics gsData, int nRows, int vCol)
but of course you didn't show what DataStruct was so you could be correct ....

in any case here is my screen shot test result:

John Z

pkparchure

No it does not show up in my case. Kindly look at the screenshot put by me in my post. There are two functions in the file gsStat.c one is int PrintStat(Statistics stats), which uses the statistics obtained from Statistics calculate_statistics(DataStruct gsData, int nRows, int vCol).

As seen in the screenshot put by me, only {}PrintStat is visible but not the {}calculate_statistics.

As suggested TimoVJL JohnZ by I tried to put the code for calculate_statistics in my main file gsMain.c as

Statistics calculate_stats1(void* gsData, int nRows, int vCol)
{
}

It does not show in the panel

However, if I write it in classical C way:

Statistics Stats;

void calculate_stats2(void* gsData, int nRows, int vCol, Stats)
{
}
it shows in the panel.

I have not tested these two functions by calling them but the program compiles.

I have put a second screenshot showing the two functions.


John Z

Hi pkparchure,

No, I was not suggesting putting in main as a solution.  It just happened that was the open
page.  Below you see it working in a support file.

You mentioned it compiles but your screen shot shows an error 1 alert, have you investigated where that was coming from?  Also you should be seeing a warning
warning #2096: Missing return value. on the declaration line number - is there one?

The only way I have been able to get it to not show is if there is an error in the procedure declaration.

John Z

MrBcx

Hi pkparchure,

My recommendation:

If possible, upload a zip containing your project.  If that is not possible,
upload a zip containing mock project files that expose the v13 IDE bug.

That should make it easier for others to help you or to confirm the bug.
Bcx Basic to C/C++ Translator
https://www.bcxbasiccoders.com

pkparchure

As suggested I have extracted main code pertaining to statistical analysis and a complete project is attached as a zip file. Exe file is also there if it does not get dropped. However, the program can be easily compiled within the project.

I have also put a ReadMe.txt file within the zip file giving some explanations about the program and myself.


TimoVJL, John Z and MrBcx - Thanks a lot for your responses. I wish to tell MrBcx that I am a great fan of BCX - Basic to C translator.

TimoVJL

pobr.exe /B /W /Ftest.db3 gsStat.c
pobr.exe /L /Ftest.db3
proves, that function is missing from database.
May the source be with you

John Z

Thanks pkparchure,

Quote from: pkparchure on Yesterday at 08:03:58 AMAs suggested I have extracted main code pertaining to statistical analysis and a complete project is attached as a zip file.

I can't explain it  :( !
Tried various modifications and could not get it to show, just as you have experienced.
Also can't explain why when I drop just that same structure and proc into my existing program it does show.  Stumped for now.

John Z

Pelle

Parsing the C language is hard. There is some agreement it's one of the harder ones.
( Without more context: does "X * Y" means variable X times variable Y, or is Y a pointer to typedef X? And so on. )

rypedef names are one complication. There are "forks" in the syntax where the parser must know if a name is a type or a variable, to correctly parse the code that follows.

The Pelles C tool that collects information about types are variables is POBR.DLL (with a command-line interface in POBR.EXE). Since POBR.DLL is also used by the IDE, it needs to "work" in interactive mode and on a project that is probably unfinished. Because of this it's generally no time (nor point) to scan every #include file (of every #include file). There may be time to search the symbol database for any recorded typedef names, but doing this repeatedly on an older/slower computer may not br a huge success either.

All that said, what you have is a typedef name in one file (gs.h) used in another file (gsStat.c) and the problem is connecting the two pieces. The current version of POBR.DLL can't hande this well. For now there are only a few options:
1) add a tag to the "Statistics" struct so you can replace the return type "Statistics" with struct <tagname>,
2) make use of one of the heuristics in POBR.DLL to identify a typedef name: <lowercase>_t (generic) or <UPPERCASE> (windows mode), and rename your structs accordingly, or
3) wait for the future, where a new version of POBR.DLL *may* appear that *may* fix this (or not - I can't really see into the future).
/Pelle

TimoVJL

So, after addingtypedef Statistics Statistics;to gsStat.c, project panel shows {} calculate_statistics
May the source be with you

John Z

Good explanation Pelle.

Also explains why it always worked for me - everything in the same file for my tests ...

John Z