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?

3
  • Like an array of structures with two members? Or an array of two arrays of strings? Commented Dec 3, 2016 at 17:53
  • 2
    Are the columns related? From the text it looks more like you want a struct of two "strings". Commented Dec 3, 2016 at 17:54
  • 2
    something like const char *array[2][4] = {{"Cp", "Mu", "H", "Si"}, {"Specific Heat", "Viscosity", "Enthalpy", "Surface Tension"}};? Commented Dec 3, 2016 at 18:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.