473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

struct ptr problem

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[i] = 20 * (i + 10);
}

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

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

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

}

}

Thank you guys!
Nov 14 '05 #1
4 2395
quek <qu****@hotmail.com> wrote:
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[i] = 20 * (i + 10);
} for (i=0; i<20; i++)
{
aLawArray[i] = linear2alaw(inputArray[i]);
}
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
qu****@hotmail.com (quek) writes:
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[i] = 20 * (i + 10);
}

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

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

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

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

return 0;
}


Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
quek wrote:
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[i] = 20 * (i + 10);
}

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

adpcm_coder_u(aLawArray, adpcmArray, 20, state);

for (i = 0; i < 20; i++) {
printf("%d Linear = %d aLaw = %d adpcm %d\n", i,
inputArray[i], aLawArray[i], adpcmArray[i]);
/* 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. */

}
Nov 14 '05 #4
Je***********@physik.fu-berlin.de wrote in message news:<2g************@uni-berlin.de>...
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.

---
OSHIMA
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
3
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
6
by: sathyashrayan | last post by:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct tree { int data; struct tree *left,*right; }; void init(struct tree *node)
7
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
0
by: Peter Demeyer | last post by:
I have this problem: 'pno' is a pointer to a struct of type PRINTER_NOTIFY_OPTIONS. This struct is holding another struct called 'not' (of type PRINTER_NOTIFY_OPTIONS_TYPE) which contains a couple...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.