The objective of my code is as follows:
Return a pointer to the array of innerStruct
either through single or double pointer (I believe I should use double pointer)
Pass that pointer to a function that modifies the value
It seems that I get a seg fault in the online compiler.
#include <stdio.h>
typedef struct
{
int innerVal;
} innerStruct;
typedef struct
{
int a;
innerStruct * inner[3];
} myStruct;
static myStruct * m1;
innerStruct ** getInnerPtrToArray()
{
return &(m1->inner);
}
void processInnerStruct(innerStruct * array_ptr[])
{
for(int i=0; i<3; i++)
{
array_ptr[i]->innerVal = i;
}
}
int main()
{
innerStruct ** array_ptr = getInnerPtrToArray();
processInnerStruct(array_ptr);
return 0;
}
Online compiler: https://onlinegdb.com/r1z0DPICb
Since inner
is an array of pointers, you need to allocate memory for them to point to. You also need to allocate memory for m1
to point to.
int main()
{
m1 = malloc(sizeof(myStruct));
for (int i = 0; i < 3; i++) {
m1->inner[i] = malloc(sizeof(innerStruct));
}
innerStruct ** array_ptr = getInnerPtrToArray();
processInnerStruct(array_ptr);
return 0;
}
Here is a second cut on what you were doing. Rather than allocating, you can also change myStruct
to simply contain an array of innerStruct
rather than an array of pointers to innerStruct
. Then no allocation is needed, eg
#define NINNER 3 /* if you need a constant, define one */
...
typedef struct {
int a;
innerStruct inner[NINNER]; /* you are not allocating */
} myStruct;
note: avoid the use of globals, they are not needed. Declare your variables in main()
and pass as parameters as required.
Putting it altogether, adjusting types as required, and including further notes as comments in-line below, you could do something like the following:
#include <stdio.h>
#define NINNER 3 /* if you need a constant, define one */
typedef struct {
int innerVal;
} innerStruct;
typedef struct {
int a;
innerStruct inner[NINNER]; /* you are not allocating */
} myStruct;
/* takes pointer to m1, returns address of array of 3 innerStruct */
innerStruct *getInnerPtrToArray (myStruct *innerptr)
{
return innerptr->inner;
}
/* takes pointer to array of innerStruct, fills */
void processInnerStruct (innerStruct *array_ptr)
{
for (int i = 0; i < NINNER; i++)
array_ptr[i].innerVal = i;
}
int main (void) {
myStruct m1 = { .a = 10 }; /* don't use globals */
/* pass the address of m1 */
innerStruct *array_ptr = getInnerPtrToArray (&m1);
processInnerStruct (array_ptr);
printf ("m1.a: %d\n", m1.a); /* output values */
for (int i = 0; i < NINNER; i++)
printf (" .inner[%d]: %d\n", i, m1.inner[i].innerVal);
return 0;
}
Example Use/Output
$ ./bin/inner
m1.a: 10
.inner[0]: 0
.inner[1]: 1
.inner[2]: 2
It's up to you to determine whether you intended to allocate for each of the 3
innerStruct
in myStruct
, but from my reading of what you were attempting to do, and with no stdlib.h
included, it appeared your intent was to handle the array without allocation. Let me know if you have further questions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.