This question already has answers here:
Access Item in Nested Structure in C
(2 answers)
Closed 6 years ago.
typedef struct _imat {
int **m_mat;
int rows, cols;
} intMat;
typedef struct _banker {
intMat A;
intMat M;
int *C;
int numRes;
int numProcs;
} banker;
int main(int argc, char* argv[])
{
banker *b,c;
b = &c;
matInit((*b).A,(*b).numProcs,(*b).numRes);
}
I am trying to access intMat A in _banker struct but getting error:
"expected ‘struct intMat *’ but argument is of type ‘intMat’ void matInit(intMat *mat,int rows, int cols){"
(*b).A is of type intMat
However matInit is expecting a intMat *
So replace (*b).A with &(*b).A, for the "&" will make it a pointer
Related
This question already has answers here:
How can I get/set a struct member by offset
(6 answers)
Closed 2 years ago.
I have struct in c
struct Book {
char title[50];
char author[50];
char subject[100];
int book_id;
};
struct Book * book;
I can access the integer book_id like book->book_id
But how can I access to book_id by offset? How can I calc (in c code) the offset of specific element in struct and access like book+X
#define offset(type, member) ((size_t)&(((type *)0) -> member))
#define ACCESS(object, type, offset) (type *)(((char *)&(object)) + (offset))
typedef struct
{
int a,b,c;
}t;
int main(void)
{
t s = {1,2,3};
printf("%zu\n", offset(t,b));
printf("%d\n", *ACCESS(s, int, offset(t,b)));
}
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
I'm not sure why the following is producing a segmentation fault. I've defined a structure and I'm trying to store a value to it.
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
void main( int argc, char *argv[] ) {
TEST_STRUCT *test;
test->sourceid = 5;
}
You declare a pointer to the type. You need to allocate memory for the pointer to point to:
#include <stdlib.h>
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
int main(int argc, char *argv[]) {
TEST_STRUCT *test;
test = malloc(sizeof(TEST_STRUCT));
if (test) {
test->sourceid = 5;
free(test);
}
return 0;
}
Alternatively, you could declare the variable on the stack:
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
int main(int argc, char *argv[]) {
TEST_STRUCT test;
test.sourceid = 5;
return 0;
}
test pointer is not pointing to any address(pointing some garbage) so it is hitting segV
TEST_STRUCT *test;
it is good practice to initialize NULL and before dereference it, check if (test != NULL) {}
then only dereference.
to solve this, first you need to create variable of TEST_STRUCT and assign address of it to test pointer or allocate memory using malloc/calloc and then try this
This question already has answers here:
Understanding typedefs for function pointers in C
(8 answers)
Closed 7 years ago.
typedef void (*Hello)(struct test1 *, test2 *, int a, int b, const int c *, int d);
In this case, I am confused by how to handle the struct as the argument.
I have written:
Hello p1;
(*p1)(....need some arguments to be added here);
Please kindly teach me how to complete this maybe sample code could help.
Thanks
Here is some code
struct point {
int x;
int y;
};
typedef void (*Hello)(struct point *p);
void resetPoint(struct point *p)
{
p->x = 10;
p->y = 0;
}
int main(void)
{
struct point dot;
Hello p1 = resetPoint;
p1(&dot);
printf("%d\n",dot.x);
return 0;
}
This question already has answers here:
Strange compiler warning C: warning: ‘struct’ declared inside parameter list
(6 answers)
Closed 7 years ago.
This is the first time I am working with structs and I am not sure what I am missing. The code below gives the following error on line 38:
conflicting types for ‘encrypt’
Here is the code:
#include<stdio.h>
#include<stdint.h>
void encrypt(struct bitfield24* , struct bitfield24*, struct bitfield24*);
struct bitfield24 {
uint32_t value : 24;
};
void main(){
struct bitfield24 key[4];
key[0].value = 0;
key[1].value = 1;
key[2].value = 2;
key[3].value = 3;
struct bitfield24 plain_text[2];
plain_text[0].value = 0;
plain_text[0].value = 1;
struct bitfield24 cipher_text[2];
cipher_text[0].value = 0;
cipher_text[1].value = 0;
struct bitfield24* pt = plain_text;
struct bitfield24* ct = cipher_text;
struct bitfield24* k = key;
encrypt(pt, ct, k); // line 30
printf("%x ,", ct[1].value);
printf("%x \n", ct[0].value);
}
/*
* Ecnryption Method
*/
void encrypt(struct bitfield24* pt, struct bitfield24* ct, struct bitfield24* k){ //line 38
// Encryption Algorithm
}
Apart from this, here are the warnings associated with the code. The same warning is being thrown for all the 3 arguments on line 30.
note: expected ‘struct bitfield24 *’ but argument is of type ‘struct bitfield24 *’ Please help me resolve this.
I can provide more information if need be.
There are two major issues with your code. First off all, you cannot declare a method prototype using a struct before you define the struct itself. The solution is to define the struct first:
#include<stdio.h>
#include<stdint.h>
struct bitfield24 {
uint32_t value : 24;
};
void encrypt(struct bitfield24* , struct bitfield24*, struct bitfield24*);
Secondly, your code will not compile because main does not return int. It must be defined like this:
int main(){
After this, your code compiles just fine.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Having Problems about how to access other structure membera with void pointer of other function??
typedef struct
{
char Buf[20];
char Str[20];
}Sample;
typedef struct
{
char Data[20];
int i;
} Test;
Void pointer structure
typedef struct
{
void *New;
int j;
} Datastruct;
int main()
{
//i am confused with first line
Datastruct->New = &Sample;
strcpy((( sample*)Datastruct->New )->Buf,"adam");
printf(" Datastruct->New->Buf");
Datastruct->New = &Test;
strcpy((( Test*)Datastruct->New)->Data,"Eve");
printf("Datastruct->New->Data");
return 0;
}
please let me know how to access members of other structures via void pointers
The compiler is also confused about first line; you can't take the address of a type. As for following void pointers, you've got the right idea: cast it to the type of pointer you wish to treat it as.
Here is a fixed version which actually compiles and works without errors:
#include <string.h>
#include <stdio.h>
typedef struct {
char Buf[20];
char Str[20];
} Sample;
typedef struct {
char Data[20];
int i;
} Test;
typedef struct {
void *New;
int j;
} Datastruct;
int main() {
Datastruct d;
Sample s;
d.New = &s;
strcpy(((Sample*)d.New )->Buf,"adam");
printf("Datastruct->New->Buf\n");
Test t;
d.New = &t;
strcpy(((Test*)d.New)->Data,"Eve");
printf("Datastruct->New->Data\n");
return 0;
}
In your original you were confusing -> with . and types (e.g. Datastruct) with variables of that type.