0

Is it possible to define and initialize 2 columns of strings with a single array? I mean I want to initialize an array with following values: {"Cp", "Mu", "H", "Si"} -> Column-1 {"Specific Heat", "Viscosity", "Enthalpy", "Surface Tension") -> Column-2 How can I do it? Will it be easier by using pointers?

skrath
  • 1
  • 1

1 Answers1

1

You could perhaps use an array of a structure, as so,

struct property{
char col1[size_of_row];
char col2[size_of_second_row];
};
struct property list[size_of_list];

Or, rather, if the number of elements in the list is not known, you could use an array of pointers, with each pointer pointing to a node with a property under column 1 and column 2. You can refer to dynamic array of dynamically allocated structs

Community
  • 1
  • 1
mahesh Rao
  • 375
  • 1
  • 4
  • 16
  • I have written following code: 3 struct Property { 4 char *Sym[4]; 5 char *SymName[4]; 6 }; 7 static const struct Property Props = { {"A", "Helmholtz Free Energy", 8 "Cp", "Specific Heat at Constant Pressure", NULL}, 9 {"Cv", "Specific Heat at Constant Volume", 10 "D", "Density", NULL} 11 }; ... 58 while (Props.Sym[i]) 59 printf("%s\n", Props.SymName[i++]); After printing 1st two values it displays "Segmentation fault" - why? – skrath Dec 03 '16 at 19:14
  • Segmentation fault generally occurs if you try to access memory, which does not lie in the declared region.So, your code should run, print out the 4 values of Props.SymName[i] and then show segmentation fault since you have declared Sym and SymName as arrays with 4 indices. I have tried running the code. Provide an `i < 4` for the while condition. Basically limit the while loop to declared size of the array. – mahesh Rao Dec 04 '16 at 03:01