Is this use of struct pointers correct? - c

I have this struct:
typedef struct Grades {
int grade1;
int grade2;
int grade3;
int grade4;
int grade5;
}
I created a pointer to a Grades struct using
struct Grades *pointer;
and I have a function() that returns a (void *) pointer to a specific Grades struct.
How do I set my pointer to that specific struct using the (void *) pointer?
I was thinking:
pointer = &function();
but that gives me an error: "'&' requires l-value
Any ideas? And by the way, I can't modify the function so...

If function() returns a pointer, you should be able to just do
pointer = function();

pointer = function();
If function() is returning a void pointer, you don't need to take the address of it, it's already a pointer pointing to a Grades struct.

Related

How to solve the error: assignment to expression with array type

I am asked to create a carinfo structure and a createcarinfo() function in order to make a database. But when trying to allocate memory for the arrays of the brand and model of the car, the terminal points out two errors.
For:
newCar->brand =(char*)malloc(sizeof(char)*(strlen(brand) + 1));
newCar->model = (char*)malloc(sizeof(char)*(strlen(model) + 1));
it says that there is an error: assignment to expression with array type and an arrow pointing to the equal sign.
struct carinfo_t {
char brand[40];
char model[40];
int year;
float value;
};
struct carinfo_t *createCarinfo(char *brand, char *model, int year, float value){
struct carinfo_t *newCar;
newCar=(struct carinfo_t*)malloc( sizeof( struct carinfo_t ) );
if (newCar){
newCar->brand =(char*)malloc(sizeof(char)*(strlen(brand) + 1));
newCar->model = (char*)malloc(sizeof(char)*(strlen(model) + 1));
strcpy(newCar->brand, brand);
strcpy(newCar->model, model);
//newCar->brand=brand;
//newCar->model=model;
newCar->year=year;
newCar->value=value;
}
return newCar;
};
Two things.
In your code, brand and model are already of array type, they have memory allocated to them based on their size (char [40]) on declaration. You need not allocate any memory using the allocator function (unlike pointers, on other hand).
You cannot assign to an array type. Array types are not suitable for a LHS argument for an assignment operator.This is what basically throws the error you see, but if you adhere to #1, you'll never reach here.
You are declaring the fixed size arrays in the struct.
Maybe you want to do this:
struct carinfo_t {
char *brand;
char *model;
int year;
float value;
};
It seems you may have mistyped the definition of your struct.
Maybe change it to something like this:
struct carinfo_t {
char* brand;
char* model;
int year;
float value;
};
The reason you are getting that error is because you are trying to assign the pointer returned by malloc to an array. That won't work. You should instead be storing the pointer returned by malloc in a char*.

Add struct to struct array using void pointer to said array in C

Let's say I have the following struct and array of that struct:
struct Fileinfo {
int ascii[128]; //space to store counts for each ASCII character.
int lnlen; //the longest line’s length
int lnno; //the longest line’s line number.
char* filename; //the file corresponding to the struct.
};
struct Analysis fileinfo_space[8]; //space for info about 8 files
I want to have a function that will add a new struct to this array. It must take a void pointer to the position where to store the struct as an argument
int addentry(void* storagespace){
*(struct Fileinfo *)res = ??? //cast the pointer to struct pointer and put an empty struct there
(struct Fileinfo *)res->lnlen = 1; //change the lnlen value of the struct to 1
}
My questions are:
What goes in place of ??? I tried (Fileinfo){NULL,0,0,NULL} as per this Stackoverflow response. But I get `error: ‘Fileinfo’ undeclared (first use in this function)
How do I create a void pointer to the array? Is (void *)fileinfo_space correct?
I am required to use void * as the argument for the function for this assignment. It's not up to me.
Let's say you have some memory block passed as storagespace void pointer:
You have to define a constant to be able to initialize (unless you're using c++11), let's call it init. BTW your assignment value is wrong: first member is an array of int. You cannot pass NULL to it. Just zero-fill it like show below.
Then cast your void pointer into a pointer on your struct, then initialize by copying the init struct, modify at will...
int addentry(void* storagespace){
static const struct Fileinfo init = {{0},0,0,NULL};
struct Fileinfo *fi = (struct Fileinfo *)storagespace;
*fi = init; //cast the pointer to struct pointer and put an empty struct there
fi->lnlen = 1; //change the lnlen value of the struct to 1
}

Change address of struct in C

Let's say that I was given a struct and I need to assign all of it's attributes to a particular address. The code below is giving me a conditional error, but i'm not trying to evaluate it.
struct header block_o_data;
block_o_data.a = 1;
block_o_data.b = 2;
void* startingAddress = sbrk(0);
&block_o_data = *address;
Please let me know what im doing wrong.
In the assignment to block_o_data, you're taking its address and trying to assign a value to it. The address of a variable is not an lvalue, meaning the expression cannot appear on the left side of an assignment.
You need to declare a pointer to a struct, then assign it the address of where the values actually live:
struct header *block_o_data;
void* startingAddress = sbrk(0);
block_o_data = startingAddress;
Suppose you have a struct like this:
struct mystruct {
int a;
char b;
};
then you probably need something like this:
// A pointer variable supposed to point to an instance of the struct
struct mystruct *pointer;
// This is a general address represented by void*
void *addr = some_function(0);
// Cast that general address to a pointer varibale pointing to
// an instance of the struct
pointer = (struct mystruct *) addr;
// Use it!
printf("%d", pointer->a);

Initializing a pointer variable of the type structure

Consider a pointer variable as defined below
struct socket_info
{
int hsocket;
int * buffer;
}
typedef struct socket_info * t_socket_info;
Now we I want to declare the pointer variable and use it as follow
t_socket_info t_socket;
How to allocate the memory for t_socket pointer variable
In C, you would do
t_socket = malloc(sizeof(struct socket_info));
or
t_socket = malloc(sizeof *t_socket);
t_socket=(socket_info *)malloc(sizeof(socket_info))

What's wrong in this allocation?

This the struct that I declared :-
struct page_table_entry {
struct addrspace* as;
vaddr_t va;
//page_state_t state;
int timestamp;
};
Now I want to dynamically allocate memory for an array of this. My implementation is here :-
struct page_table_entry **coremap = (struct page_table_entry**)
kmalloc(npages*sizeof(struct page_table_entry*));
int i;
for(i=0;i<npages;i++)
{
coremap[i] = (struct page_table_entry*)kmalloc(sizeof(struct page_table_entry));
coremap[i].va=(firstAddress+(i*PAGE_SIZE));
}
Its giving me an error on the last line where I am accesing the variable va. Error is:-
error: request for member `va' in something not a structure or union
You have an array of pointers to structs, not an array of structs.
In the line
coremap[i] = (struct page_table_entry*)kmalloc(sizeof(struct page_table_entry));
you cast your memory allocation to page_table_entry*, so coremap[i] is this pointer.
You access the actual struct via
coremap[i]->va=(firstAddress+(i*PAGE_SIZE));
coremap is a pointer to a pointer to a struct page_table_entry.
When you dereference it with coremap[i] you get a pointer to a struct page_table_entry.
You cannot use . on a pointer to a structure. You must use ->:
coremap[i]->va=(firstAddress+(i*PAGE_SIZE));
or
(*coremap[i]).va=(firstAddress+(i*PAGE_SIZE));
Aside from the obvious change to coremap[i]->va, you could change to an array of structs:
struct page_table_entry *coremap = (struct page_table_entry*)kmalloc(npages*sizeof(struct page_table_entry));
int i;
for(i=0;i<npages;i++)
{
coremap[i].va=(firstAddress+(i*PAGE_SIZE));
}

Resources