typedef pointer const weirdness - c

please consider the following code:
typedef struct Person* PersonRef;
struct Person {
int age;
};
const PersonRef person = NULL;
void changePerson(PersonRef newPerson) {
person = newPerson;
}
For some reason, the compiler is complaining about read-only value not assignable. But the const keyword should not make the pointer const. Any ideas?

Note that
typedef int* intptr;
const intptr x;
is not the same as:
const int* x;
intptr is pointer to int. const intptr is constant pointer to int, not pointer to constant int.
so, after a typedef pointer, i can't make it const to the content anymore?
There are some ugly ways, such as gcc's typeof macro:
typedef int* intptr;
intptr dummy;
const typeof(*dummy) *x;
but, as you see, it's pointless if you know the type behind intptr.

const PersonRef person = NULL;
is
struct Person*const person= NULL;
so you are consting the pointer and not the object.

While the problem is already solved by the answers above, I do miss the reason why...
So maybe as a rule of thumb:
The const always refers to it's predecessor token.
In case there is no such, it's "consting" it's successor token instead.
This rule can really help you out for declaring a pointer to const pointers or something equally neat.
Anyway, with this in mind, it should get clear why
struct Person *const person = NULL;
declares a const pointer to a mutable struct.
Think about it, your typedef "groups" the struct Person with the pointer token *.
So, for writing
const PersonRef person = NULL;
your compiler sees something like this (pseudo-code):
const [struct Person *]person = NULL;
As there's nothing to const's left, it deklares the token to it's right struct Person * constant.
Well I think, this is the reason why I don't like hiding pointers by typedefs, while I do like typedefs as such. What about writing
typedef struct Person { ... } Person;
const Person *person; /*< const person */
Person *const pointer; /*< const pointer to mutable person */
and it should be quite clear to compilers and humans, what you're up to.

Never hide pointers behind typedefs, it is really really bad practice and will only create bugs.
One such infamous bug is that a typedef:ed pointer type that is declared as const will be treated as a "constant pointer to non-constant data", rather than "a non-constant pointer to constant data" which is what one intuitively expects. This is what happens in your program.
Solution:
typedef struct
{
int age;
} Person;
const Person* person = NULL; // non-constant pointer to constant Person

you are getting and error
error: assignment of read-only variable ‘person’
on statement
person = newPerson;
because you have declared person as const so its value is only read only ....const value can not be changed
if you are going to change that vatiable then why you are kepping it const?
remove const keyword your code will works fine

As an addition to Piotr's (accepted) answer, it's possible to avoid GCC-specific typeof:
static_assert(std::is_same<const int *, std::add_const_t<std::remove_pointer_t<intptr>> *>::value, "not same via type_traits");
static_assert(std::is_same<const int *, std::remove_reference_t<decltype(std::as_const(*intptr()))>*>::value, "not same via decltype");
By changing foo_t<T> to foo<T>::type above and/or using boost's version, it's even possible to do this in C++98, though it's only pretty since C++11.
Alternatively, use two different typedefs, which also works for cases other than plain pointers. For example, consider every container's iterator and const_iterator typedefs.

Related

How do I get a const pointer to a const struct?

