pointer to multiple arrays

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am wanting to create three arrays and have them pointed to by a pointer so that when I increment the pointer, it will move me to the next element in all of the arrays. How would I go about the declaration for this? Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With three arrays there is no way to do it since the pointer can only point to one of them. You can, however have a structure that has 3 members and have one array of that type. So instead of:

int firstArray[32];
float secondArray[32];
char thirdArray[32];

you have:

typedef struct arrayElement
{
   int firstMember;
   float secondMember;
   char thirdMember;
};

arrayElement finalArray[32];

Then finalArray++ will take you to the next element. To access the data would be finalArray[i].firstMember, etc.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You cannot have one pointer that points to three different arrays at once.

You probably want to access three parallel arrays, in which case just use an integer index:

char ear[20];
long nose[20];
short throat[20];

int index;

index = 5;
printf("ear:%c nose:%ld throat:%d\n",
 ear
, nose
, throat
);

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or you can try multidimensional arrays.

#define SIZE_OF_ARRAY 20
uint8_t *ptr[SIZE_OF_ARRAY];
uint8_t a[3][SIZE_OF_ARRAY];

ptr = &a[0][0];