c how to make a dynamic array pointer in a struct - c

I am taking in input from the user for the number of elemenets inside an array. The array is inside my struct 'Polymer'
struct Polymer
{
int length;
struct Monomer *monomer;
}polymer;
In main, I am creating a new monomer array pointer and setting the mononomer pointer in "Polymer" to it
struct Monomer *monomers[size];
polymer.monomer = momomers;
I am getting the error "Assignment from incompatible pointer type" which I assume is because we are converting a monomer array pointer to a monomer. How do I declare it as a monomer array pointer in the struct?

You are declaring an array of monomer pointers when you probably want an array of monomers. Drop the *:
struct Monomer monomers[size];
polymer.monomer = momomers;

struct Monomer *monomers[size];
polymer.monomer = momomers;
monomers is an array of pointers. They aren't pointing to any valid locations and has garbage values. While Polymer::monomer is a pointer. Array of pointers isn't type compatible to just a pointer.
Instead try -
struct Monomer monomers[size];
polymer.monomer = momomers; // 2
Now this statement 2 is valid because array decays to a pointer.

Related

How to store a memory address of a 2d array in a pointer member of a struct in C

The struct is
struct cloud {
//declare a pointer to point to the 2d array
char *pointer;
};
The main function is given (struct cloud u*) as a parameter, in order to refer to the structure
The rest of the function involves assigning the values to a 2d array arr, then I need to make the pointer in the struct point to the memory location of the 2d array.
So far I've done the following and the memory address of the struct pointer does not change after the assignment.
printf("2d array memory address is %p\n", arr);
printf("struct array memory address before assignment is %p\n", &(u->pointer));
u->pointer = arr;
printf("struct array memory address after assignment is %p\n", &(u->pointer));
As you wrote in the comment you have a one-dimensional array declared like
char *arr[rownum];
Within the structure you can declare a pointer that will point to the first element of the array like
struct cloud {
char **pointer;
};
and then write something like
u->pointer = arr;
Pay attention to that if the array is a local variable of a function and an object of the structure is declared outside the function then after exiting the function the pointer will have an invalid value because the array will not be alive.

array of structure pointers

So I have 3 files: main.c,countries.h and countries.c
I declare pointer of the structure called "Country" in the countries.h
I have included the countries.h in countries.c and in main.c
and declared the structure its self in countries.c
countries.h
typedef struct Country* pCountry;
countries.c
struct Country {
char *name;
pCity cities;
int numCities;
pTerritory countryTerr;
};
now, I want to create array of pointers of the Country structure, using malloc
so I did that:
pCountry countries_array;
countries_array = (pCountry);
malloc(num_of_countries*sizeof(countries_array));
and to assign pointers to each pointer,even though the malloc, does seems to work I cant
assign pointers to the elements in the array using []:
countries_array[0]= new_pointer;
I get "invalid use of undefine struct country" and "derefrecing pointer to incomplete",
what is the problem with the code?
thanks
Looks good. Just assign it to something of the same type, struct Country. Also, as pointed out in the comments, it should be malloc num_of_countries * sizeof struct Country (not the pointer type), which is now correctly dereferenced below as sizeof (*countries_array) which also works.
pCountry countries_array;
countries_array = malloc(num_of_countries * sizeof (*countries_array));
struct Country Jefferson = {"Jefferson", 1,2,3 };
countries_array[0] = Jefferson;
// don't forget to free the memory when no longer needed.
free (countries_array);
If we must put a pointer into this array of structs, we can either dereference the pointer like countries_array[0] = *pointer, or... we could declare countries_array as an array of pointers, instead of an array of structs. Perhaps this is what you may want. Either way, the actual structures have to occupy memory somewhere...
pCountry *countries_array = malloc(num_of_countries*sizeof countries_array);
pCountry j = &Jefferson; // `&`, "address of" operator
countries_array[0] = j; // put a `pointer` into the array...

Accesing values on a 2d struct array

