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

Parents
  • 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.
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.
Children
No data