How to define an array having 2 columns of strings - c

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?

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

Related

How can use __attribute__ (section) for each element of an array?

I have a multidimensional array and I want to define each element of this array in a different section. Something like:
int array [2][200];
array[0] __attribute__((section (".section1")));
array[1] __attribute__((section (".section2")));
I know this piece of code is not correct because __atribute__ ((section "section name")) should be used in the array definition. Do you have any suggestions on how I can do it?
Thanks in advance
No. The array is a contigous chunk of memory and elements cannot be in different sections.
C standard (6.2.5.20):
An array type describes a contiguously allocated nonempty set of
objects with a particular member object type, called the element type.
Do you have any suggestions on how I can do it?
You need to have two separate arrays in different sections and array of two pointers referencing those arrays.

Sentinels in non-pointer arrays

Let's say I have the following array:
int numbers[] = {-2,4,9,11};
int sum=sum_list(numbers);
If I were to pass pointers, I could add in a NULL at the end of the list to signal the size of it. However, in the above non-pointer list, is there a way to do that? If not, is the way around this just to explicitly pass the size of the array to the callee, for example:
int sum_list(int numbers[], size_z len);
When dealing with an array of non-pointer values such as this, you basically have two choices:
Pass the length of the list to any function that also receives the list
Designate some value that is outside the range of acceptable values to use as an end-of-list indicator
A third choice would be to implement a Pascal-type array where index 0 contains the length of the array.

Uses of data types in linked lists

While creating a linked list, a data type is supposed to be defined beforehand. Here's a pseudocode for creating a new linked list:
Type ListNode
Declare Pointer as integer
Declare data as string
EndType
Declare Namelist[1-50] of Listnode
For Index=1 to 49
Namelist[Index].pointer=Index + 1
Endfor
Namelist[50].pointer=0
What confuses me is the similarity between newly defined data types and multidimensional arrays where multiple data elements could be stored.
How do newly defined data types allow the storage of multiple different data elements within a single array element?
Before answering your question, I want to define few things. I am considering C programming language here.
Array: An array is a container object that holds a fixed number of values of a single type.
Basic data-type: A basic type is a data type provided by a programming language as a basic building block.
Struct: A struct in the C programming language (and many derivatives) is a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address.
How do newly defined data types allow the storage of multiple different data elements within a single array element?
As mentioned above, array contains object of a single type. If you have defined a struct (in C) or a class (in Java), you can store objects of type defined by you in an array and each array element will be a compound element.
Example
typedef struct student {
int id;
char name[30];
} Student;
This defines a type called struct student and we have set a name (Student) for the type using typedef. Note that, typedef is just a way to alias a type with a specific name.
Now, we can declare variables or array of variables of type Student:
Student record;
Student records[10];
We can declare the above in the following way as well.
struct student record;
struct student records[10];
Here, record is a single variable and records is an array which contains 10 variables of type struct student.
Now each element of array records has two data elements, id (type int) and name (type char array). This is how you can store different data elements in a single element of an array.
Note that if you declare an array of primitive data types, say integer or double, you will only be able to store elements of a that specific type, not elements with different data types in the array.
Multidimensional array
int var[2][3];
Here, var is a multidimensional (2d) array which can hold 6 (2 x 3) integer values. Note that, even though var is a 2d array, it can only contain elements of type int, not others.
Your understanding about multidimensional arrays is incorrect. As you said, in multidimensional array, multiple data elements could be stored. It is correct but all the data elements will be of same type. So, there is no similarity between multidimensional array and our defined data types (say struct in C).

Julia - fixed size array in C struct

I need to make a Julia type corresponding to a C struct that has a fixed size array:
struct cstruct {
...
int arr[N] //N known at compile time
...
};
I have defined Julia types corresponding to other C structs with arrays like this:
type jstruct
...
arr::Ptr{Cint}
...
end
But as I understand it, this only works when arr is a pointer, not an array of a specific size. How can I ensure that the offsets of elements coming after arr remain the same in both languages?
When you define a C struct with a fixed size array (or with the array hack), the data are stored directly inline within that struct. It's not a pointer to another region. The equivalent Julia structure is:
type JStruct{N}
arr::NTuple{N,Int}
end
That will store the integers directly inline within the struct.
Note that if you want array-type operations on this object in Julia, the StaticArrays package might be useful. It uses tuples to store the elements of arrays, while also giving them an AbstractArray interface.

assigning two different arrays the same subscript in c

I can't seem to find the answer I'm looking for. I am working on a project that involves creating two arrays. One stores a string, the other stores a number (name and age). I have to print the arrays, once in the order they are entered, and once alphabetized. I can alphabetize just fine, but I cannot get the ages to line up with the right name. Is there a way to assign an array the same subscript as another array in C ?
Declare a struct like this:
struct person
{
char name[30];
int age;
};
Declare an array like this:
struct person record[2];
Use it like this (example):
strcpy(record[0].name, "Raju");
record[0].age = 24;
Now record[0] has both a name and an age. When you sort they will stay together.
Assuming the array of strings is a 2d matrix, where each row contains a name string, and if you want to keep the two arrays separate, then create a third array of indices, and sort the third array according to the array of names. Then display the names and ages using the third array of indices.
If the array of strings is an array of pointers to strings, you could create a array of indicesto the array of pointers to strings, then sort that array of indices according to the strings, then use the sorted indices to display the two arrays in sorted order.
If using a sort function where you supply a compare function, then create an array of pointers to the array of names (pointer[i] = &names[i]), sort the pointers according to the names, and convert the pointers to indices using pointer subtraction (index = pointer[i] - &name[0]).

Resources