Hello, I'm trying to compile some code with uses an array as the output of a function. It's constantly saying that it expects a modifiable lvalue. What can be the problem? I'm running out of ideas (and time). Thank you very much for your help! Andres Rosado |
|
Arrays permitted in CodeWarrior for HC12?
Started by ●November 23, 2002
Reply by ●November 23, 20022002-11-23
At 09:58 PM 11/23/2002 +0000, you wrote: >Hello, > >I'm trying to compile some code with uses an array as the output of >a function. It's constantly saying that it expects a modifiable >lvalue. What can be the problem? I'm running out of ideas (and time). If you mean array = f(x); No, that's not legal C. Try something like f(x, array). See chapter 5 of K&R. Andrei >Thank you very much for your help! > >Andres Rosado > >-------------------- > >">http://docs.yahoo.com/info/terms/ |
|
Reply by ●November 23, 20022002-11-23
--- In 68HC12@y..., Andrei Chichak <acpmiedm@t...> wrote: > At 09:58 PM 11/23/2002 +0000, you wrote: > >Hello, > > > >I'm trying to compile some code with uses an array as the output of > >a function. It's constantly saying that it expects a modifiable > >lvalue. What can be the problem? I'm running out of ideas (and time). > > If you mean array = f(x); > > No, that's not legal C. Try something like f(x, array). See chapter 5 of K&R. I'll try. Thanks! AR |
Reply by ●November 23, 20022002-11-23
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 ----- Original Message ----- From: "Andres Rosado" <> To: <> Sent: Saturday, November 23, 2002 2:43 PM Subject: [68HC12] Re: Arrays permitted in CodeWarrior for HC12? > --- In 68HC12@y..., Andrei Chichak <acpmiedm@t...> wrote: > > At 09:58 PM 11/23/2002 +0000, you wrote: > > >Hello, > > > > > >I'm trying to compile some code with uses an array as the output > of > > >a function. It's constantly saying that it expects a modifiable > > >lvalue. What can be the problem? I'm running out of ideas (and > time). > > > > If you mean array = f(x); > > > > No, that's not legal C. Try something like f(x, array). See > chapter 5 of K&R. > > I'll try. Thanks! > > AR > > -------------------- > > ">http://docs.yahoo.com/info/terms/ |
|
Reply by ●November 24, 20022002-11-24
Hello Andreas, 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? Erich > -----Original Message----- > From: Andres Rosado [mailto:] > Sent: Saturday, November 23, 2002 10:58 PM > To: > Subject: [68HC12] Arrays permitted in CodeWarrior for HC12? > Hello, > > I'm trying to compile some code with uses an array as the output of > a function. It's constantly saying that it expects a modifiable > lvalue. What can be the problem? I'm running out of ideas (and time). > > Thank you very much for your help! > > Andres Rosado > > -------------------- > > ">http://docs.yahoo.com/info/terms/ |
Reply by ●November 25, 20022002-11-25
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 |
|
Reply by ●November 26, 20022002-11-26
----- Original Message ----- From: "Andrei Chichak" <> To: <> Sent: Monday, November 25, 2002 8:02 AM Subject: Re: [68HC12] Re: Arrays permitted in CodeWarrior for HC12? > 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. > > AC I'm not sure where I picked it up, but it could have been while doing some C programming on a PC (using a C++ compiler). Things like assigning structs to each other and passing by value were (are) possible (where are the compiler vendors today?). I don't have any of my books at home, but here is a tidbit from MSDN (the horror): "The C/C++ technique for passing parameters is always the same, regardless of calling convention: All parameters are passed by value, except for arrays, which are translated into the address of the first member. To pass data by reference, pass a pointer to it. Note that an array name in C is equated to its starting address. Therefore, arrays are passed by reference. To pass an array by value, declare a structure with the array as its only member." So it could be just a C++ compiler thing, but maybe not. I'll have to check with an ANSI-C book (and my compiler manual) to see which way my current tools go on this. Most of my programming in recent years has been on the itty-bitty end so using structs (let alone passing them around) is a distant memory. :( GB |
Reply by ●November 26, 20022002-11-26
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/ |
|
Reply by ●November 26, 20022002-11-26
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/ |
Reply by ●November 26, 20022002-11-26
Hi all, I need to enter the discussion here, because too many wrong things have been written regarding this topic in the last messages, and some clarifications from a compiler writer may be usefull. Arguments in C are not types, neither objects or variables; arguments are expressions, and the only way to pass the result of an expression is to copy it on the stack (physical or simulated, whatever). It means that any C simple object beeing copied, it is passed by value, then meaning that any attempt to modify such an argument inside the called function will modify a copy on the stack, and not the original object itself. This mechanism has to be detailed for compound objects: arrays, structures, and functions (yes, a function is a C object). Inside an expression, an array name is replaced by a pointer to its first element, with the exception of the sizeof operator. It means that the indirect '*' and index '[i]' operators can be applied indifferently on both arrays and pointers. An argument beeing an expression, if you try to pass an array to a function, the argument will be considered as a pointer to a C object, which can also be declared as an array inside the called function, or as a pointer. This is transparently handled by the compiler, but this behaviour has an important consequence. By passing an address, any attempt to modify an array element will actually modify the original array. This mechanism is then acting as for an argument passed by address (or reference). So passing an array is equivalent to passing a pointer. Note that in most cases, the compiler would be in trouble guessing an array size when declared like: 'extern int array[];', where no dimension is provided. For the same reason, arrays cannot be assigned; this would imply that the sizes are always known. This is different for structures, because they are usually completely defined, and their size usually known by the compiler. As a first result, structures can be assigned. This is not a pointer copy, but a full memory copy (this may cost some code and time). So, when a structure is passed as an argument to a function, it is actually copied completely on the stack. Of course, this is costing code, time, and stack space, and this is why this feature is usually avoided. It is much more efficient to pass explicitly the address of a structure by using the '&' operator, but in such a case, the argument must be declared as a pointer to such a structure explicitly inside the called function, and of course, any attempt to modify a structure field through this pointer will modify the original structure. Note that functions can be passed as arguments to functions, because a function name is also equivalent to a code address. Such an argument will have to be declared explicitly as a pointer inside the called function. The function call operator '()' may be used indifferently on both function names and function pointers. You can call a function pointer by dereferencing the pointer '(*pf)();' or directly 'pf();' as if pf was a function. Now, regarding the return statement, C is returning expressions, so you can simply consider the same mechanisms for returning values. Returning an array name will actually return a pointer to that array. Be cautious about returning the address of a local array, as well as the address of a local variable, because such an address may be irrelevant after the called function has returned. The local space has probably been cleaned out from the stack space and may be overwritten by another function call or an interrupt. Returning a structure will physically copy the returned structure to the destination (usually the assigned structure), and this will cost code and time. Unless the structure is small enough, it is more efficient to set directly the resulting structure inside the called function by passing the structure address as an extra argument, thus replacing 'str = fs(i);' by 'fs(&str, i);'. Returning a function name will physically return the function address. Sorry to have been so long and so professoral, but I thought it was necessary. Best Regards, Jean-Pierre Lavandier COSMIC Software -----Message d'origine----- De : Erich Styger [mailto:] Envoy: mardi 26 novembre 2002 06:17 : Objet : 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://www.motorola.com/mcu > ">http://docs.yahoo.com/info/terms/ <http://docs.yahoo.com/info/terms/> > > <http://rd.yahoo.com/M#7459.2675695.4055211.2225243/D=egroupweb/S065542 05:HM/A67611/R=0/*http://ad.doubleclick.net/jump/N2524.Yahoo/B1071650;sz= 300x250;ord38288019035540?> <http://us.adserver.yahoo.com/l?M#7459.2675695.4055211.2225243/D=egroupmai l/S=:HM/A67611/rand26365077> -------------------- <http://www.motorola.com/mcu ">http://docs.yahoo.com/info/terms/> . |