I have a queue that stores thread structures
struct x
{
int n;
char *c;
void *f;
};
The function below returns a structure defined as the following:
struct s {
void *data;
};
When I call *new = allocate(&x_ptr), how can I access the members of x?
I tried the following but it does not work:
printf("%d\n", new ->data->n)
I get this error: request for member ānā in something not a structure or union
data has type of void *, you need to cast it to the correct pointer type (assuming it is referencing a valid object of that type).
printf("%d\n", node->data->threadid);
---->
printf("%d\n", ((threaddesc *)(node->data))->threadid);
Related
I defined a structure called coordonnees declared as a pointer, I want to append values to the pointer a_visiter but it's not working. How can I fix that?
Here is the struct code:
typedef struct couple
{
int ligne;
int colonne;
}*coordonnees;
int main()
{
coordonnees a_visiter;
a_visiter=(coordonnees)malloc(DIMENSION*4*sizeof(struct couple));
int p=0;
etc ...
}
void voisin_egaux(char matrice[][DIMENSION+1],int x, int y,coordonnees **a_visiter,int *p)
{
if (matrice[x][y]==matrice[x+1][y]){
(a_visiter+(*p))->ligne=x+1;
(a_visiter+(*p))->colonne=y;
++(*p);
}
I get as an error: request for member ligne in something not a structure or union.
This parameter declaration
coordonnees **a_visiter
is equivalent to
struct couple ***a_visiter
Thus the expression
(a_visiter+(*p))
has the above pointer type that does not point to an object of the structure type.
To make this code semantically valid
(a_visiter+(*p))->ligne=x+1;
(a_visiter+(*p))->colonne=y;
the parameter should be declared like
coordonnees a_visiter
Otherwise you need to write
( **a_visiter+(*p))->ligne=x+1;
( **a_visiter+(*p))->colonne=y;
I am trying to understand why the following code compiles and runs fine. I would expect any assignment using data inside f not to compile with a gcc error assignment of member āiā in read-only object. Is there some kind of exception, because data.i is allocated dynamically?
#include <stdio.h>
#include <stdlib.h>
struct a {
int *i;
};
void f(const struct a *data) {
data->i[0] = 55;
}
int main() {
struct a data;
data.i = malloc(2 * sizeof(int));
f(&data);
printf("%d\n", data.i[0]);
return 0;
}
const front of a struct will make it read-only. If the struct contains pointer members, then those pointers themselves will turn read-only. Not what they point at.
That is, const struct a will make the member i behave is if it was declared as int *const i;, meaning that the pointer itself cannot be changed to point elsewhere. The pointed-at data is still of read/write int though.
If you want to restrict access to i inside a function, you should make that function take a const int* parameter and pass the i member to that function.
In the below code, const indicates what data points to is not to be modified. data->i[0] = 55; does not modify the pointer data->i. Instead that line of code modifies the memory pointed to by data->i. This is allowed as pointer .i is int * and not const int *.
struct a {
int *i;
};
void f(const struct a *data) {
data->i[0] = 55;
}
You cant modify i but you can modify the objects referenced by i.
To prevent it you need to:
struct a {
const int *i;
};
I came across a piece of code that looks like this, where nodeptr and bodyptr are pointers to a struct, and type is a member of the struct.
#define Type(x) (((nodeptr) (x))->type)
What does it mean to have two pointers next to each other in brackets? I get that the -> notation gets the member of the struct, but am not sure about the first part of the line. I'm fairly new to C and am trying to get my head around pointers!
It's a cast.
In this part, ((nodeptr)(bodyptr)), The pointer bodyptr is casted as pointer of type nodeptr, then it accesses the member type of the structure pointed to by bodyptr.
I.e.
void *GetStructPtr(void); //The function returns a pointer to void
typedef struct //This is our structure
{
float a;
int type;
} node;
type def node *nodeptr; //This is our pointer type
void my func(void)
{
void *bodyptr; //Here we have a generic void pointer
bodyptr = GetStructPtr(); //Assign to it the vallue returned from function
//In the next line we cast our void* to a pointer to a structure node
//and then access its member type.
((nodeptr)bodyptr)->type = 0;
}
In your case it has been inserted in a macro to make it easier to use.
It is cast. The pointer bodyptr is being casted to nodeptr and then type member accessed. This means that instead of accessing type member directly from bodyptr it is first converted to pointer of type nodeptr and only then accessed. It is useful e.g. when first pointer is just a pointer to raw memory, of type void * and you want to treat this memory as given type, maybe some struct.
Example:
struct e {
int a;
double b;
};
struct e foo { 1, 2.0 };
void *pFoo = &foo; // p points at foo now
// I know p is now address of object of type struct e
// and I want to get it's 'a' element BUT I can't
// do p->a, p is of void* type, yet I can do
int a = ((struct e*)(pFoo))->a;
So I have this Linked List print function that is giving me the error:
error: invalid use of void expression
Here's the line that causes this error:
printf("[%d] -> ", *(link->pointer)); /*note: i tried to cast it to (int) but still same error. */
Here's my struct for the link; very straight forward:
typedef struct DLLNode {
void * pointer;
struct DLLNode *next;
struct DLLNode *previous;
} DLLNode;
I'm using my prepend function as so:
...
int j = 2;
prepend(&j, list);
...
so that it uses the pointer to variable two as the value the struct stores in new DLLNode as the pointer.
Could someone tell me why this is happening and what it means? Thanks for your time.
I suppose you are trying to print the value that link->pointer points to.
printf("[%d] -> ", var) expects to see either an integer in its argument list or something that can be converted to it (for example, a char).
link->pointer is of type void * and after dereferencing, it looks like a void type variable. Thus, compiler can't convert *(link->pointer) to int type. To tell it that you actually keep an integer value behind that void * pointer you need to explicitly convert link->pointer to a int * type, as Kaz pointed out in the comments and only then dereference it. I hope the following code demonstrates this more clearly:
DLLNode *list;
// initialization and other stuff
void *p = list->pointer;
int *p_i;
p_i = (int*)p; // explicit cast to int*
// print the integer value pointed to by the list->pointer
printf("[%d] -> ", *p_i);
I started feeling comfortable with C and then I ran into type casting. If I have the following defined in an *.h file
struct data {
int value;
char *label;
};
and this in another *.h file
# define TYPE void*
How do I cast the void pointer to the struct so that I can use a variable "TYPE val" that's passed into functions? For example, if I want to utilize the value that TYPE val points to, how do I cast it so that I can pass that value to another functions?
(struct data*)pointer
will cast a pointer to void to a pointer to struct data.
Typecasting void pointer to a struct can be done in following
void *vptr;
typedef struct data
{
/* members */
} tdata;
for this we can typecast to struct
lets say u want to send this vptr as structure variable to some function
then
void function (tdata *);
main ()
{
/* here is your function which needs structure pointer
type casting void pointer to struct */
function((tdata *) vptr);
}
Note: we can typecast void pointer to any type, thats the main purpose of void pointers.