I like to create scenarios in my code where I declare a static global struct inside a .c file that everyone will share, it contains configuration stuff. Right underneath the declaration, I'll create a constant pointer to this struct and put this in the .h file so that everyone can get access to it.
Sometimes, within a .c file, I like to have a global pointer to the specific configuration that that .c file cares about, that way I don't have constantly keep referencing the global struct, because sometimes I'll get this configuration from a different source on different projects.
The issue that I have is that I can't define this "local global" pointer because the initializer element is not constant. Here is an example.
typedef struct
{
int Value;
} mystruct_t, *pmystruct_t;
static const mystruct_t GlobalStruct;
const pmystruct_t pGlobalStruct = &GlobalStruct;
const int *ValuePtr = &pGlobalStruct->Value;
int main()
{
*ValuePtr = 10;
return 0;
}
I tried reading on the const keywords for pointer in C, and I thought I understood it, but apparently it's still a mystery to me. The line of code that I tried, and may gotten me closer to that piece of code to compile is
const mystruct_t const *pGlobalStruct = &GlobalStruct;
However, it still doesn't compile because ValuePtr initializer element is not constant (the error I get).
The end goal here is to have ValuePtr be a constant, where no one can change where it is pointing to, but allow change the elements of the struct that it is pointing to.
EDIT: I want ValuePtr to use pGlobalStruct
The definition
const pmystruct_t pGlobalStruct = &GlobalStruct;
makes the variable (the pointer itself) pGlobalStruct constant, not the structure that it points to.
This is only one of the many flaws that hiding pointers behind type-aliases have.
And to make the definition of ValuePtr valid as well, then you need to make both the pointer variable as well as the structure it points to constant:
const mystruct_t * const pGlobalStruct = &GlobalStruct;
Now pGlobalStruct is a constant pointer to a constant mystruct_t object.
Do not hide pointers behind the typedefs.
If your pointer is referencing constant object you cant assign it with the value. You can only initialize the object.
const int x = 10; //initialization
cosnt int *ptr = &x;
How do I get a const pointer to a const struct?
All possible combinations below:
struct str
{
int x;
float b;
};
struct str a;
const struct str b;
const struct str *c;
struct str * const d;
const struct str * const e;
a - not const structure
b - const structure
c - not const pointer to const structure
d - const pointer to not const structure
e - const pointer to const structure
In your case
const mystruct_t * const pGlobalSttruct = &GlobalStruct;

Initializing a const array inside a struct

