I want to have a program in C that reads 3 numbers and prints the bigger one, using only one pointer and no more variables.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *p;
p = malloc(sizeof *p); // assume it worked
if (scanf("%d", p) != 1) /* error */; // read 1st number
printf("%d\n", *p); // print 1st number
if (scanf("%d", p) != 1) /* error */; // read 2nd number
printf("%d\n", *p); // print 2nd number
if (scanf("%d", p) != 1) /* error */; // read 3rd number
printf("%d\n", *p); // print 3rd number
free(p);
}
The program above, does what is required. In addition it prints the middle and smaller numbers!
Update using array of 3 pointers
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *p[3];
p[0] = malloc(sizeof *p[0]); // assume it worked
p[1] = malloc(sizeof *p[0]); // assume it worked
p[2] = malloc(sizeof *p[0]); // assume it worked
if (scanf("%d", p[0]) != 1) /* error */; // read 1st number
if (scanf("%d", p[1]) != 1) /* error */; // read 2nd number
if (scanf("%d", p[2]) != 1) /* error */; // read 3rd number
if ((*p[0] >= *p[1]) && (*p[0] >= *p[2])) printf("%d\n", *p[0]);
if ((*p[1] > *p[0]) && (*p[1] >= *p[2])) printf("%d\n", *p[1]);
if ((*p[2] > *p[0]) && (*p[2] > *p[1])) printf("%d\n", *p[2]);
free(p[2]);
free(p[1]);
free(p[0]);
}
5
solved How can i compare three strings using only one pointer and nothing else