Problem with pointer, array and function parameter

Discussion in 'C Programming' started by fl, Jun 12, 2012.

  1. fl

    fl Guest

    Hi,

    I have c function {bitrev_cplx} with having function parameters float
    array. In the function bitrev_cplx internal, some array members will
    be swap value. I find that when array index i1=1, xi1 gets the right
    value, but when j1=16, xj1 gets the wrong value (very large 1.32xx e+8
    for example). I suspect the pointer, function parameter usage are
    wrong, but I cannot find what is wrong. Could you help check my code?

    Thanks a lot.


    .....................
    void bitrev_cplx (float *, int *, int );
    int main()
    {
    float x[32*2];
    float fw[16*2], fwc[16], fws[16];

    for (i=0; i<32/2; i++){
    fwc = cos(2*PI*i/32);
    fws = sin(2*PI*i/32);
    }

    bitrev_cplx (fwc, temp, nx);
    }


    void bitrev_cplx (float *x, int *index, int nx)
    {
    int i;
    int i0, i1, i2, i3;
    int j0, j1, j2, j3;
    float xi0, xi1, xi3;
    float xj0, xj1, xj3;

    if (t){
    x[i0] = xj0;
    x[j0] = xi0;
    }
    i1 = i0 + 1;
    j1 = j0 + halfn;
    xi1 = x[i1];
    xj1 = x[j1];
    }
     
    fl, Jun 12, 2012
    #1
    1. Advertising

  2. fl

    Ike Naar Guest

    On 2012-06-12, fl <> wrote:
    > I have c function {bitrev_cplx} with having function parameters float
    > array. In the function bitrev_cplx internal, some array members will
    > be swap value. I find that when array index i1=1, xi1 gets the right
    > value, but when j1=16, xj1 gets the wrong value (very large 1.32xx e+8
    > for example). I suspect the pointer, function parameter usage are
    > wrong, but I cannot find what is wrong. Could you help check my code?
    >
    > void bitrev_cplx (float *, int *, int );
    > int main()
    > {
    > float x[32*2];
    > float fw[16*2], fwc[16], fws[16];
    >
    > for (i=0; i<32/2; i++){
    > fwc = cos(2*PI*i/32);
    > fws = sin(2*PI*i/32);
    > }
    >
    > bitrev_cplx (fwc, temp, nx);
    > }
    >
    >
    > void bitrev_cplx (float *x, int *index, int nx)
    > {
    > int i;
    > int i0, i1, i2, i3;
    > int j0, j1, j2, j3;
    > float xi0, xi1, xi3;
    > float xj0, xj1, xj3;
    >
    > if (t){
    > x[i0] = xj0;
    > x[j0] = xi0;
    > }
    > i1 = i0 + 1;
    > j1 = j0 + halfn;
    > xi1 = x[i1];
    > xj1 = x[j1];
    > }


    Sorry, but this code is so incomplete that it is almost
    impossible to say anything about it. Look at the
    bitrev_cplx function, for instance. What is t ?
    Why are i0 and j0 used uninitialized? What is halfn?
     
    Ike Naar, Jun 12, 2012
    #2
    1. Advertising

  3. fl

    James Kuyper Guest

    On 06/11/2012 11:55 PM, fl wrote:
    > Hi,
    >
    > I have c function {bitrev_cplx} with having function parameters float
    > array. In the function bitrev_cplx internal, some array members will
    > be swap value. I find that when array index i1=1, xi1 gets the right
    > value, but when j1=16, xj1 gets the wrong value (very large 1.32xx e+8
    > for example). I suspect the pointer, function parameter usage are
    > wrong, but I cannot find what is wrong. Could you help check my code?
    >
    > Thanks a lot.
    >
    >
    > ....................
    > void bitrev_cplx (float *, int *, int );
    > int main()
    > {
    > float x[32*2];
    > float fw[16*2], fwc[16], fws[16];


    x and fw are not used anywhere. Why are they here? If this was trimmed
    down from a larger example, that's a good idea, but you should trim
    everything that's not actually used, to avoid confusion.

    > for (i=0; i<32/2; i++){


    No variable named 'i' has been declared in the code you've given us.
    Please provide a complete, compilable program demonstrating your
    program. It's nearly useless looking at code that isn't the actual code
    that compiled and failed.

    > fwc = cos(2*PI*i/32);
    > fws = sin(2*PI*i/32);
    > }
    >
    > bitrev_cplx (fwc, temp, nx);


    Neither temp nor nx are declared anywhere.

    > }
    >
    >
    > void bitrev_cplx (float *x, int *index, int nx)


    Neither index nor nx are used anywhere in this function, so I suppose
    the cancels out the fact that neither temp nor nx are declared anywhere
    that's visible in main(). If they're relevant, show us how they're used.
    If not, remove them.

    > {
    > int i;
    > int i0, i1, i2, i3;
    > int j0, j1, j2, j3;


    Variables named that way are almost always a bad idea. If i0 through i3
    are all closely related to each other, you're probably better off
    declaring a single array named 'i', and using i[1] instead of i1. It may
    seem wasteful typing the extra brackets, but if the values are
    sufficiently closely related, you'll often find contexts in which
    accessing them as elements of an array simplifies your code.

    On the other hand, if they're not sufficiently closely related to
    justify making them array, they should be given distinct names, not
    closely related names as you have done.

    > float xi0, xi1, xi3;
    > float xj0, xj1, xj3;


    i2, j2, i3, j3, xi3, and xj3 are not used anywhere in this function.
    Not knowning the purpose of your code, I can't be sure, but are xi2 and
    xj2 missing?

    > if (t){


    t is not declared anywhere.

    > x[i0] = xj0;


    Neither i0 nor xj0 have been initialized, at best they have unspecified
    values; at worst, they could be trap values, in which case the behavior
    is undefined. If i0 has a value greater than 15, this will result in
    trying to access a part of fwc that doesn't exist, rendering the
    behavior of your code undefined.

    > x[j0] = xi0;


    Neither j0 nor xi0 have been initialized.

    > }
    > i1 = i0 + 1;
    > j1 = j0 + halfn;


    halfn is not declared anywhere. j0 is still uninitialized.

    > xi1 = x[i1];
    > xj1 = x[j1];


    The values of i1 and j1 are, at best, unspecified at this point. If
    either one has a value greater than 15, this code results in an attempt
    to access a part of fwc that doesn't exist, rendering the behavior of
    your code undefined.

    I reiterate: give us a simple complete compilable example program that
    demonstrates your problem, and it will be a lot easier for us to help
    you. "simple" means, among other things, removing every feature not
    needed to demonstrate the problem. "complete" means that it must define
    or declare everything needed to demonstrate the problem. "compilable" is
    something that you should confirm by compiling it yourself, with warning
    levels on the compiler set as high as possible, before you post it here.
    If it produces any warning messages, please give us the complete text of
    those messages. Telling us precisely which compiler you're using, and
    what options you selected for the compiler, would also help.

    Finally, make sure that you present us with the complete actual code
    that actually compiled into a program that demonstrates your problem.
    Don't try to copy it by hand, any errors you make while copying it (and
    you'll almost certainly make some) will only confuse the issue. Cut and
    paste the actual code directly from your source code files.
    --
    James Kuyper
     
    James Kuyper, Jun 12, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lighter
    Replies:
    6
    Views:
    435
    Lighter
    Jun 21, 2007
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    682
    Martin Ambuhl
    Jan 28, 2008
  3. Replies:
    25
    Views:
    1,715
  4. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    471
    James Kuyper
    Sep 23, 2011
  5. AzamSharp
    Replies:
    2
    Views:
    192
Loading...

Share This Page