3

I've defined those two data structures as types:

typedef struct {
    float x,y,z;
} location3d;

typedef struct {
    location3d location;
    float radius; 
} particle3d;

My question is: can I create a constant location3d or a constant particle3d? I searched about constants but all I found is how to define constant integers or chars... etc.

Acorn
  • 24,970
  • 5
  • 40
  • 69
moamen
  • 169
  • 1
  • 7
  • 1
    `typedef struct` is `c`. Do you need to support `c` or are you able to use modern `c++` – drescherjm Aug 17 '20 at 19:00
  • This should be of use: https://stackoverflow.com/q/388242/2079303 – eerorika Aug 17 '20 at 19:01
  • I mean yes... const locaion3d. (Probably location3d), But you would need to initialize it on the spot. So your struct would need some constructor that you can pass in and initialize values with. So eventually look like const locaion3d my_locaion(10.0,20.0,30.0); – Omid CompSCI Aug 17 '20 at 19:03
  • @Barmar How can I then set the values of location or radius? I mean they are constants. – moamen Aug 17 '20 at 19:04
  • Uniform initialization in `c++` would work without needing a constructor. This is the reason for my first comment. – drescherjm Aug 17 '20 at 19:04
  • 1
    C or C++, choose ***one***. Recommendations are likely to differ substantially depending on that choice. – John Bollinger Aug 17 '20 at 19:10
  • 1
    I'm using C, I put both of them in the tags expecting they will be similar. I'll edit the question. – moamen Aug 17 '20 at 19:15
  • 2
    `c` and `c++` are different languages. As time goes on they drift further apart. – drescherjm Aug 17 '20 at 19:18

2 Answers2

6

You use const with user-defined types exactly the same way as with built-in types. And as with other constants, you need to initialize it.

const particle3d my_particle = { {10.0, 21.5, 3}, 1.23};

You can also use designated initializers.

const particle3d my_particle = {.radius = 1.23, .location = {.x = 10.0, .y = 21.5, .z = 3}};
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Do you know if the internal initialization does this based on where the members are declared. Like in order of the class initialization? I feel like this is a horrible way, as the class gets adjusted with new members, etc. then developers would add these members in various areas, and could mess up the initialization this way. Feel like a created constructor is safer and more readable of what is being passed in etc. – Omid CompSCI Aug 17 '20 at 19:08
  • 2
    Yes, you can use designated initializers, just like non-const structures. – Barmar Aug 17 '20 at 19:13
5

You can use const the same way as with the built-in types.

Have a look at the following implementation:

#include <stdio.h>

typedef struct {
    float x,y,z;
} location3d;

typedef struct {
    location3d location;
    float radius; 
} particle3d;

int main(){

    const particle3d particle = {{1, 2, 3}, 8.9};
    
    printf("%f %f %f\n", particle.location.x, particle.location.y, particle.location.z);

    printf("%f\n", particle.radius);
    
    return 0;
}

Output:

1.000000 2.000000 3.000000
8.900000

PS: Author recently removed the C++ tag. Removed C++ code and shifted to C.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35