c - Cannot pass a variable of my own (non-standard) type to a function -
i'm writing program must normalize audio *.wav
file. there task "to display header's data": chunkid
, chunksize
, on.
i want make function named display_hdr
(in order have less code in main.c file, become esier read code). have pass function header's variable (variable of type header) argument, writes
functions.h|1|error: unknown type name 'header'|
main.c:
typedef struct fmt { char subchunk1id[4]; int subchunk1size; short int audioformat; short int numchannels; int samplerate; int byterate; short int blockalign; short int bitspersample; } fmt; typedef struct data { char subchunk2id[4]; int subchunk2size; int data[]; } data; typedef struct header { char chunkid[4]; int chunksize; char format[4]; fmt s1; data s2; } header;
header's variable declared in way:
header hdr;
and now, when try pass hdr
function prints error
:
functions.h|1|error: unknown type name 'header'|
functions.h
void display_hdr( header hdr ) { printf("\n*********************************\n"); printf("wave file's metadata:\n\n"); printf("%4.4s\n", hdr.chunkid ); printf("%d\n", hdr.chunksize ); printf("%4.4s\n", hdr.format ); printf("%4.4s\n", hdr.s1.subchunk1id ); printf("%d\n", hdr.s1.subchunk1size ); printf("%d\n", hdr.s1.audioformat ); printf("%d\n", hdr.s1.numchannels ); printf("%d\n", hdr.s1.samplerate ); printf("%d\n", hdr.s1.byterate ); printf("%d\n", hdr.s1.blockalign ); printf("%d\n", hdr.s1.bitspersample ); printf("%4.4s\n", hdr.s2.subchunk2id ); printf("%d\n", hdr.s2.subchunk2size ); /// samples' size printf("\n*********************************\n"); return; }
so, how pass variable of own (non-standard) type function argument?
"the scope of global variables can restricted placing declaration. visible declaration until end of current source file."
there no problem on subject ( no warning or error ). moved definition of struct (type) main.c function.c (where variable header
of type hdr
declared).
function.h
typedef struct fmt { char subchunk1id[4]; int subchunk1size; short int audioformat; short int numchannels; int samplerate; int byterate; short int blockalign; short int bitspersample; } fmt; typedef struct data { char subchunk2id[4]; int subchunk2size; int data[]; } data; typedef struct header { char chunkid[4]; int chunksize; char format[4]; fmt s1; data s2; } header; /* `header` type created. */ header hdr; /* after `header`type created. */ void display_hdr( header hdr ) { printf("\n*********************************\n"); printf("wave file's metadata:\n\n"); printf("%4.4s\n", hdr.chunkid ); printf("%d\n", hdr.chunksize ); printf("%4.4s\n", hdr.format ); printf("%4.4s\n", hdr.s1.subchunk1id ); printf("%d\n", hdr.s1.subchunk1size ); printf("%d\n", hdr.s1.audioformat ); printf("%d\n", hdr.s1.numchannels ); printf("%d\n", hdr.s1.samplerate ); printf("%d\n", hdr.s1.byterate ); printf("%d\n", hdr.s1.blockalign ); printf("%d\n", hdr.s1.bitspersample ); printf("%4.4s\n", hdr.s2.subchunk2id ); printf("%d\n", hdr.s2.subchunk2size ); /// samples' size printf("\n*********************************\n"); return; }
ps
of course pass pointer function, i'd pass variable. there no need thought better (economy of memory).
the problem because of misunderstanding (poor/bad understanding) of 'scope' properly.
pss
maybe, andrew w's right in way, doesn't solve current problem. quite type of problem. worth reading too!
Comments
Post a Comment