EmbeddedRelated.com
Forums

Dragon Board 12 Serial Communication C code Help

Started by zach...@gmail.com October 26, 2009
On Oct 31, 2009, at 7:54 AM, Bill_CT wrote:

> --- In 6..., Andrei Chichak wrote:
>> On 2009-October-28, at 11:06 PM, Bill Auerbach wrote:
>>>>> Andrei Chichak writes on 02:20 AM 10/28/2009
>>>>> In the definition of encode - either use pointers or arrays, don't
>>>>> intermix. Use void encode( char out[]) instead. What you have
>>>>> would
>>>>> probably cause all kinds of damage.
>
> You say "probably cause all kinds of damage". Before anyone here
> thinks that's a really bad thing and it means they are doing
> something wrong, perhaps you should back this statement with
> something concrete. Show us a case that causes "all kinds of
> damage". If you can't, please don't put your gloom and doom
> programming rules out on a public forum where beginners may be
> trying to learn correct programming practices.

This struck me as particularly strange since I've been mixing pointers
and arrays since 1980 (when I first used C) and have never caused any
kind of damage! In fact you really need to mix the two to get maximum
leverage from the language.

1. You have to declare arrays to allocate memory since I think most of
us can agree that malloc has no
business in embedded programming of small microcontrollers.
2. It is generally more efficient to walk through an array using
pointers rather than indexing.
3. Array and pointer parameters are basically identical since the
array is passed by reference and the pointer is passed by value. They
can be used interchangeably.
4. The comment "the only time a problem will occur is when a pointer
and array are used in different modules (one public and the other
externed)" isn't an issue if using header files with extern
declarations for all global variables is practiced. This will catch
the definition being different than the declaration.

Tom Almy
Tualatin, Oregon USA
Internet: t...@almy.us
Website: almy.us
> Tom Almy writes on 10:57 AM 10/31/2009
>This struck me as particularly strange since I've been mixing pointers
>and arrays since 1980 (when I first used C) and have never caused any
>kind of damage! In fact you really need to mix the two to get maximum
>leverage from the language.

Technically "Hello World" mixes an array used with printf which takes
a pointer.

>1. You have to declare arrays to allocate memory since I think most of
>us can agree that malloc has no
>business in embedded programming of small microcontrollers.

Agree.

>2. It is generally more efficient to walk through an array using
>pointers rather than indexing.

Yes. I might not be wrong to say it's always more efficient.

>3. Array and pointer parameters are basically identical since the
>array is passed by reference and the pointer is passed by value. They
>can be used interchangeably.

Yes. The one place that can burn you because of this is sizeof used
on an argument to a function with an array type:

void function(char buffer[32])
{
int i = sizeof buffer; // This is NOT 32 but sizeof (char *)
}

However, this isn't going to cause "damage" because this error would
fall out quickly in testing.

>4. The comment "the only time a problem will occur is when a pointer
>and array are used in different modules (one public and the other
>externed)" isn't an issue if using header files with extern
>declarations for all global variables is practiced. This will catch
>the definition being different than the declaration.

I agree. It shouldn't be an issue, but since it can be done, it is a
problem when it is. I've seen a lot of programmer's code over the
years and there's a lot of "quick extern" declarations that are
included in the C file and not a header file. It's not a good
practice but it's still pretty common.
Bill