Pointer to pointer member Struct - c

Its a part of code that i working on. When i try to assign Diagcb.end to ptr2 it returned an error.
I tried to write it as:
ptr2 = &Diagcb.end;
ptr2 = &diagcb->end;
but the same error is returned.
typedef struct reportType {
int a;
int b;
int c;
int* ptr;
} reportType;
typedef struct cb {
reportType* start;
reportType* end;
reportType arr[10];
}cb;
int fun (reportType* report);
main()
{
cb Diagcb;
reportType report;
report.a = 0;
report.b = 1;
report.c = 2;
report.ptr = NULL;
reportType* ptr2;
ptr2 = cb.end;
}

You should write it as ptr2 = Diagcb.end. Because Diagcd.end is already a pointer, you don't need to use & to get the address.

Related

How to access the value of a struct member which stores return value of a function pointer?

I have the following structure definitions:
typedef struct S_t S_t;
struct S_t {
float *s_ptr;
uint32_t ns;
};
typedef struct p_t p_t;
struct p_t {
int32_t pID;
float pVal;
};
typedef struct pr_t pr_t;
struct pr_t {
S_t *S;
int (*TrimS)(S_t *TS, int sSize);
p_t *TP;
};
I also have the following function defined:
int success = 0;
int failure = -1;
int ChopS(S_t *TS, int size)
{
int i, c;
for(i = 0; i < TS->ns; i++)
{
if (i >= size)
TS->s_ptr[i] = 0.0;
}
c = 0;
for(i = 0; i < TS->ns; i++)
{
if(TS->s_ptr[i] == 0.0)
c++;
}
if (c == size)
return success;
else
return failure;
}
I do the following (full code here) to assign values to each member of type pr_t , one of whose member is TrimS which is a status flag function pointer:
p_t *p2 = NULL;
S_t *tS2 = NULL;
tS2 = malloc(sizeof(S_t));
pr_t *SP2 = NULL;
SP2 = malloc(sizeof(pr_t));
p2 = (p_t *) malloc(sizeof(p_t));
SP2->S = tS2;
SP2->TP = p2;
SP2->TrimS = ChopS;
SP2->TrimS(tS2, 8);
If I try to access the value of the flag using SP2->TrimS, I get a junk value (or an address value I think) but not 0 or -1 as I expect. What is exactly happening over here? How can I access the value set by ChopS in SP2->TrimS?
You need to call the function to get the return value. It is not stored anywhere and the function pointer does not store the last return value. Use it as any "normal" function
if(SP2->TrimS(tS2, 8) == failure)
{
/* do something */
}
else
{
/* do something else */
}
or if you want to access it later (after the call) you need to store it in the variable or have an additional struct member to store it:
struct pr_t {
S_t *S;
int (*TrimS)(S_t *, int);
int last_TrimS_Return_Value;
p_t *TP;
};
/* .... */
SP2 -> last_TrimS_Return_Value = SP2->TrimS(tS2, 8);

Pointer to struct in C, what is the content?

I was wondering if I have a code like this:
struct something{
int x;
float y;
};
int main(void)
{
struct something *p;
p = malloc(sizeof(struct something));
p->x = 2;
p->y = 5.6;
return 0;
}
what's the content of *p (with *) if called somewhere? Is it the address of the structure or what?
Here's an example of the usage of *p - that is, dereferencing the pointer:
#include <stdio.h>
#include <stdlib.h>
struct something {
int x;
float y;
};
int main(void) {
struct something *p;
p = malloc(sizeof *p);
p->x = 2;
p->y = 5.6;
struct something s;
s = *p; // dereference p and copy into s
free(p);
// now check s:
printf("%d, %.1f\n", s.x, s.y); // prints 2, 5.6
}
p is a pointer to struct something. *p will dereference that pointer to give the struct itself. But, here is the catch: struct (*p) is a composite data type, you need to use a . operator to get its member.
(*p).x = 2;
(*p).y = 5.6;
This can also be done without using the indirection operator (*) (as you did):
p->x = 2;
p->y = 5.6;

Assigning int array to a int pointer

