struct ptr problem

Q

quek

Im having a problem passing a struct ptr.
Maybe someone could help me.

Heres what Visual C gives me as error:
error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct
main::$S1 *' to 'struct adpcm_state *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast


Here is the code

struct adpcm_state {
short valprev; /* Previous output value */
char index; /* Index into stepsize table */
};

extern void adpcm_coder_u(unsigned char[], char[], int, struct
adpcm_state *);
extern void adpcm_decoder_u(char[], unsigned char[], int, struct
adpcm_state *);

void main (void)
{
int i;
int inputArray[20];
unsigned char aLawArray[20];
char adpcmArray[20];
struct *state;

for(i = 0; i<20; i++)
{
inputArray = 20 * (i + 10);
}

for (i=0; i<20; i++)
{
aLawArray = linear2alaw(inputArray);
}

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

for(i=0;i<20;i++){
printf("%d Linear = %d aLaw = %d adpcm
%d",i,inputArray,aLawArray,adpcmArray);

}

}

Thank you guys!
 
J

Jens.Toerring

quek said:
Im having a problem passing a struct ptr.
Maybe someone could help me.
Heres what Visual C gives me as error:
error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct
main::$S1 *' to 'struct adpcm_state *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

You are obviously using a C++ compiler to to compile a C program
(things like "main::$S1" and "reinterpret_cast" are dead giveaways).
Try to figure out how to invoke your compiler in C mode. But there
are some C problems with the code:
Here is the code
struct adpcm_state {
short valprev; /* Previous output value */
char index; /* Index into stepsize table */
};
extern void adpcm_coder_u(unsigned char[], char[], int, struct
adpcm_state *);
extern void adpcm_decoder_u(char[], unsigned char[], int, struct
adpcm_state *);
void main (void)
{
int i;
int inputArray[20];
unsigned char aLawArray[20];
char adpcmArray[20];
struct *state;

Here's the type of the structure missing. Let's assume for the
following you meant

struct adpcm_state *state;
for(i = 0; i<20; i++)
{
inputArray = 20 * (i + 10);
}

for (i=0; i<20; i++)
{
aLawArray = linear2alaw(inputArray);
}


Why not combine that loops into a single one?
adpcm_coder_u(aLawArray, adpcmArray, 20, state);

Now, here things get fishy. Why do you pass an unitialized pointer to
the function ('state' hasn't been assigned a value yet). Even if the
function you call allocates memory for the structure the caller will
never see anything of it (remember, in C arguments are passed by value).
So I guess you will either have to pass a pointer to that structure
pointer to the function or to initialize 'state' to something the
called function can use. Or drop the argument completely if it's not
used at all.
Regards, Jens
 
M

Martin Dickopp

Im having a problem passing a struct ptr.
Maybe someone could help me.

Heres what Visual C gives me as error:
error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct
main::$S1 *' to 'struct adpcm_state *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

There's some C++ terminology in this message. Please make sure you
invoke your compiler as a C compiler (not a C++ compiler) when compiling
C code.
Here is the code

struct adpcm_state {
short valprev; /* Previous output value */
char index; /* Index into stepsize table */
};

extern void adpcm_coder_u(unsigned char[], char[], int, struct
adpcm_state *);
extern void adpcm_decoder_u(char[], unsigned char[], int, struct
adpcm_state *);

void main (void)

Undefined behavior. The `main' function must return `int'.
{
int i;
int inputArray[20];
unsigned char aLawArray[20];
char adpcmArray[20];
struct *state;

This is a syntax error. If you want to define `state' as a pointer to a
struct, you must specify *which* struct you mean. Did you mean:

struct adpcm_state *state;
for(i = 0; i<20; i++)
{
inputArray = 20 * (i + 10);
}

for (i=0; i<20; i++)
{
aLawArray = linear2alaw(inputArray);
}

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

for(i=0;i<20;i++){
printf("%d Linear = %d aLaw = %d adpcm
%d",i,inputArray,aLawArray,adpcmArray);

}


The `main' function must return an `int' value. Insert:

return 0;

Martin
 
M

Martin Ambuhl

quek said:
Im having a problem passing a struct ptr.
Maybe someone could help me.

Heres what Visual C gives me as error:
error C2664: 'adpcm_coder_u' : cannot convert parameter 4 from 'struct
main::$S1 *' to 'struct adpcm_state *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

This diagnostic suggests that you are not using your compiler as a C
compiler, but as a C++ compiler.
Here is the code

Here's a commented and somewhat corrected (up to being compilable) version:

#include <stdio.h> /* mha: the variadic function printf
*must* have a declaration visible.
Including <stdio.h> is not only an
easy way to do this, but it makes
sure that the declaration is right. */

struct adpcm_state
{
short valprev; /* Previous output value */
char index; /* Index into stepsize table */
};

extern void adpcm_coder_u(unsigned char[], char[], int, struct
adpcm_state *);
extern void adpcm_decoder_u(char[], unsigned char[], int, struct
adpcm_state *);

/* mha: main returns an int. This is so fundamental and is repeated
so many times a day here, that your use of 'void' as the return
type is prima facie evidence that you did not follow the newsgroup
before posting (a usenet sin) and that you did not check the FAQ
before posting (another usenet sin). This may seem a small thing
to get worked up over, but you *cannot* make this error if you have
observed the basic etiquette of newsgroups. I've fixed the error. */
int main(void)
{
int i;
int inputArray[20];
unsigned char aLawArray[20];
char adpcmArray[20];
struct adpcm_state *state; /* mha: the 'struct *state;' which was
here does not say which of the
various types of structs state might
point to. */

for (i = 0; i < 20; i++) {
inputArray = 20 * (i + 10);
}

for (i = 0; i < 20; i++) {
/* mha: there is no function 'linear2alaw' declared. */
aLawArray = linear2alaw(inputArray);
}

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

for (i = 0; i < 20; i++) {
printf("%d Linear = %d aLaw = %d adpcm %d\n", i,
inputArray, aLawArray, adpcmArray);
/* mha: I added the '\n' to your specification string. There are
two reasons for this. The first is aesthetic (20 run-together
lines are very hard to read), the second is to ensure that the
last line of output has a final end-of-line character.
Without this, the behavior is not portable. note that your
specification string was broken by your posting software.
This leads to compilation errors that you didn't even have
before. */
}
return 0; /* mha: main returns an int, you should
do so explicitly even if your
compiler (and the C99 standard) let
you get away without doing so. */

}
 
O

OSHIMA

Now, here things get fishy. Why do you pass an unitialized pointer to
the function ('state' hasn't been assigned a value yet). Even if the
function you call allocates memory for the structure the caller will
never see anything of it (remember, in C arguments are passed by value).
So I guess you will either have to pass a pointer to that structure
pointer to the function or to initialize 'state' to something the
called function can use. Or drop the argument completely if it's not
used at all.
Regards, Jens

Yes, that right. And have a question. Who wrote adpcm_coder_u()
function? AD-Converter board's function library?
If so,
struct adpcm_state {...}
and
struct adpcm_state *state;
are defined in the library function or header file already.
You just include it only.
Or not, you have to do as follows:

struct adpcm_state state_tmp, *state=&state_tmp;

or

struct adpcm_state state;
...
adpcm_coder_u(aLawArray, adpcmArray, 20, &state);

Your source code:

struct adpcm_state *state;
...
adpcm_coder_u(aLawArray, adpcmArray, 20, state);

will cause the memory leak.
The point is "Who allocate the variable 'state'?".
Note: 'who' mean the C functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top