EmbeddedRelated.com
Forums

Arrays permitted in CodeWarrior for HC12?

Started by Andres Rosado November 23, 2002
Jean-Pierre

Thank you for taking the time to provide such an excellent explanation.

Message: 22
Date: Tue, 26 Nov 2002 12:42:55 +0100
From: Jean-Pierre Lavandier <>
Subject: RE: Re: Arrays permitted in CodeWarrior for HC12?
Bob Lewis

Mgr. Process Computer Systems

Tilbury Cement Ltd (604) 946-0411 work
PO Box 950 (604) 946-2214 fax
7777 Ross Road (604) 727-1244 cell
Delta, BC V4G 1B8 (888) 820-5852 pager (major
cities)



Hi,

My favourite array manupilation is the following... char *cal_ptr;

cal_ptr = (char *)&(cal[x]);

Where cal[] is an array of structures but could just as well be a elementry
array of some sorts. ----- Original Message -----
From: "Daniel Friederich" <>
To: <>
Sent: Tuesday, November 26, 2002 2:40 AM
Subject: RE: [68HC12] Re: Arrays permitted in CodeWarrior for HC12? > Actually, you can pass (almost) any C type as argument in C. The only
thing special with arrays is that they are implicitly passed
> by reference, and therefore it better to pass a pointer instead of an
array to show this explicitly. The resulting code is
> identical.
> Passing large structs as argument is inefficient, especially in its stack
usage, and should be avoided.
>
> As return value, you can use also (almost) any type except arrays. This
makes sense as passing arrays by reference cannot be done
> for arrays allocated on the stack. You can however still put the array
inside of a struct and return it, but here the same remark as
> for the passing of structs. It's slow and it needs a lot of stack.
>
> Anyway, here's a little example (which wastes a lot of stack space):
>
> typedef int intArray[2];
> typedef struct { intArray arr; } arrayInStruct;
>
> /* note: intArray foo(intArray arr); is illegal in C. No array return */
> arrayInStruct foo(intArray arr) {
> arrayInStruct str;
> arr[1]++; /* CAREFUL: changes globArray, as arr is passed by reference
*/
> str.arr[0]= arr[0];
> str.arr[1]= arr[1];
> return str;
> }
>
> intArray globArray= {1,2};
> arrayInStruct globStruct= {{4,5}};
> void main(void) {
> globStruct= foo(globArray);
> }
>
> Bye
>
> Daniel
>
> > -----Original Message-----
> > From: Erich Styger [mailto:]
> > Sent: Tuesday, November 26, 2002 6:17
> > To:
> > Subject: RE: [68HC12] Re: Arrays permitted in CodeWarrior for HC12?
> >
> >
> > for C, you only can pass simple types (char, int, pointer, etc) as
argument,
> > but no arrays or structs (you have to use pointers).
> > However, as return value, you can return a struct (struct-return).
> >
> > myStructType foo(void) {
> > ...
> > }
> >
> > Erich
> >
> > > -----Original Message-----
> > > From: Andrei Chichak [mailto:]
> > > Sent: Monday, November 25, 2002 5:03 PM
> > > To:
> > > Subject: Re: [68HC12] Re: Arrays permitted in CodeWarrior for HC12?
> > >
> > >
> > > At 03:12 PM 11/23/2002 -0800, you wrote:
> > > >You can pass/return a "struct" I believe, but it's not as efficient
as
> > > >passing/returning an address of an array (or whatever).
> > > >
> > > >GB
> > >
> > > GB! You should know better than that! C will only pass simple types,
ints,
> > > floats, pointers to complex types (structs, arrays), etc. You can
appear to
> > > pass in a struct, but you only pass a pointer to it. You can't pass
out a
> > > complex type, but you can pass out a pointer to one. DON'T pass out a
> > > pointer to a local struct, it goes out of scope when the routine ends.
> > >
> > > Hey GB, are you doing Thanksgiving on Thursday or did you do it last
month?
> > >
> > > AC
> > >
> > >
> > > --------------------
> > >
> > >
> > >
> > > ">http://docs.yahoo.com/info/terms/
> > >
> > >
> >
> >
> > --------------------
> >
> >
> >
> > ">http://docs.yahoo.com/info/terms/
> >
> >
> -------------------- >
> ">http://docs.yahoo.com/info/terms/




On 11:25 AM 11/25/2002 +0000, the keyboard of emmited:
>a small example of what you are trying to do would be helpful.
>Note that you cannot directly return an array with a C function:
>you have to use a pointer instead (no return by value).
>Maybe this is your problem?

Yes, this was my problem. I tried:

char * f(void);

Now I changed to:

void f(char*); --
Andres Rosado
Email:
ICQ: 66750646
Homepage: http://andres980.tripod.com/

If we all work together, we can totally disrupt the system.