struct ptr problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • quek

    #1

    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_cas t, 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(u nsigned 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(inp utArray[i]);
    }

    adpcm_coder_u(a LawArray, adpcmArray, 20, state);

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

    }

    }

    Thank you guys!
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: struct ptr problem

    quek <quekqc@hotmail .com> wrote:[color=blue]
    > Im having a problem passing a struct ptr.
    > Maybe someone could help me.[/color]
    [color=blue]
    > 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_cas t, C-style cast or function-style cast[/color]

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

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

    struct adpcm_state *state;
    [color=blue]
    > for(i = 0; i<20; i++)
    > {
    > inputArray[i] = 20 * (i + 10);
    > }[/color]
    [color=blue]
    > for (i=0; i<20; i++)
    > {
    > aLawArray[i] = linear2alaw(inp utArray[i]);
    > }[/color]

    Why not combine that loops into a single one?
    [color=blue]
    > adpcm_coder_u(a LawArray, adpcmArray, 20, state);[/color]

    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 ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Martin Dickopp

      #3
      Re: struct ptr problem

      quekqc@hotmail. com (quek) writes:
      [color=blue]
      > 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_cas t, C-style cast or function-style cast[/color]

      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.
      [color=blue]
      > Here is the code
      >
      > struct adpcm_state {
      > short valprev; /* Previous output value */
      > char index; /* Index into stepsize table */
      > };
      >
      > extern void adpcm_coder_u(u nsigned char[], char[], int, struct
      > adpcm_state *);
      > extern void adpcm_decoder_u (char[], unsigned char[], int, struct
      > adpcm_state *);
      >
      > void main (void)[/color]

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

      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;
      [color=blue]
      >
      > for(i = 0; i<20; i++)
      > {
      > inputArray[i] = 20 * (i + 10);
      > }
      >
      > for (i=0; i<20; i++)
      > {
      > aLawArray[i] = linear2alaw(inp utArray[i]);
      > }
      >
      > adpcm_coder_u(a LawArray, adpcmArray, 20, state);
      >
      > for(i=0;i<20;i+ +){
      > printf("%d Linear = %d aLaw = %d adpcm
      > %d",i,inputArra y[i],aLawArray[i],adpcmArray[i]);
      >
      > }[/color]

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

      return 0;
      [color=blue]
      > }[/color]

      Martin


      --
      ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
      / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
      \ `-' `-'(. .)`-'
      `-. Debian, a variant of the GNU operating system. \_/

      Comment

      • Martin Ambuhl

        #4
        Re: struct ptr problem

        quek wrote:
        [color=blue]
        > 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_cas t, C-style cast or function-style cast[/color]

        This diagnostic suggests that you are not using your compiler as a C
        compiler, but as a C++ compiler.[color=blue]
        >
        >
        > Here is the code[/color]

        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(u nsigned 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(inp utArray[i]);
        }

        adpcm_coder_u(a LawArray, 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. */

        }


        Comment

        • OSHIMA

          #5
          Re: struct ptr problem

          Jens.Toerring@p hysik.fu-berlin.de wrote in message news:<2g6thtF49 mmtU1@uni-berlin.de>...
          [color=blue]
          > 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[/color]

          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_t mp;

          or

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

          Your source code:

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

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

          ---
          OSHIMA

          Comment

          Working...