So I have this struct
struct cell
{
int downwall;
int rightwall;
};
I have dynamically allocated memory for a 2d array of struct cell
(struct cell ** array)
However when I try to access a certain cell with the command
array[i][j] -> downwall = 0;
I get this error:
invalid type argument of '->' (have 'struct cell')
Use
array[i][j].downwall = 0;
instead.
You would have used -> if arrray[i][j] had type struct cell* which it doesn't have. It has type struct cell.
The type of array[i][j] will be of struct cell, not struct cell *. You should use the . operator to access a member.
You need to write
array[i][j].downwall = 0; // use of .
Please note that
struct cell** array
is not a 2D array! it is a pointer to a pointer of type 'struct cell'.
You should regard it as a 2D array only if there is an allocated memory to which the values point (Statically or dynamically). Otherwise, you are on the road for a Segmentation Faults.
Your struct is not a pointer struct so simply do something like:
//array cells of type cell and can hold 10 cell's
struct cell cells[10];
//Try using malloc for memory allocation
cells[0] = (struct cell *) malloc(sizeof(struct cell));
//example assignment
cells[0].downwall=0;
cells[0].rightwall=1;
You need to declare an actual array with the right number of indices, then make the pointer point to it. Use typed names to help ( simplified Hungarian Notation )
int iAry[M][N];
int **ptrAry;
ptrAry = iAry; /* equivalent to ptrAry = &iAry[0][0]; */
/* then use -> operator as you have done */

typedef, arrays and pointers in C

I am studying a code written in C language.
The following part isn't clear for me:
typedef uint8_t data_t[4][4];
typedef struct {
data_t *data;
...
} my_struct;
The thing that I don't understand is, what is the type of data?
Plus, I want to assign values to this variable.
For example, in the code there is:
my_struct st;
st.data = (int8_t *)array
where array is defined as int8_t *array.
I don't understand how this assignation works, could someone explain it clearly?
To finish, is it possible to assign values to my data_t variable without declaring it as a pointer in my structure?
The thing that I don't understand is, what is the type of data?
The type of data is a pointer to a two-dimensional array. That is uint8_t(*data)[4][4].
See C right-left rule for deciphering C declarations.
Plus, I want to assign values to this variable st.data = (int8_t *)array.
In this case array must have the same layout as uint8_t[4][4] array. Since arrays are contiguous in C, that array must have at least 4 * 4 elements of type int8_t.
The fact that you have to cast the array with (uint8_t*) first implies that array has a different type and that may cause trouble.
Note that this is a pointer assignment only, not an element-wise copy of array.
is it possible to assign values to my data_t variable without declaring it as a pointer in my structure?
It is possible if data is not a pointer, i.e. declare it as data_t data;. And then copy into it using memcpy.
This declaration
typedef uint8_t data_t[4][4];
declares name data_t as an alias for type uint8_t [4][4].
Thus in the structure definition
typedef struct {
data_t *data;
...
} my_struct;
member data is a pointer of type data_t * that is the same as uint8_t ( * )[4][4].
So it is a pointer to a two-dimensional array of pointers to objects of type `uint8_t
Arrays do not have the assignment operator. You have to copy elements of one array into another.
If for example you have another one-dimensional array of pointers like
uint8_t *array[4];
you could write
my_struct st;
st.data = malloc( sizeof( data_t ) );
memcpy( st.( *data )[0], array, 4 * sizeof( uint8_t * ) );
Take into account that as the data is a pointer you have at first to allocate memory where you are going to copy objects of type uint8_t *.

Pointer to Two Dimensional Structure Array

I have a Structure say MyStruct:-
#define SEC_DIMENSION 2
struct MyStruct
{
char cChar;
float fFloat;
int iInt;
};
struct MyStruct StructArray[SEC_DIMENSION][20]; //Two Dimensional Array of Structures.
Now I want to access this with Pointer.
struct MyStruct *StructPtr[SEC_DIMENSION];
I did assignment as follows:-
StructPtr[0] = &StructArray[0][0];
Now, I want to access Members of Structure StructArray[0][1] i.e. StructArray[0][1].cChar or StructArray[0][1].fFloat
How can I access them by using StructPtr?
I tried using StuctPtr[0][1]->cChar then ((StructPtr[0])[1])->cChar
Both returned an error.
With StructPtr[0]->cChar build was successful. But this is not what I want.
"Now I want to access this with Pointer" is not very descriptive. What you have in your example will not work. What you declared is not a pointer to an array but rather an array of pointers. Is that what you need? I don't know.
If you really need a pointer to an array, you can declare your pointer it as
struct MyStruct (*StructPtr)[20];
and then make it point to your array as
StructPtr = StructArray;
From this point on you can access the original array through this pointer as StructPtr[i][j]
StructPtr[i][j].cChar = 'a';
Alternatively, you can declare the pointer as
struct MyStruct (*StructPtr)[SEC_DIMENSION][20];
and then make it point to your array as
StructPtr = &StructArray;
From this point on you can access the original array through this pointer as (*StructPtr)[i][j]
(*StructPtr)[i][j].cChar = 'a';
I think you need a "pointer to an array of dimension [SEC_DIMENSION][20] of structures of type struct MyStruct":
struct MyStruct (*StructPtr)[SEC_DIMENSION][20];
StructPtr = StructArray;
StructPtr[i][j]->cChar;

Resources