Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Variable Array in C

Status
Not open for further replies.

Jon Wilder

Active Member
Hi all. I'm not a beginner when it comes to PICmicro MCU's and am very proficient in Microchip as well as Intel assembly. But now I'm working on learning C language for embedded MCU's. I'm versed with the basics of it but I'm still learning how to implement certain structs. I'm using Microchip's XC8 compiler.

My question is how do you create a variable array? I've searched online and can't seem to find anything that illustrates how to do this.
 
I not quite sure what you're asking but if it's how to define an array then it's quite simple.

Code:
// a string array of 20 characters
unsigned char buffer[20];

// an initialized string array of characters
unsigned char buffer[] = "Hello World!";

//a const array (in rom)
rom char HW[]="Hello World!";

//an array of colours
rom char *RGB1 = {
	135,169,107,	//Asparagus
	255,164,116,	//Atomic Tangerine
	162,162,208,	//Blue Bell
	102,153,204,	//Blue Gray
	13,152,186,	//Blue Green
//lots removed for sanity
};

//etc.

The above is all C18.

Mike.
 
Basically what I'm asking is how to define a buffer of 16 variable characters that can be written out as a 16 byte page to a piece of hardware such as an LCD or an external memory chip. In assembly I would do the same thing using indirect addressing with the FSR/INDF.
 
You declare a 16 byte array like this:
uint8_t nameOfarray[16];

"uint8_t" is a C99 standard style variable type. You need to #include <stdint.h> to use it.

You can access the array by indexing:
nameOfArray[0] = value1;
nameOfArray[1] = value2;
// etc...
nameOfArray[15] = value16;

The compiler decides where the array memory is allocated. If you want to access specific memory location the codes gets a little more cryptic, but is not difficult.

This code writes a value 0xAA to a memory location 0x0123:
(*(uint8_t *)0x0123) = 0xAA; // cast memory location 0x0123 to a uint8_t pointer and "dereference" it for access.

You can use a direct memory address as an array like this:
((uint8_t *)0x0123)[0] = value1; // use indexing to write to an offset location from memory address. Notice the missing "dereference asterix".. the indexing [] does that in this case (with memory offset).
((uint8_t *)0x0123)[1] = value2;
// ...
((uint8_t *)0x0123)[15] = value16; // value 16 is written to memory location (0x0123+15)


You can also declare a pointer variable and assign it any memory address you like:
uint8_t* bufferPointer;
bufferPointer = (uint8_t*)0x0123;


If this was not what you were looking for, or was too confusing, please give a specific example problem and I can write it in C.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top