I'm trying to dynamically create an array of ints for grades that are inside of a structure but I have a small syntax error. Here is my code:
typedef struct
{
int Stud_ID;
int class_ID;
int* Grades;
int Grade_Cap;
int Count;
float Average;
}enrollment;
typedef struct
{
int Enrollment_Count;
int Enrollment_Cap;
enrollment *enrollment_list;
}enrollments;
void add_grade_space(enrollments *enroll)
{
enroll->enrollment_list[enroll->Enrollment_Count].Grade_Cap = malloc(sizeof(int)*2);
}
The error I get says: assignment makes integer from pointer without cast.
enroll->enrollment_list[enroll->Enrollment_Count].Grade_Cap = malloc(sizeof(int)*2);
You are trying to allocate memory to Grade_Cap which is not a pointer . It is an integer variable .You need to declare it as int * to allocate memory if you want to.
Related
I have the following code.
FlowNProcess f1[3];
int f1resources[2]={-1,0};
f1[1].resoures =f1resources;
typedef struct FlowNProcess
{
int id;
int tt;
int wt;
Requirement *requirement;
int *resoures;
char *state;
} FlowNProcess;
Here since the resources is an int* so, I am first creating an array of size 2 and then
assigning the pointer to it. Is there a better way to achieve the same, maybe a one-liner
Aim : To create a structure of element having certain properties. Then utilize that structure type by creating it's array in another structure.
struct Element
{
int i;
int j;
int x;
};
struct Sparse
{
int r;
int c;
int n;
struct Element *ele;
ele = (struct Element *)malloc(n*sizeof(struct Element));
};
What I wish to know is that which part of the code am I not allowed to write while creating a structure.
The common way to do this is:
struct Element
{
int i;
int j;
int x;
};
struct Sparse
{
int r;
int c;
int n;
struct Element ele[0]; // Make a zero length array
};
struct Sparse* MakeNewSparse(size_t num_ele)
{
struct Sparse* sparse = malloc(sizeof(*sparse) + num_ele*sizeof(struct Element));
return sparse;
}
This works because accessing off the end of a zero-length array is totally legal in C, provided you have allocated memory there.
In this example, we allocate enough space for the struct Sparse, and then enough more contiguous space for the array of struct Element.
After that, accessing element sparse->ele[5] is totally legal.
The line
ele = (struct Element *)malloc(n*sizeof(struct Element));
should not be part of the struct definition - that's something you do at runtime, along these lines:
struct Sparse s; // create new struct Sparse instance
s.n = get_some_size();
s.ele = malloc( s.n * sizeof *s.ele ); // no need for cast
struct in c is syntactically similar with types like int, char, etc. The definition of a struct is for compiler to know how to use variable declared with that struct such as struct Sparse var;. So the definition of a struct is not actually the code itself. It will be used at compile time.
However, malloc() is a function, which will be used at runtime, so it is nonsense to put malloc() in your struct definition.
(There was a similar question but the answer given doesn't work)
There is no warning. But after entering values, segmentation fault appears.
#include <stdio.h>
typedef struct
{
int a;
char b;
}PO;
PO *P[1000];
int main()
{
scanf("%d%c",&(P[0]->a),&(P[0]->b));
}
PO *P[1000];
defines an array of 1000 pointers to PO structures.
Written like that it would probably be clearer:
PO* P[1000]; // <-- P is an array of 1,000 pointers to PO
What you probably want is a preallocated array of structures, not pointers to those, e.g.:
PO Data[1000];
In this case, you can read data into it with code like this:
scanf("%d%c", &(Data[0].a), &(Data[0].b));
Or, if you still want an array of pointers, you should allocate each structure using malloc() and then free() it after use.
This code should work:
typedef struct
{
int a;
char b;
}PO_t;
int main(void)
{
unsigned int i=0;
PO_t PO[1000]; //create a 1000 struct of the type PO
PO_t* ptr[1000]; //create 1000 pointers to struct of type PO_t
for(i=0;i<1000;i++)
{
ptr[i]=&PO[i]; //or ptr[i]=&PO[0] if you want 1000 pointers to a single struct
}
for(i=0;i<1000;i++)
{
scanf("%d%c\n",&(PO[i].a),&(PO[i].b));
}
for(i=0;i<1000;i++)
{
printf("%d%c\n",ptr[i]->a),ptr[i]->b));
}
return 0;
}
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 am trying to assign an int pointer to an int array which is a member of a structre.
The structure is a member of another structure, which happens to be an array of structures. And this array of structures happens to be a member of another structure. And this last structure happens to be an element of an array of structures.
typedef struct s_ptxRowProperties
{
int lastPlotValue[134];
} ptxRowProperties;
typedef struct s_ptxRow
{
ptxRowProperties PtxRowProperties;
} ptxRow;
typedef struct s_workSpace
{
ptxRow PtxRow[100];
} workSpace;
Edit:
I allocate 1 of these behemoths like this:
WorkSpace[n] = (workSpace *) calloc(1, sizeof(workSpace));
I have tried the following incantations, to no avail:
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= (&WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue)[0];
int *x= WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;
int *x= *(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue);
int *x= (*WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue);
I believe the hypothetical million monkeys in a room for 100 years will have composed Hamlet before they can create the correct form for this. Any ideas?
You probably want
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0]);
This assumes that WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0] would reference the first element of the int array.
If WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue is the value you are interested in, than one would use & to get its address:
int *x = &WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;