what is use of array in embedded c programming

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi,
generally I have seen array is use for storing element, letters or message .

C arrays are declared in the following form
type name [number of elements];

For example, if we want an array of five integers , we write in C:
int numbers[5];

For a five character array
char letters[5];
type name [number of elements]={comma-separated values}
For example, if we want to initialize an array with five integers, with 1, 3, 5, 0, 9, as the initial values: int number[5]={1,3,5,0,9};

Example of array
int Array[5] = {1,2,3,4,5};

char days[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

generally, we deal with device such as sensor , LCD..etc in embedded system. what is use of array in embedded c programming. Is it only use for storing element, letters or message or use of any thing else . what are the common examples of array in embedded c programming
 
When you interface lcd and write any msg in program like "BINGO" where it get stored ? In an array.
Knowingly or unknowingly you uses array.
When you access string using pointer data structure required is array.
When you will enter in the world of rtos you will find arrays are used there for Queue service implementation.
Suppose you using UART or some other communication protocol in which you accepting string you will store it in array.
etc........................................................................................................................................................................................
measure the dots that much places you can use array
 

simozz

Joined Jul 23, 2017
126
Hello,
Parth786 said:
C:
char days[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
This is wrong and should be:

C:
char *days[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Parth786 said:
what are the common examples of array in embedded c programming
There are not different or additional uses respect to a non embedded application.
simozz
 

simozz

Joined Jul 23, 2017
126
Hello,
Parth786 said:
Why that is wrong, char *days is pointer.
A string in C is a char pointer.
The following are possible ways of declaring C strings:

C:
char *a = "blabla";
char b[] = "abcdef";
So the previous code
C:
char *days[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
declares an array of 7 char pointers.
If you declare an array of N char elements, each element will be 1 byte long.
The following declarations are equivalent:
C:
char name[6] = { "simozz" };
char name[6] = { 's', 'i', 'm', 'o', 'z', 'z' };
simozz
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hello,
simozz
sorry but now looking your example I am confused between array, pointer. I explained what is array in my previous post
Example of array
int Array[5] = {0,0,7,8,6};
Char Array[5] =
{P,A,R,T,H};
suppose I want to store element , mix of numbers and characters like PARTH786. which data type should i use int or char. I am confused because I have read somewhere that array is use to store similar data type elements

When should we use pointers in a C program? What is the difference between array and pointer?
 

simozz

Joined Jul 23, 2017
126
Hello,
suppose I want to store element , mix of numbers and characters (...) which data type should i use int or char.
Take a look at C structs.
However, if you can't difference how to use integer data type or char data type, I suggest you to get a book on C programming.
Study it & practice with it at the same time.

You can start with this wikibook, or the GNU C Reference Manual. Every Linux distribution comes with gcc preinstalled or available from repos. This could be a good starting point.
simozz
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hello,

Take a look at C structs.
However, if you can't difference how to use integer data type or char data type, I suggest you to get a book on C programming.
Study it & practice with it at the same time.

You can start with this wikibook, or the GNU C Reference Manual. Every Linux distribution comes with gcc preinstalled or available from repos. This could be a good starting point.
simozz
I understand the use of data types. its totally depend on size. my main question is that When should we use pointers in a C program? What is the difference between array and pointer?
 

ErnieM

Joined Apr 24, 2011
8,377
An array is one special case of a pointer. A pointer points toward... something. It may point to a single type such as a char or an interger, even another pointer.

An array is actually also a pointer which points to the first member of a group of like types.

Pointers hold a specific memory address. If you add to a pointer it doesn't count that as memory locations but the size of the type it points toward.
 

Papabravo

Joined Feb 24, 2006
21,225
Never mind the qualifier: "in embedded C programming". Maybe you should answer the more fundamental question" "What is the use of an array"?
The answer is that it is a data structure composed of one or more elements, all of which have the same data type. Now add the qualifier and the answer is still the same.
 

philba

Joined Aug 17, 2017
959
It's interesting to hear these questions. I wonder if the current state of software/programming instruction is so divorced from the underlying hardware that the concept of a pointer to something is completely ignored. I mean C++ and object orientation are good tings but it's sad that this code fragment
while(*s) *p++ = *s++;​
is deeply confusing to so many.
 

WBahn

Joined Mar 31, 2012
30,062
sorry but now looking your example I am confused between array, pointer. I explained what is array in my previous post
Example of array
int Array[5] = {0,0,7,8,6};
Char Array[5] =
{P,A,R,T,H};
suppose I want to store element , mix of numbers and characters like PARTH786. which data type should i use int or char. I am confused because I have read somewhere that array is use to store similar data type elements

When should we use pointers in a C program? What is the difference between array and pointer?
I'm going to assume you meant 'char' and not 'Char' (since we are talking 'C' and not 'C++' and since the post editor loves to capitalize things for you).

In

int Array[5] = {0,0,7,8,6};

You have an array containing five variables of type 'int' (which can be anything from 16 bits to 64 bits each, depending on your compiler) with integer values, almost certainly represented using two's complement, 0, 0 7, 8, and 6 stored in them.

The statement

char Array[5] = {P,A,R,T,H};

is almost certainly illegal, unless P, A, R, T, and H are #defined somewhere as suitable values that can be acceptably coerced into value of type 'char'.

You probably meant

char Array[] = {'P','A','R','T','H'};

in which case you have an array of five variables of type 'char' (which is probably one byte, but could be different depending on your compiler) with the codes (usually ASCII) for the characters 'P', 'A', 'R', 'T', and 'H' stored in them.

Notice that when you give a complete initialization list for an array, you don't (and arguably shouldn't) also provide an explicit size for the array dimension. So these could also have been declared using

int Array[] = {0,0,7,8,6};
char Array[] = {'P','A','R','T','H'};

Without the size, the compiler looks at the initialization list and determines how big to make the array

What would be different if you did

char Array[] = "PARTH";

A "string" in C is a NUL-terminated array of characters, so this array holds six values of type ends, the last of which is the NUL terminator. It is equivalent to

char Array[] = {'P','A','R','T','H', '\0'};

What about the following:

char *Array = "PARTH";

In most respects, this is the same as either of the earlier two declarations, but there is one important difference. In all cases, the value of the variable 'Array' is the address at which the first character (the code for 'P') is stored. However, in the last case the data might be stored within the program code or some other place that either shouldn't or perhaps even can't be changed.

Now what about if you want your initializer to be {"Bob", "Tom", "Sue"}?

Well, look at what it is. All three elements are, on their own, strings (or arrays of characters). An array is referenced by the memory address of the first character. So this initializer is actually three memory addresses. That means that you need to store them in an array in which each element can hold the memory address of an array of characters -- also known as a pointer of type 'char'.

char *Array[] = {"Bob", "Tom", "Sue"};
 

nsaspook

Joined Aug 27, 2009
13,279
It's interesting to hear these questions. I wonder if the current state of software/programming instruction is so divorced from the underlying hardware that the concept of a pointer to something is completely ignored. I mean C++ and object orientation are good tings but it's sad that this code fragment
while(*s) *p++ = *s++;​
is deeply confusing to so many.
These questions have little in common with the current state of software/programming instruction. They are mainly related to a one person search for answers, over and over.
 

Papabravo

Joined Feb 24, 2006
21,225
These questions have little in common with the current state of software/programming instruction. They are mainly related to a one person search for answers, over and over.
Is it my imagination or is the same question being asked in different epochs expecting a different answer?
 
Top