Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access and array of strings

Status
Not open for further replies.

JasonWB007

Programmer
Dec 21, 2005
14
US
Hi everyone,

Sorry if this is too simple, but I am trying to figure out how to access an array of strings, using an index for the string. I have:

typedef struct
{
PARMTYPE parmtype;
_iq* parmptr;
int num_indices;
const char *(indices[10]);
// const char *indices;
} PARMTAB;

and I want to be able to access an array of strings, such as:

// PD biquad coefficients
#define NUM_PD_COEFF_INDICES COEFFS_PER_BIQUAD*NBIQ*NUM_PID_LOOPS
const char PD_Coeff_Indices[NUM_PD_COEFF_INDICES][MAX_CONTROLPARMINDEXLIST_ITEM_SIZE] =
{
"Speed a2\0", "Speed a1\0", "Speed b2\0", "Speed b1\0", "Speed b0\0",
"Temp a2\0", "Temp a1\0", "Temp b2\0", "Temp b1\0", "Temp b0\0"
};

// Offset all the way into the individual string
// parm_index_ptr = parmtab[parm_index].indices + 10*index;
parm_index_ptr = parmtab[parm_index].indices[index];

The commented ways were the old ones that worked, but I would really like to be able to specify which character array I want by an index, not by a bunch of silly math. Any ideas?

Thanks,

Jason
 
Just watch the position of the parentheses in the member declaration in the struct.
Code:
#include <stdio.h>
#include <stdlib.h>

#define NUM_PD_COEFF_INDICES                10
#define MAX_CONTROLPARMINDEXLIST_ITEM_SIZE  10

const char PD_Coeff_Indices[NUM_PD_COEFF_INDICES][MAX_CONTROLPARMINDEXLIST_ITEM_SIZE] =
{
  "Speed a2",   "Speed a1",   "Speed b2",   "Speed b1",   "Speed b0",
  "Temp a2",    "Temp a1",    "Temp b2",    "Temp b1",    "Temp b0"
};

typedef struct
{
  const char        (*indices)[MAX_CONTROLPARMINDEXLIST_ITEM_SIZE];
} PARMTAB;

int main(void)
{
  PARMTAB foo;
  foo.indices = PD_Coeff_Indices;
  printf( "A string = %s\n", foo.indices[5] );    /* temp a2 */
  printf( "A char   = %c\n", foo.indices[5][2] ); /* 'm' of the above string */
  return 0;
}

Use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
OK, I think I am getting closer to figuring this out - thanks! But if I am right, this declares an array of 10 pointers. What I want to do is declare a "pointer to an array of character arrays, each of which is 10 characters (max) long". The idea being I would like to store a single pointer to the array and then use and index to find the string I want, not a pointer to each entry. Is that possible?

Thanks,

Jason
 
> But if I am right, this declares an array of 10 pointers.
No it doesn't.
[tt]char *arr[10]; [/tt]is an array of 10 pointers
[tt]char (*arr)[10]; [/tt]is a pointer to an array of 10 characters
The () are extremely important here.

> The idea being I would like to store a single pointer to the array and then use and index to find the string I want,
> not a pointer to each entry. Is that possible?
Which is EXACTLY what I posted.

Just do sizeof() on the struct and you'll see that there is only one pointer there, not 10 of them.

--
 
OK, thanks. Sorry for the confusion! I do appreciate your replies.

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top