Pointer to struct in C, what is the content? - c

I was wondering if I have a code like this:
struct something{
int x;
float y;
};
int main(void)
{
struct something *p;
p = malloc(sizeof(struct something));
p->x = 2;
p->y = 5.6;
return 0;
}
what's the content of *p (with *) if called somewhere? Is it the address of the structure or what?

Here's an example of the usage of *p - that is, dereferencing the pointer:
#include <stdio.h>
#include <stdlib.h>
struct something {
int x;
float y;
};
int main(void) {
struct something *p;
p = malloc(sizeof *p);
p->x = 2;
p->y = 5.6;
struct something s;
s = *p; // dereference p and copy into s
free(p);
// now check s:
printf("%d, %.1f\n", s.x, s.y); // prints 2, 5.6
}

p is a pointer to struct something. *p will dereference that pointer to give the struct itself. But, here is the catch: struct (*p) is a composite data type, you need to use a . operator to get its member.
(*p).x = 2;
(*p).y = 5.6;
This can also be done without using the indirection operator (*) (as you did):
p->x = 2;
p->y = 5.6;

Related

Pointer to pointer member Struct

Its a part of code that i working on. When i try to assign Diagcb.end to ptr2 it returned an error.
I tried to write it as:
ptr2 = &Diagcb.end;
ptr2 = &diagcb->end;
but the same error is returned.
typedef struct reportType {
int a;
int b;
int c;
int* ptr;
} reportType;
typedef struct cb {
reportType* start;
reportType* end;
reportType arr[10];
}cb;
int fun (reportType* report);
main()
{
cb Diagcb;
reportType report;
report.a = 0;
report.b = 1;
report.c = 2;
report.ptr = NULL;
reportType* ptr2;
ptr2 = cb.end;
}
You should write it as ptr2 = Diagcb.end. Because Diagcd.end is already a pointer, you don't need to use & to get the address.

Allocate record with variable at negative offset

When allocating memory for a pointer to a record I also need space for an integer pointer located just before the allocated record. This pointer cannot be part of the record itself and it cannot be placed after the record. My current approach is the following:
#include <stdlib.h>
static int n;
struct { int f; } *p;
p = malloc(sizeof (int *) + sizeof *p);
if (p != NULL) {
p = (void *) ((int **) p + 1);
*((int **) p - 1) = &n;
}
Are the casts well defined? If not, what should I do instead?
Edit:
What I'm trying to achieve is to implement extensible records (OOP) and the integer pointer represent a type ID. An extended record should be compatible with its base type. However, I only need type ID:s for pointer to records. Here is a complete example:
#include <assert.h>
#include <stdlib.h>
struct T0 {
int f;
};
int T0ID;
struct T1 {
struct T0 base;
int g;
};
int T1ID;
int main(void)
{
struct T0 *x;
struct T1 *y;
y = malloc(sizeof (int *) + sizeof *y);
if (y != NULL) {
*((int **) y) = &T1ID;
y = (void *) ((int **) y + 1);
((struct T0 *) y)->f = 1;
y->g = 2;
}
x = (struct T0 *) y;
assert(x->f == 1);
return 0;
}
I'm not sure your approach is good. Especially I'm worried about changing the value of p. You'll need that value later when you need to free the memory.
I would wrap the int* and the struct together in another struct. Something like:
static int n;
struct someData { int f; };
struct wrapper {int* pn; struct someData data;};
struct someData* pd; // Pointer to the data struct
struct wrapper* pw = malloc(sizeof *pw);
if (pw != NULL) {
pw->pn = &n;
pd = &pw->data;
}
Your code, in which you cast (arbitrary) memory addresses to particular object types, is might yield undefined behaviour due to incorrect alignment (cf.
C standard draft):
6.3.2.3 Pointers
(7) A pointer to an object type may be converted to a pointer to a
different object type. If the resulting pointer is not correctly
aligned for the referenced type, the behavior is undefined.
You are having a pointer declared to be of type struct{int}* pointing to a (larger memory block), and then casting the pointer and doing arithmetic operations on the (casted) pointer. As it is not guaranteed that int * and struct{int}* are aligned the same way, it is not guaranteed that the behaviour is defined.
To avoid this, encapsulate your structure and the preceeding integer in another structure, e.g. as follows:
static int n;
struct data_struct {
int f;
};
struct enclosing_struct {
int *nPtr;
struct data_struct data;
};
int main() {
struct enclosing_struct *e = malloc (sizeof(struct enclosing_struct));
e->nPtr = &n;
struct data_struct *dataPtr = &e->data;
return 0;
}

Write a Struct to Binary File in C [duplicate]

