Is there a way to get a pointer to an anonymous struct? With out anonymous structs I could write the following:
struct a{
int z;
};
struct b{
int y;
struct a *x;
}
This works fine, but I only use struct a within struct b and it seems redundant to pollute the global namespace with it. Is there a way I could define a pointer (x) to an anonymous struct. Something that would probably look like the following:
struct b{
int y;
struct {
int z;
} *x;
}
Or is this valid on its own?
Yes you can do this. But there is a complication: there is no way to directly declare another pointer to same type - or an object of that type, because... the struct type is anonymous.
It is still possible to use it however, by allocating memory for it with malloc, as conversions from void * to any pointer to object are possible without an explicit cast:
struct b {
int y;
struct {
int z;
} *x;
} y;
y.x = malloc(sizeof *y.x * 5);
Why would you think that this is better than polluting the namespace is beyond my imagination.
GCC provides the typeof so you can increase insanity by things like
typeof(y.x) foo;
or even declare a structure of that type
struct b y;
typeof(y.x[0]) foo;
foo.z = 42;
y.x = &foo;
I have a C program where I have two structures
struct one{
int a;
int b;
char *s;
int c;
};
struct two{
int a;
int e;
char *s;
int d;
};
Is possible to write a function that copies the values of the variables with the same type and name from struct one to struct two?
For example, in this case the function should do this
two.a = one.a;
two.s = one.s;
There's no way to automatically grab fields of a given name from a struct. While you could do something like this in Java with reflection, it can't be done in C. You just need to manually copy the relevant members.
You may write function macro like this:
#define CAT(A, B) A ## B
#define ARGS_ASSIGN(N, L, R, ...) CAT(_ARGS_ASSIGN, N) (L, R, __VA_ARGS__)
#define _ARGS_ASSIGN1(L, R, M0) L.M0 = R.M0;
#define _ARGS_ASSIGN2(L, R, M0, M1) L.M0 = R.M0; L.M1 = R.M1;
/* ... define sufficiently more */
and use in such way:
ARGS_ASSIGN(2, two, one, a, s)
In theory you could do this with a simple block copy function for your example above using the code below if you are certain that your compiler sequences the structure as sequenced in its type definition. However, I don't think it's a great idea. Block copy would be safer with two data structures of the same type as defined in one of the answers proposed above.
Example using block copy function:
void main(void)
{
struct one{
int a;
int b;
char *s;
int c;
};
struct two{
int a;
int e;
char *s;
int d;
};
// Place code that assigns variable one here
memcpy(&two, &one, sizeof(one));
}
So I've got an assignment that I'm having trouble with. I'm trying use pthreads to sum the elements of a matrix with 3 different processors. I have a struct
typedef struct{
int rows;
int cols;
pid;
int localsum;
}ThreadData;
some global variabls
int processors=3;
int rows=4;
int cols=4;
int matrix[10][10];
and a sum function
void *matrixSum(void *p){
//cast *a to struct ThreadData?
int sum=0;
int i=p->pid;
int size=p->rows*p->cols;
//to sequentially add a processor's 'owned' cells
int row=p-pid/p-cols;
int col=p-pid%p->cols;
int max_partition_size = ((size/processors)+1);
for(i;i<max_partition_size*processors;i+=processors){
col=i%p->cols;
row=i/p->cols;
if(i<=size-1){
sum+=matrix[row][col]+1;
}
}
p->localsum=sum;
}
so my main method looks like this:
int main(){
int totalsum=0;
ThreadData *a;
a=malloc(processors*(sizeof(ThreadData));
int i;
for(i=0;i<processors;i++){
a[i].rows=rows;
a[i].cols=cols;
a[i].pid=i;
a[i].localsum=0;
}
//just a function that iterates over the matrix to assign it some contents
fillmatrix(rows, cols);
pthread_t tid[processors];
for(i=0;i<processors;i++){
pthread_create(tid,NULL,matrixSum,(void *)&a);
totalsum+=a[i].localsum;
}
pthread_join();
}
My ultimate goal is to pass my matrixSum() with a ThreadData struct as the argument.
So I think I have to cast the void pointer given in matrixSum() to a struct, but I'm having trouble doing so.
I tried doing so like this
ThreadData *a=malloc(sizeof(ThreadData));
a=(struct ThreadData*)p;
But I get a warning: assignment from incompatible pointer type error.
So what's the proper way to do this - that is to cast the void pointer taken from the parameters, and operate on it like the struct it is meant to be?
Try using a=(ThreadData*)p;.
In C language, struct ThreadData is differ to ThreadData.
In this case, you used typedef and defined no tag to the struct, so you mustn't use struct to use the struct.
I have 2 headers that need to define 2 similar structures. One structure should be aligned to 32bytes and other is similar but doesn't need to align to 32bytes so I can save some memory here. One is included in the other. Here is main_header.h
struct mainStruct
{
int a;
int b;
int c;
char pad[20];
};
Following is sub_header.h
#include "main_header.h"
struct subStruct
{
int a;
int b;
int c;
};
Now what I wish to do is define subStruct in a way that, whenever i change mainStruct other than pad[20] all other fields should be updated in subStruct. That is, if tomorrow, I make mainStruct like this:
struct mainStruct
{
int a;
int b;
int c;
int d;
char pad[16];
};
then, automatically it should be reflected in sub_header.h as
struct subStruct
{
int a;
int b;
int c;
int d;
};
Is there an efficient way of doing it using some kind of Macros or other preprocessor directives??
And subStruct should always be derived from mainStruct and not other way around, this is very important.
Thanks in advance.
Did you consider the other way around; including subStruct as an element in mainStruct? Like this:
struct sub {
int a, b, c;
};
struct main {
struct sub head;
char dummy[16];
};
True, it does take an extra layer of syntax to get at a, b or c, but the memory layout and performance should be the same.
Otherwise, there's nothing preventing you from using a macro, as you suggest yourself:
#define SUB_FIELDS int a; int b; int c;
struct main {
SUB_FIELDS
char dummy[16];
};
struct sub {
SUB_FIELDS
};
Well, that's why C++ was initially invented. But if you wish to stay with plain C (there could be reasons for such decision) you may check GLib / GObject type system implemented as a set of macros over plain C.
I have a question in initializing my structure array inside a structure array. for example if I have a code as below:
#include <stdio.h>
int main() {
typedef struct {
int a;
int b;
int c;
} FIRST_T;
typedef struct {
int x;
int y;
int z;
FIRST_T *p;
} SECOND_T;
FIRST_T p1[]={{1,2,3},{3,4,5},{6,7,8}};
FIRST_T p2[]={{4,5,6},{7,8,9}};
SECOND_T my_second[]=
{
{1,2,3,p1},
{4,5,6,p2}
};
}
if I have to initialize my first array in second array intialization part itself, then how would I write my typedef of SECOND_T?
like
SECOND_T my_second[]=
{
{1,2,3,{{1,2,3},{4,5,6},{7,8,9}}},
{4,5,6,{{1,1,1},{2,2,2}}}
};
then how should be my SECOND_T?
I am sure I can't define it as:
typedef struct {
int x;
int y;
int z;
FIRST_T p[]; //(I know its a blunder)
} SECOND_T;
Please help.
You can't define a type with unbounded array in C, you have to specify the dimension. So you either do:
typedef strut {
int x;
int y;
int z;
FIRST_T f[SOME_VALUE];
} SECOND_T;
and then initialize SOME_VALUE count of members always, or do it the way you did.
I don't think you can do this, i.e, initialize the FIRST_T inside the initialization of SECOND_T, other than the first way you do it. Think about it, how can the compiler tell how many FIRST_T are there in SECOND_T? The problem here is, you can NOT define an array of flexible size struct statically.
you can't to do like that:
SECOND_T my_second[]=
{
{1,2,3,{{1,2,3},{4,5,6},{7,8,9}}},
{4,5,6,{{1,1,1},{2,2,2}}}
};
with
typedef struct {
int x;
int y;
int z;
FIRST_T p[]; //(I know its a blunder)
} SECOND_T;
becase the compiler don't know how to malloc memory for this struct. if you must do like this, you should define
typedef struct { int x,y,z; FIRST_T p[CONST_VALUE];}SECOND_T;
but I advise you use the first style that you write.
I don't know if I completely capture the question that you have. If it is that you want to avoid to have to declare an auxiliary variable for the pointers in the structure you can use a compound literal as follows:
SECOND_T my_second[] = {
{1,2,3, &(FIRST_T){{1,2,3},{3,4,5},{6,7,8}}},
{4,5,6, &(FIRST_T){{4,5,6},{7,8,9}}}
};
You'd have a compiler that complies to C99 for that.