J
John Reye
Hello,
Exercise 0.2 (page 16) from "The Standard C Library" is:
Create a program which contains this line
x : ((struct x *)x)->x = x(5);
An "easy" solution is given right at the bottom; but that is not the
main focus here...
I am interested in a solution where the ":"
is part of an inline if... () ? :
Thus the line will occur like this:
?
x : ((struct x *)x)->x = x(5);
Here is my attempt:
#include <stdio.h>
#include <stdlib.h>
struct y
{
struct y *num;
int a;
};
struct x
{
struct y *x;
};
#define x(num) (&(struct y){NULL, (num)})
int main(void)
{
int i;
struct y *tmp;
struct y *x = &(struct y) {&(struct y) {NULL, 2}, 1};
x = x(5);
((struct x *)x)->x = x(5);
for (i = 0; i < 2; ++i) {
tmp = i ?
x : ((struct x *)x)->x = x(5);
printf("%d\n", tmp->num->a);
}
return EXIT_SUCCESS;
}
The compiler (gcc) complains:
almost.c:30:31: error: lvalue required as left operand of assignment
I find this rather strange, since the following lines from above,
compile without error:
x = x(5);
((struct x *)x)->x = x(5);
How can one fix the compiler error????
Not even this works:
tmp = ((struct y*)(i ?
x : ((struct x *)x)->x) = x(5));
As an aside... Ironically if I dereference the inline if, then the
following compiles without problem:
*tmp = *(i ?
x : ((struct x *)x)->x) = *x(5);
My question:
1) Can one fix my above attempt, that results in the compiler error?
2) Can one have this line in a valid C program, as part of an inline-
if
/*... */ ? /*... */
x : ((struct x *)x)->x = x(5);
Thanks.
"Easy" solution:
#include <stdio.h>
#include <stdlib.h>
struct y
{
struct y *num;
int a;
};
struct x
{
struct y *x;
};
#define x(num) (&(struct y){NULL, (num)})
int main(void)
{
int i;
struct y *tmp;
struct y *x = &(struct y) {&(struct y) {NULL, 2}, 1};
// x is a goto-label
x : ((struct x *)x)->x = x(5);
printf("%d\n", x->num->a);
return EXIT_SUCCESS;
}
Exercise 0.2 (page 16) from "The Standard C Library" is:
Create a program which contains this line
x : ((struct x *)x)->x = x(5);
An "easy" solution is given right at the bottom; but that is not the
main focus here...
I am interested in a solution where the ":"
is part of an inline if... () ? :
Thus the line will occur like this:
?
x : ((struct x *)x)->x = x(5);
Here is my attempt:
#include <stdio.h>
#include <stdlib.h>
struct y
{
struct y *num;
int a;
};
struct x
{
struct y *x;
};
#define x(num) (&(struct y){NULL, (num)})
int main(void)
{
int i;
struct y *tmp;
struct y *x = &(struct y) {&(struct y) {NULL, 2}, 1};
x = x(5);
((struct x *)x)->x = x(5);
for (i = 0; i < 2; ++i) {
tmp = i ?
x : ((struct x *)x)->x = x(5);
printf("%d\n", tmp->num->a);
}
return EXIT_SUCCESS;
}
The compiler (gcc) complains:
almost.c:30:31: error: lvalue required as left operand of assignment
I find this rather strange, since the following lines from above,
compile without error:
x = x(5);
((struct x *)x)->x = x(5);
How can one fix the compiler error????
Not even this works:
tmp = ((struct y*)(i ?
x : ((struct x *)x)->x) = x(5));
As an aside... Ironically if I dereference the inline if, then the
following compiles without problem:
*tmp = *(i ?
x : ((struct x *)x)->x) = *x(5);
My question:
1) Can one fix my above attempt, that results in the compiler error?
2) Can one have this line in a valid C program, as part of an inline-
if
/*... */ ? /*... */
x : ((struct x *)x)->x = x(5);
Thanks.
"Easy" solution:
#include <stdio.h>
#include <stdlib.h>
struct y
{
struct y *num;
int a;
};
struct x
{
struct y *x;
};
#define x(num) (&(struct y){NULL, (num)})
int main(void)
{
int i;
struct y *tmp;
struct y *x = &(struct y) {&(struct y) {NULL, 2}, 1};
// x is a goto-label
x : ((struct x *)x)->x = x(5);
printf("%d\n", x->num->a);
return EXIT_SUCCESS;
}