I am looking to create histograms in C, each of which is identified by a symbol and each bar is defined by a x-value and y-value. For this I have the following declarations:
#define RESOLUTION 200
typedef struct sCohort {
float x; // location of cohort
int y; // count
} chrt;
typedef struct sHistogram {
char *symbol;
struct sCohort cohort[RESOLUTION]; // there can never be more than RES cohorts
} histogram;
Without the variable name chrt
a warning appears in gcc-9.4: **warning**: useless storage class specifier in empty declaration
This tells me there is a more efficient way to declare my histogram. Can someone indicate a better approach I could have taken?
For what it is worth, a histogram in the current setup would look something like the following:
A -0.030009 61
A -0.025005 99
A -0.020003 189
A -0.015001 312
A -0.010000 679
A -0.005000 1329
A 0.000000 1999
A 0.005000 1500
A 0.010000 833
A 0.015001 387
A 0.020003 165
A 0.025005 88
A 0.030009 56
Hi, c or c++ ? As (am sure you know this) c doesn't have classes
What is the full compiler command line used?
https://stackoverflow.com/questions/2743949/useless-class-storage-specifier-in-empty-declaration#2743984
Am not at a computer atm so limited as to what help I provide right now.
gcc histogram.c -g -Wall -o histogram
And yes, the language is C.
The canonical way to do this is to define the struct, and then declare the type separately.
struct sA { int mA; };
typedef struct sA tA;
You can condense that it two ways:
(a) Omit the declaration of the struct name (so all references are via the typedef).
typedef struct { int mB; } tB;
(b) Declare both the struct name and the typedef name together.
typedef struct sC ( int mC; } tC;
What you cannot do is specify you are declaring a typedef, and then not supply a name for it, because without a name you cannot instantiate it.
typedef struct sD ( int mD; } /* tD needed here */;
In your code, you never reference chrt
, so you do not need to typedef that struct at all.
1 Like
as @Paul_Pedant mentions, if you are not using the typedef 'chrt' / 'histogram' you can remove them and just use 'struct sCohort and struct sHistogram'
here's a simple example using both
cat histogram.c
#include <stdio.h>
#define RESOLUTION 200
typedef struct sCohort {
float x;
int y;
} chrt; // chrt is effectively a synonym for 'struct sCohort'
typedef struct sHistogram {
char *symbol;
chrt cohort[RESOLUTION]; // an array of chrt's
struct sCohort anotherCohort[RESOLUTION]; // an array of 'struct sCohort' not using the synonym
} histogram; // histogram is a synonym for struct sHistogram
int main( int argc, char **argv )
{
histogram h;
h.symbol = "a random string";
h.cohort[0].x = 3.143;
h.cohort[0].y = 143;
printf(" h.symbol == %s\n" , h.symbol );
printf(" h.cohort[0].x == %f\n" ,h.cohort[0].x );
printf(" h.cohort[0].y == %d\n" ,h.cohort[0].y );
h.anotherCohort[0].x = 5432.1;
h.anotherCohort[0].y = 666;
printf(" h.anotherCohort[0].x == %f\n" ,h.anotherCohort[0].x );
printf(" h.anotherCohort[0].y == %d\n" ,h.anotherCohort[0].y );
}
gcc -std=c99 -Wall -o hst histogram.c
./hst
h.symbol == a random string
h.cohort[0].x == 3.143000
h.cohort[0].y == 143
h.anotherCohort[0].x == 5432.100098
h.anotherCohort[0].y == 666
3 Likes
Excellent stuff! That really helps.
1 Like