This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Function returns structure problem in ARM926

  • Note: This was originally posted on 5th July 2009 at http://forums.arm.com

    Thanks for your kind and reply..

    Still i couldnt solve my problem,i will explain you logic in my
    application

    struct A
    {
      int k[10]; 
      int l[10];
      int m[10];
    };
    static struct B
    {
      int a[10]; 
      int b[10];
      int c[10];
    };                
    struct B should be static structure because

    I am calling f2() function many times in my suppilcation,so next time when i call the function f2() it should have previous values of f2()so it should be static


    ############  main.c #################
    main()
    {
    f4()
    {
      f3();  frist time
      f3();  second time the values of f2() will use here
    }
    ##############  test.c  #######################
    f3()
    {
      f2();
    }
    #########################################

    In f2() we should modify static strure B vlues,by passing refernce of B to f2() function.In side the function f2() using another structure (structure A),with A object we should modify values and should return updated static data values.

    I should not use static structre vabiles in f2() because inside f2() function i have tight for loops and doing math operation on values,so copy static values to local variable and modify those varibles with math operations and return those varibles to satic structure.

    I should not do math operations on static variables,becuase it will take more core cycles than local varibles, thats why i am copying to another local structe and do math operations.


    Please give me the solution how can i do this..

    waiting for kind reply
  • Note: This was originally posted on 5th July 2009 at http://forums.arm.com

    reddyr,

    Your code really isn't C compliant, I'm sure even VC isn't compiling this without warnings.
    In summary your initial problem is that f3() is attempting to return a struct when in fact you've defined it to return an int (by not defining the return type).
    Compiling this code with warnings enable highlights a plethora of issues;

    1. "f2(struct A *a);" ends up prototyping f2 to take a pointer to a struct which hasn't yet been defined(you might want to move this prototype to after the declaration of struct A), however, the return type isn't specified and will default to "int", which doesn't appear to be what you're actually returning from f2(), consider chainging this to "struct A *f2(struct A *q);".

    2. The "static" in "static struct B..." is completely redundant as no objects of type "struct B" are actually being declared to assume the "static" specifier.

    3. "void main()" is not a compliant definition for a hosted environment, and should probably be "int main(void)"

    4. incomplete definition of "f3() {..." should really be "int f3(void) {...", however, you haven't specified a "return" within f3; perhaps f3 should be prototyped and defined as "void f3(void) {.."

    5. There is not visible definition of "memcpy()", you should include "string.h".

    6. The definition of f2 "f2(struct A *q) {.." also implicitly defines f2 as returning "int", which doesn't match the type of "q" which is actually returned, resulting in an implicit pointer to integer cast.

    7. On top of all of this, you are intermixing the use of structs A and B, which aren't guaranteed to have the same representation.

    Fixing eveything except issue 7, results in :

    #include <stdio.h>
    #include <string.h>

    struct A
    {
      int k[10];
      int l[10];
      int m[10];
    };

    struct B
    {
      int a[10];
      int b[10];
      int c[10];
    };

    struct A *f2(struct A *q);
    void f3(void);

    int main(void)
    {
      f3();
      return 0;
    }

    void f3(void)
    {
      int d[9]={1,1,1,1,1,1,1,1,1};
      int i;
      struct B b;
      memcpy(b.a,d,sizeof d);
      memcpy(b.b,d,sizeof d);
      memcpy(b.c,d,sizeof d);

      printf("Values before function f2()\n\n\n");
      for(i=0;i<9;i++)
    {

       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2((struct A*)&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2((struct A*)&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

    }

    struct A *f2(struct A *q)
    {
      int i;
      for(i=0;i<9;i++)
    {
       q->k[i]=q->k[i]+1;
       q->l[i]=q->l[i]+1;
       q->m[i]=q->m[i]+1;
    }
      return q;
    }


    hth
    s.
  • Note: This was originally posted on 6th July 2009 at http://forums.arm.com

    reddyr,

    There is no point in making "struct B b;" static i.e. "static struct B b;", as it gets immediately overwritten by the memcpy()s in f3(). Also, given that f2() takes a pointer, I don't see any advantage to the thing it points at being "local" or not. Assuming you were going to remove the memcpy()s from f3() so the value of "b" really was persistent, then better code might be:

    #include <stdio.h>
    #include <string.h>

    struct A
    {
      int a[10];
      int b[10];
      int c[10];
    };

    void f2(struct A *q);
    void f3(void);

    int main(void)
    {
      f3();
      return 0;
    }

    void f3(void)
    {
      int d[9]={1,1,1,1,1,1,1,1,1};
      int i;
      static struct A b;
      memcpy(b.a,d,sizeof d);
      memcpy(b.b,d,sizeof d);
      memcpy(b.c,d,sizeof d);

      printf("Values before function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

      f2(&b);
      printf("Values 1 after function f2()\n\n\n");
      for(i=0;i<9;i++)
    {
       printf("Values In a[i] %d,Values In b[i] %d,Values In c[i] %d\n\n",
       b.a[i],b.b[i],b.c[i]);
    }

    }

    void f2(struct A *q)
    {
      int i;
      for(i=0;i<9;i++)
    {
       q->a[i]=q->a[i]+1;
       q->b[i]=q->b[i]+1;
       q->c[i]=q->c[i]+1;
    }
    }


    hth
    s.
  • Note: This was originally posted on 6th July 2009 at http://forums.arm.com

    reddy,

    The code I pasted compiles without warning or error on both gcc and armcc.

    s.