I am having problems with this program. It's very simply. I need to assign values to my struct from the pointers I created, but I keep getting a segmentation fault. Any ideas what I'm doing wrong:
#include <stdio.h>
#include <stdlib.h>
struct problem37
{
int a;
int b;
int c;
};
int main()
{
printf("Problem 37\n");
//create struct
struct problem37 myStruct;
//create the pointer
int* p;
int* q;
int* r;
*p = 1;
*q = 5;
*r = 8;
//read the data into the struct using the pointers
myStruct.a = *p;
myStruct.b = *q;
myStruct.c = *r;
printf("%d\n", myStruct.a);
printf("%d\n", myStruct.b);
printf("%d\n", myStruct.c);
return 0;
}
Your are assigning a value to *p, *q and *r, but they are not initialized: they're pointers pointing to random memory.
You need to initialize them, either assigning them a new value allocated in the heap (with malloc):
int *p = (int*) malloc( sizeof(int) );
*p = 1;
or making them point to an already existent value:
int x;
int *p = &x;
*p = 1; // it's like doing x=1
Your problem is that you write at random memory locations since you do not initialize your pointers nor allocate memory.
You could do the following:
int* p = malloc(sizeof(int));
int* q = malloc(sizeof(int));
int* r = malloc(sizeof(int));
Obviously you need to free them when you are done using them:
free(p);
free(q);
free(r);
You're not allocating memory to the pointer. Therefore when you're doing *p and *q and *r you're dereferencing a null pointer (or a random pointer). This leads to a segmentation fault. Use p = malloc(sizeof(int)); when you declare the variables.

C Pointer for Struct - Segmentation fault

I am having problems with this program. It's very simply. I need to assign values to my struct from the pointers I created, but I keep getting a segmentation fault. Any ideas what I'm doing wrong:
#include <stdio.h>
#include <stdlib.h>
struct problem37
{
int a;
int b;
int c;
};
int main()
{
printf("Problem 37\n");
//create struct
struct problem37 myStruct;
//create the pointer
int* p;
int* q;
int* r;
*p = 1;
*q = 5;
*r = 8;
//read the data into the struct using the pointers
myStruct.a = *p;
myStruct.b = *q;
myStruct.c = *r;
printf("%d\n", myStruct.a);
printf("%d\n", myStruct.b);
printf("%d\n", myStruct.c);
return 0;
}
Your are assigning a value to *p, *q and *r, but they are not initialized: they're pointers pointing to random memory.
You need to initialize them, either assigning them a new value allocated in the heap (with malloc):
int *p = (int*) malloc( sizeof(int) );
*p = 1;
or making them point to an already existent value:
int x;
int *p = &x;
*p = 1; // it's like doing x=1
Your problem is that you write at random memory locations since you do not initialize your pointers nor allocate memory.
You could do the following:
int* p = malloc(sizeof(int));
int* q = malloc(sizeof(int));
int* r = malloc(sizeof(int));
Obviously you need to free them when you are done using them:
free(p);
free(q);
free(r);
You're not allocating memory to the pointer. Therefore when you're doing *p and *q and *r you're dereferencing a null pointer (or a random pointer). This leads to a segmentation fault. Use p = malloc(sizeof(int)); when you declare the variables.

How to initialize const members of structs on the heap

I would like to allocate a structure on the heap, initialize it and return a pointer to it from a function. I'm wondering if there's a way for me to initialize const members of a struct in this scenario:
#include <stdlib.h>
typedef struct {
const int x;
const int y;
} ImmutablePoint;
ImmutablePoint * make_immutable_point(int x, int y)
{
ImmutablePoint *p = (ImmutablePoint *)malloc(sizeof(ImmutablePoint));
if (p == NULL) abort();
// How to initialize members x and y?
return p;
}
Should I conclude from this that it is impossible to allocate and initialize a struct on the heap which contains const members?
Like so:
ImmutablePoint *make_immutable_point(int x, int y)
{
ImmutablePoint init = { .x = x, .y = y };
ImmutablePoint *p = malloc(sizeof *p);
if (p == NULL) abort();
memcpy(p, &init, sizeof *p);
return p;
}
(Note that unlike C++, there is no need to cast the return value of malloc in C, and it is often considered bad form because it can hide other errors).
If this is C and not C++, I see no solution other than to subvert the type system.
ImmutablePoint * make_immutable_point(int x, int y)
{
ImmutablePoint *p = malloc(sizeof(ImmutablePoint));
if (p == NULL) abort();
// this
ImmutablePoint temp = {x, y};
memcpy(p, &temp, sizeof(temp));
// or this
*(int*)&p->x = x;
*(int*)&p->y = y;
return p;
}
If you insist on keeping the const in the structure, you are going to have to do some casting to get around that:
int *cheat_x = (int *)&p->x;
*cheat_x = 3;
I like caf's approach, but this occured to me too
ImmutablePoint* newImmutablePoint(int x, int y){
struct unconstpoint {
int x;
int y;
} *p = malloc(sizeof(struct unconstpoint));
if (p) { // guard against malloc failure
*p.x = x;
*p.y = y;
}
return (ImmutablePoint*)p;
}

Resources