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.

Accessing array of variables

Status
Not open for further replies.
Hello I am working on a project. I am using MicroC pro for PIC microcontroller. I have created, say, 10 arrays-

const char V01 [15]= { some 15 values }
const char V02 [15]= { some 15 values }
.
.
.
const char V10[15]= { some 15 values }

Now I want to access values from V01 to V10. I tried to make another array "VX" of variables V01 to V10. But could not succeed. I am also confused between pointers, array of pointers, addresses, datatype of variable VX etc and how to put in the code.

I also tried-

value = VX [n];

if (some logic) VX = V01;
else if (some logic) VX = V02;
.
.
.
else VX = V10;


Note- 2-D array is not a solution for me.
 
You are very nearly there.. Create one pointer

const char* VX;

Then point it to the start of each array..

if (some logic) VX = &V01[0];
else if (some logic) VX = &V02[0];
.
.
.
else VX = &V10[0];

Then access like this..

VX[10] = xxx;
 
Okay!! In C, an array name is essentially a pointer.. So!!

char array[10]; //Set up a variable array..

char * arrayptr = &array[0]; //Point to first location..

Now this statement..
x = array[3];

is exactly the same as

x = arrayptr[3];

if you had another character array..

char array2[10];

you can point to it with the same pointer..
arrayptr = &array2[0];

But the pointer can be moved whereas the array name cannot!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top