#define LENGTH 6
typedef char data_t[LENGTH];
struct foo {
const data_t data;
...
}
...
void bar(data_t data) {
printf("%.6s\n", data);
struct foo myfoo = {*data};
printf("%.6s\n", foo.data);
}
I'm trying to have this struct which holds directly the data I'm interested in, sizeof(foo) == 6+the rest, not sizeof(foo) == sizeof(void*)+the rest. However I can't find a way to initialize a struct of type foo with a data_t. I think maybe I could remove the const modifier from the field and use memcpy but I like the extra safety and clarity.
I don't get any compile errors but when I run the code I get
123456
1??
so the copy didn't work properly I think.
This is for an arduino (or similar device) so I'm trying to keep it to very portable code.
Is it just not possible ?
EDIT: removing the const modifier on the data_t field doesn't seem to help.
It is possible to do this, for some cost >=0.
typedef struct
{
char c[LENGTH];
} data_t; // this struct is freely copyable
struct foo
{
const data_t data; // but this data member is not
int what;
};
void foo (char* x) {
data_t d; // declare freely copyable struct instance
memcpy(d.c, x, sizeof(d.c)); // memcpy it
struct foo foo = { d, 42 }; // initialise struct instance with const member
...
};
Some compilers (e.g. clang) are even able to optimise away the redundant copying (from x to d.c and then from d to foo.data ⇒ from x straight to foo.data). Others (gcc I'm looking at you) don't seem to be able to achieve this.
If you pass around pointers to data_t rather than straight char pointers, you won't need this additional memcpy step. OTOH in order to access the char array inside foo you need another level of member access (.data.c instead of just .data; this has no runtime cost though).
It's impossible to do it in a standard compliant way.
Due to its being const, const char data[6]; must be initialized to be usable, and it may only be initialized statically (static objects with no initializer get automatically zeroed), with a string literal, or with a brace-enclosed initializer list. You cannot initialize it with a pointer or another array.
If I were you, I would get rid of the const, document that .data shouldn't be changed post-initialization, and then use memcpy to initialize it.
(const on struct members doesn't work very well in my opinion. It effectively prevents you from being able to have initializer functions, and while C++ gets around the problem a little bit by having special language support for its constructor functions, the problem still remains if the const members are arrays).

constant pointer to structure element in constant structure

I am trying to access structure element via constant pointer. Program works like it should but I got warning 'intialization from incompatible pointer type' and '(near initalization for 'B.settings)'. I don't really know how to correctly initalize it. Can someone pls help me figure that out?
Here's my code :
It's just a snippet of larger part. Idea is to have access to structure variables x,y when moving via pointers to const structure. Hope that make sense.
#include <stdio.h>
#define PGM_STR(X) ((const char[]) { X })
struct SettingsStruct
{
unsigned int x;
unsigned int y;
}Settings;
struct constitem
{
const char * const text;
const struct constitem *next;
const struct SettingsStruct * settings;
};
struct constitem const A;
struct constitem const B = {PGM_STR("x"), &A, &Settings.x };
struct constitem const A = {PGM_STR("y"), &B, &Settings.y };
static const struct constitem *currMenuPtr=&A;
void main()
{
Settings.x = 1;
Settings.y = 2;
printf("%s\n",currMenuPtr->text);
printf("%d\n",*(currMenuPtr->settings));
currMenuPtr = currMenuPtr->next;
printf("%s\n",currMenuPtr->text);
printf("%d\n",*(currMenuPtr->settings));
}
In your code, Settings.x is an unsigned int, and therefore &Settings.x is an unsigned int *. You are trying to use it to initialize a value of type const struct SettingsStruct *. The compiler is quite right to complain -- what you are doing is highly questionable, and I suppose probably not what you actually mean to do. The same applies to Settings.y.
It looks like you could get the compiler to stop complaining (about that) by changing the type of the third element of struct constitem to unsigned int *. You'll have to judge whether that actually works for you in the larger scheme of your program, though.
There is also a problem with using &A in the initializer for variable B when A is not yet declared at the point where the initializer appears. Inasmuch as you also refer to B in A's initializer, you can't solve that by swapping the declaration order. If you really do want a circular chain of pointers, then the pointer values cannot be const, because at least one of them will need to be modified after initialization.

Why does this C code treat a local struct as a pointer, when it doesn't appear to be a pointer?

In the following code, "stk" is treated as if it is a pointer. But after looking at it from every angle for hours, I cannot for the life of me see how it is a pointer. Can someone please explain what I'm missing?
struct T {
int count;
struct elem {
void *x;
struct elem *link;
} *head;
};
T Stack_new(void) {
T stk;
NEW(stk);
stk->count = 0;
stk->head = NULL;
return stk;
}
My interpretation says that T is a struct, and therefore stk is a local, automatic variable containing a struct. It is not a pointer, but then it gets treated as a pointer, leaving me stuck in a WTF state.
More Background
This code is from a book called "C Interfaces and Implementations" by Hanson. He creates a library of abstract data types that expose an interface and hide the implementation. The stack is the first one he covers. I'm a long-time programmer just now digging into C, and apparently there's some way of parsing this syntax that I'm missing. Thanks.
In case it is relevant, here is the definition for NEW and the things that new calls:
#define NEW(p) ((p) = ALLOC((long)sizeof *(p)))
#define ALLOC(nbytes) \
Mem_alloc((nbytes), __FILE__, __LINE__)
extern void *Mem_alloc (long nbytes,
const char *file, int line);
In the snippet above, T stk will declare stk as a variable of type T. However type T isn't defined anywhere, and the code won't compile.
If it instead said struct T stk;, it would be declaring stk as a variable having type struct T. However, the attempts to dereference stk would be meaningless and the code would again fail to compile.
To make the example work, you could add something like,
typedef struct T *T
which defines type T to be a pointer to struct T. I would find this highly confusing though.

What does the code below mean, in regards to structs in C?

I'm really new to C programming and I'm still trying to understand the concept of using pointers and using typedef structs.
I have this code snippet below that I need to use in a program:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
}* pStudentRecord;
I'm not exactly sure what this does - to me it seems similar as using interfaces in Objective-C, but I don't think that's the case.
And then I have this line
pStudentRecord* g_ppRecords;
I basically need to add several pStudentRecord to g_ppRecords based on a number. I understand how to create and allocate memory for an object of type pStudentRecord, but I'm not sure how to actually add multiple objects to g_ppRecords.
defines a pointer to the struct described within the curly bracers, here is a simpler example
typedef struct {
int x;
int y;
}Point,* pPoint;
int main(void) {
Point point = {4,5};
pPoint point_ptr = &point;
printf("%d - %d\n",point.x,point_ptr->x);
pPoint second_point_ptr = malloc(sizeof(Point));
second_point_ptr->x = 5;
free(second_point_ptr);
}
The first declares an unnamed struct, and a type pStudentRecord that is a pointer to it. The second declares g_ppRecords to be a pointer to a pStudentRecord. In other words, a pointer to a pointer to a struct.
It's probably easier to think of the second as an "array of pointers". As such, g_ppRecords[0] may point to a pStudentRecord and g_ppRecords[1] to another one. (Which, in turn, point to a record struct.)
In order to add to it, you will need to know how it stores the pointers, that is, how one might tell how many pointers are stored in it. There either is a size somewhere, which for size N, means at least N * sizeof(pStudentRecord*) of memory is allocated, and g_ppRecords[0] through g_ppRecords[N-1] hold the N items. Or, it's NULL terminated, which for size N, means at least (N+1) * sizeof(pStudentRecord*) of memory is allocated and g_ppRecords[0] through g_ppRecords[N-1] hold the N items, and g_ppRecords[N] holds NULL, marking the end of the string.
After this, it should be straightforward to create or add to a g_ppRecords.
A struct is a compound data type, meaning that it's a variable which contains other variables. You're familiar with Objective C, so you might think of it as being a tiny bit like a 'data only' class; that is, a class with no methods. It's a way to store related information together that you can pass around as a single unit.
Typedef is a way for you to name your own data types as synonyms for the built-in types in C. It makes code more readable and allows the compiler to catch more errors (you're effectively teaching the compiler more about your program's intent.) The classic example is
typedef int BOOL;
(There's no built-in BOOL type in older ANSI C.)
This means you can now do things like:
BOOL state = 1;
and declare functions that take BOOL parameters, then have the compiler make sure you're passing BOOLs even though they're really just ints:
void flipSwitch(BOOL isOn); /* function declaration */
...
int value = 0;
BOOL boolValue = 1;
flipSwitch(value); /* Compiler will error here */
flipSwitch(boolValue); /* But this is OK */
So your typedef above is creating a synonym for a student record struct, so you can pass around student records without having to call them struct StudentRecord every time. It makes for cleaner and more readable code. Except that there's more to it here, in your example. What I've just described is:
typedef struct {
char * firstName;
char * lastName;
int id;
float mark;
} StudentRecord;
You can now do things like:
StudentRecord aStudent = { "Angus\n", "Young\n", 1, 4.0 };
or
void writeToParents(StudentRecord student) {
...
}
But you've got a * after the typedef. That's because you want to typedef a data type which holds a pointer to a StudentRecord, not typedef the StudentRecord itself. Eh? Read on...
You need this pointer to StudentRecord because if you want to pass StudentRecords around and be able to modify their member variables, you need to pass around pointers to them, not the variables themselves. typedefs are great for this because, again, the compiler can catch subtle errors. Above we made writeToParents which just reads the contents of the StudentRecord. Say we want to change their grade; we can't set up a function with a simple StudentRecord parameter because we can't change the members directly. So, we need a pointer:
void changeGrade(StudentRecord *student, float newGrade) {
student->mark = newGrade;
}
Easy to see that you might miss the *, so instead, typedef a pointer type for StudentRecord and the compiler will help:
typedef struct { /* as above */ } *PStudentRecord;
Now:
void changeGrade(PStudentRecord student, float newGrade) {
student->mark = newGrade;
}
It's more common to declare both at the same time:
typedef struct {
/* Members */
} StudentRecord, *PStudentRecord;
This gives you both the plain struct typedef and a pointer typedef too.
What's a pointer, then? A variable which holds the address in memory of another variable. Sounds simple; it is, on the face of it, but it gets very subtle and involved very quickly. Try this tutorial
This defines the name of a pointer to the structure but not a name for the structure itself.
Try changing to:
typedef struct
{
char* firstName;
char* lastName;
int id;
float mark;
} StudentRecord;
StudentRecord foo;
StudentRecord *pfoo = &foo;

Resources