I want to assign value of an array to a pointer or need a better way to do the below operation .
I have a structure
struct ver{
int *a;
int *b
int *c;
}
struct Versions{
int ver1[3];
int ver2[3];
int ver3[9];
}
static const Versions versionsinfo[] {
"001234",
{0,18,0},
"000000"
};
static Temp_Data;
Versions * GetVersions() {
Versions * pt = NULL;
memcpy(&Temp_Data,&versionsinfo[1]);
pt = &Temp_Data;
return pt ;
}
struct Versions *pointer;
pointer = GetVersions();
struct ver *newVer;
newVer->a= pointer->ver1;
newVer->b= pointer->ver2;
newVer->c= pointer->ver3;
I want to assign the value of Ver1 to a member of struct ver , either a, b or c.
can anyone please let me know if this is possible in C.
Hope this helps
struct ver
{
int *a;
int *b;
int *c;
};
int Ver1[3] = {1,2,3};
int Ver2[3] = {1,2,3};;
int Ver3[9];
struct ver *newVer;
int main()
{
struct ver newV;/*the memory for struct var is allocated in main */
newVer = (struct ver *)malloc(sizeof(struct ver));/*memory allocated in heap*/
newV.a = Ver1;/*OR newVar.a = &Ver1[0];*/
newV.b = Ver2;
newV.c = Ver3;
printf("newV->a[0] is %d", newV.a[0]);
/*OR*/
newVer->a = Ver1;
newVer->b = Ver2;
newVer->c = Ver3;
printf("\nnewVar->a[1] is %d", newVer->a[1]);
free(newVer);
return 0;
}
Well,
int Ver1[3];
int Ver2[9];
int Ver3[9];
They are initializing arrays of type int. So if you want to get those numbers (which are the sizes of the arrays in the above) you need to do
int Ver1 = 3;
int Ver2 = 9;
int Ver3 = 9;
The allocate some memory for the pointer
struct ver *newVer = malloc(sizeof(newVer));
and then put the values in the struct
newVer[0].a = Ver1;
newVer[0].b = Ver2;
newVer[0].c = Ver3;

type casting void pointer to struct type

How do I type cast a void pointer to a struct array? Here is my code:
typedef struct{
int a;
double b;
} myStruct;
void Func1(void * Array1);
int main(){
myStruct S1[5];
S1[0].a = 1;
S1[0].b = 2.3;
S1[1].a = 2;
S1[1].b = 3.4;
Func1(S1);
return 0;
}
void Func1(void * Array1){
myStruct S2[5];
S2[0] = (myStruct *)Array1[0];
}
I get compile errors in Func1 for assigning S2[0]. How do I typecast the Array1 correctly?
[] operator has a higher precedence that a (cast) operator. Thus you have to use additional parenthesis:
S2[0] = ((myStruct *)Array1)[0];
or use a pointer:
myStruct* a = Array1 ;
S2[0] = a[0];

How to malloc "MyDef ** t" to a specific length, instead of "MyDef * t[5]" in C

A struct like the following works fine, I can use t after calling malloc(sizeof(mystruct)):
struct mystruct {
MyDef *t[5];
};
I want to be able to dynamically set the length of the array of MyDef, like the following:
struct mystruct {
MyDef **t;
int size;
};
What do I need to do additionally to malloc(sizeof(mystruct)) to get this to work, so I can do TestStruct->t[3] = something? Just getting a segmentation fault!
Thanks!
EDIT with code that causes seg fault, unless I'm blind this seems to be what the answers are so far:
#include <stdio.h>
typedef struct mydef {
int t;
int y;
int k;
} MyDef;
typedef struct mystruct {
MyDef **t;
int size;
} MyStruct;
int main(){
MyStruct *m;
if (m = (MyStruct *)malloc(sizeof(MyStruct)) == NULL)
return 0;
m->size = 11; //seg fault
if (m->t = malloc(m->size * sizeof(*m->t)) == NULL)
return 0;
return 0;
}
struct mystruct *s = malloc(sizeof(*s));
s->size = 5;
s->t = malloc(sizeof(*s->t) * s->size);
m = (MyStruct*)malloc(sizeof(MyStruct)) == NULL
What that does. Calls malloc, compares return of malloc to NULL. Then assigns the result of that comparison(a boolean value) to m.
The reason it does that is because '==' has a higher precedence than '='.
What you want:
if ( (m = (MyStruct *)malloc(sizeof(MyStruct))) == NULL)
...
if ( (m->t = malloc(m->size * sizeof(*m->t))) == NULL)
That happens because you do not allocate memory for array itself, only for pointer to this array.
So, first you have to allocate mystruct:
struct_instance = malloc(sizeof(mystruct));
and then you have to allocate memory for array of pointers to MyDef and initialize pointer in your struct
struct_instance->size = 123;
struct_instance->t = malloc(sizeof(MyDef*) * struct_instance->size);

Resources