I have a structure:
struct mystruct
{
int* pointer;
};
structure mystruct* struct_inst;
Now I want to change the value pointed to by struct_inst->pointer. How can I do that?
EDIT
I didn't write it, but pointer already points to an area of memory allocated with malloc.
As with any pointer. To change the address it points to:
struct_inst->pointer = &var;
To change the value at the address to which it points:
*(struct_inst->pointer) = var;
You are creating a pointer of type mystruct, I think perhaps you didn't want a pointer:
int x;
struct mystruct mystruct_inst;
mystruct_inst.pointer = &x;
*mystruct_inst.pointer = 33;
Of if you need a mystruct pointer on the heap instead:
int x;
struct mystruct *mystruct_inst = malloc(sizeof(struct mystruct));
mystruct_inst->pointer = malloc(sizeof(int));
*(mystruct_inst->pointer) = 33;
/*Sometime later*/
free(mystruct_inst->pointer);
free(mystruct_inst);
Related
My understanding is that the -> operator is shorthand for dereferencing a pointer to a struct, and accessing the value of one struct member.
struct point {
int x;
int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point; /* p is a pointer to my_point */
(*p).x = 8; /* set the first member of the struct */
p->x = 8; /* equivalent method to set the first member of the struct */
So the last 2 lines of the example above are equivalent. But I've encountered some code similar to this:
*p->x = 8
Using both the asterisk and arrow together. What does this do? Would this try to "double dereference" the pointer and assign to memory address 8, or something else? Maybe undefined behavior, or just a compiler error?
*p->x is equivalent to *(p->x) - you are dereferencing the result of p->x, which implies the x member itself has pointer type. Given a line like
*p->x = 8;
that implies x has type int *:
struct foo {
...
int *x;
...
};
Note that x must be assigned a valid pointer value before you can assign to *x. You can allocate memory dynamically:
struct foo *p = malloc( sizeof *p ); // dynamically allocate space for 1 instance of struct foo
if ( !p )
// handle allocation failure and exit here.
p->x = malloc( sizeof *p->x ); // dynamically allocate space for 1 int.
if ( !p->x )
// handle allocation failure and exit here.
Or you can set x to point to an existing int object:
int a;
...
p->x = &a;
*p->x = 8;
For a structure
struct tagp
{
int *x=someaddress;
}p0;
struct tagp *p=&p0;
*p->x accesses the address stored in the pointer x inside the structure. It is same as *((*p).x) and *(p0.x) which accesses the memory at Someaddress.
Check this link
I would like to know, how to access first member of struct through pointer. I have tried this:
#include <stdio.h>
#include <stdlib.h>
struct foo
{
int a;
char *str;
};
int main()
{
struct foo *p = malloc(sizeof(struct foo));
int val = 10;
*(int**)p = &val; //set the value of the first member of struct foo
printf("%i\n",p->a);
}
but that print some garbage. How can I set it in similar manner?
What you're doing there is setting the value of p->a to the address of val (possibly truncated to 32 bits, depending on the implementation).
To access the field via a pointer, you have to take the field's address and then assign to the dereferenced pointer.
int *pa = &p->a;
*pa = val;
Of course you can also just write p->a = val. Note that p->a is just an abbreviation for (*p).a.
The assignment should be:
*(int*)p = val;
You want to assign to an int member, so the pointer has to be a pointer to int, not pointer to pointer to int. And the value being assigned must be int; &val is a pointer to int.
I've been learning structs and I've come to the pointers to structs, where I'm currently struggling with this.
I've got this piece of code:
struct point {
int x;
int y;
} *ptr;
ptr->x = 8;
ptr->y = 8;
Running this gives a segmentation error. What I want to do is assign the value of 8 to x /y, to which, as far as I understand it, ptr is pointing to.
Your ptr is a pointer to struct point.
However, there is no struct point it is pointing to.
struct point {
int x;
int y;
} *ptr;
struct point pt; // add this...
ptr = &pt; // ...and this.
ptr->x = 8;
ptr->y = 8;
You have created a pointer called ptr but not initialized it. When you dereference it with ptr-> (or *ptr), you invoke undefined behavior which in your case is crashing the program (but could do anything).
This would be better:
struct point {
int x;
int y;
};
struct point sp = {0,0};
struct point *ptr = &sp;
sp.x = 8;
ptr->y = 8;
The problem is that the pointer ptr does not point to any valid memory buffer, assign it to a allocated or automatic storage instead:
// way 1
struct point p;
ptr = &p;
ptr->x = 8;
ptr->y = 8;
// way 2
ptr = malloc(sizeof *ptr);
ptr->x = 8;
ptr->y = 8;
// when you are done remember to release the allocated memory
free(ptr);
Let me explain you the simple way.
1.
Variables hold the data(values to want them to hold) and pointers hold only the memory address of the variables(which are just a section of memory). Pointers are used to just hold the address of the variables so they cannot hold any user data. We can although create a pointer to a variable and manipulate that variable using the specific pointer. We first need to create the variable, then we create a pointer which references that variable, then we do anything with the pointer and the variable will be manipulated.
struct point
{
int x;
int y;
};
struct point var1; //declare a variable
struct point *ptr=&var1;// declare a pointer to that variable
ptr->x = 8; // manipulate the variable
ptr->y = 8;
Or
2.
If you insist on using pointers only, then you need to allocate memory dynamically and then assign the base address of the allocated memory to the pointer.
struct point
{
int x;
int y;
} *ptr;
ptr=(struct point*)malloc(sizeof(struct point)); // this will allocate memory required to hold your structure
ptr->x = 8; //manipulate the memory content pointed by your pointer.
ptr->y = 8;
You need to assign a valid memory location to ptr, which is a pointer to struct point. You could either use malloc, or declare a variable with struct point and assign its address to ptr (like the above answers).
Btw, if ptr has file scope, then it is initialized with zero, i.e., the NULL pointer; otherwise, evaluating ptr is undefined behavior (you could get any value). In both cases, trying to dereference ptr is undefined behavior and thus you could get the segfault (in some implementation you might modified some memory location unintendedly).
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);
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))