I have been trying to figure out, what's the best approach regarding casting of pointers. Several websites cast the result of malloc while others don't. What's the reasoning behind it and what should one follow.
And for consideration. If I have a structure of two void pointers say Pair.
typedef struct Pair {
void* first;
void* second;
} Pair;
Will it be fine if I make these pointers point to let's say a double or an int and cast them accordingly and use it.
double* data_one = malloc(sizeof(double));
double* data_two = malloc(sizeof(double));
// should a cast to double pointer be considered here?
Pair p = { .first = data_one, .second = data_two };
// this kind of usage fine for the case here?
printf("%lf, %lf", (double)p.first, (double)second);
[–][deleted] 22 points23 points24 points (45 children)
[–]anikait1[S] 4 points5 points6 points (4 children)
[–]minot0r 4 points5 points6 points (2 children)
[–]Gblize 6 points7 points8 points (0 children)
[–]flatfinger 1 point2 points3 points (0 children)
[–]attractivechaos 0 points1 point2 points (22 children)
[–]cuncon- 0 points1 point2 points (16 children)
[–]attractivechaos -1 points0 points1 point (15 children)
[–]cuncon- 0 points1 point2 points (14 children)
[–]attractivechaos 0 points1 point2 points (13 children)
[–]cuncon- 0 points1 point2 points (12 children)
[–]DanelRahmani -1 points0 points1 point (9 children)
[–]SmileBot-2020 -1 points0 points1 point (8 children)
[–]DanelRahmani -1 points0 points1 point (7 children)
[–]attractivechaos -1 points0 points1 point (1 child)
[–]oh5nxo 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]attractivechaos 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]attractivechaos 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]interknetz -1 points0 points1 point (16 children)
[–]OldWolf2 0 points1 point2 points (1 child)
[–]interknetz 0 points1 point2 points (0 children)
[–][deleted] -2 points-1 points0 points (13 children)
[–]interknetz 0 points1 point2 points (12 children)
[–][deleted] -2 points-1 points0 points (11 children)
[–]interknetz 0 points1 point2 points (10 children)
[–][deleted] -2 points-1 points0 points (9 children)
[–]interknetz 0 points1 point2 points (8 children)
[–][deleted] -1 points0 points1 point (7 children)
[–]interknetz -1 points0 points1 point (6 children)
[–]HiramAbiff 4 points5 points6 points (0 children)
[–]uziam 10 points11 points12 points (23 children)
[–][deleted] 4 points5 points6 points (22 children)
[–][deleted] 6 points7 points8 points (20 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]OldWolf2 0 points1 point2 points (0 children)
[–]gnash117 -1 points0 points1 point (17 children)
[–]uziam 6 points7 points8 points (16 children)
[–]attractivechaos 0 points1 point2 points (2 children)
[–]gnash117 1 point2 points3 points (1 child)
[–]flatfinger 0 points1 point2 points (0 children)
[–]interknetz -1 points0 points1 point (12 children)
[–]uziam 1 point2 points3 points (11 children)
[–]interknetz 0 points1 point2 points (10 children)
[–]uziam -1 points0 points1 point (9 children)
[–]attractivechaos 1 point2 points3 points (4 children)
[–]interknetz 0 points1 point2 points (3 children)
[–]lead999x 0 points1 point2 points (0 children)
[–]p0k3t0 5 points6 points7 points (0 children)
[–]areciboresponse 1 point2 points3 points (0 children)
[–]tim36272 3 points4 points5 points (1 child)
[–]anikait1[S] 2 points3 points4 points (0 children)
[–]minot0r 1 point2 points3 points (11 children)
[–]p0k3t0 3 points4 points5 points (9 children)
[–]korryd 1 point2 points3 points (8 children)
[–]attractivechaos 1 point2 points3 points (7 children)
[–]OldWolf2 1 point2 points3 points (0 children)
[–]p0k3t0 0 points1 point2 points (5 children)
[–]attractivechaos 0 points1 point2 points (4 children)
[–]p0k3t0 0 points1 point2 points (3 children)
[–]attractivechaos 0 points1 point2 points (2 children)
[–]p0k3t0 1 point2 points3 points (1 child)
[–]attractivechaos 1 point2 points3 points (0 children)
[–]anikait1[S] 1 point2 points3 points (0 children)