I have been asked to make a code that will rearrange 3 entered integers into ascending/descending order using pointers.
I need to use the function order() to return a pointer to either function ascending3() or descending(), depending on what value of 'e' is entered.
I keep getting an error on the line specified in the readInts() function and am not sure how to fix it.
ERRORS
lvalue required as unary ‘&’ operand" -- The error for `ptr = &(order(e))`
warning: assignment makes pointer from integer without a cast -- The error for `ptr=order(e)`
Pointer code
void readInts(){
int *a,*b,*c;
char e;
int (*ptr1)(int*,int*,int*);
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = &(order(e)); /*ERROR HERE*/
result = (*ptr1)(a,b,c);
printf("%d %d %d", a, b, c);
}
Functions
int ascending3(int* x, int* y, int* z)
{
/*removed sorting code for the sake of shortening the question*/
*x=smallest;
*y=middle;
*z=largest;
}
int descending(int* x, int* y, int* z)
{
int swap;
ascending3(x,y,z);
swap=*x;
*x=*z;
*z=swap;
}
int (*order(char e))(int*x ,int*y,int*z)
{
if(e=='a')
{
return ascending3;
}
else if(e =='d')
{
return descending;
}
return;
}
A function cannot return a function. For this reason you cannot apply the address of operator (&) to the result of your function (to retrieve an address). But a function can return a pointer to a function.
The name of a function in C (prefixed or not by the & operator) is always set as the address of the function, that's a pointer to that function.
The correct code is:
int ascending3(int *x, int *y, int *z);
int descending(int *x, int *y, int *z);
typedef int (*fn)(int *x, int *y, int *z);
fn order(char e)
{
if (e == 'a')
{
return ascending3;
}
else if (e == 'd')
{
return descending;
}
return NULL;
}
void readInts(void)
{
int *a, *b, *c;
char e;
fn ptr1;
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = order(e);
result = (*ptr1) (a, b, c);
printf("%p %p %p", a, b, c);
}
Where I used a typedef to declare the type of our function pointer (and also 2 prototypes for the ordering functions).
If you like to have the asterisk to better show the pointer nature of our type, you can define fn as function (not a pointer to function):
typedef int (fn)(int *x, int *y, int *z);
So you can use the asterisk notation:
typedef int fn(int *x, int *y, int *z);
fn *order(char e)
{
if (e == 'a')
{
return ascending3;
}
else if (e == 'd')
{
return descending;
}
return NULL;
}
void readInts(void)
{
int *a, *b, *c;
char e;
fn *ptr1;
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = order(e);
result = (*ptr1) (a, b, c);
printf("%p %p %p", a, b, c);
}
Related
#include<stdio.h>
void add(int a,int b)
{
int c=a+b;
printf("\nSum=%d",c);
}
void hello(char *name)
{
printf("Hello %s",*name);
}
int main()
{
int a,b;
char name[20];
void (*ptr)(int,int)=&add;
void (*hello)(char*)=hello;
printf("Enter your Name:");
scanf("%s",&name);
hello(&name);
printf("Enter the two values\n");
scanf("%d%d",&a,&b);
ptr(a,b);
return 0;
}
i want to take input from user then pass it to function but i am unable to do so.
Here is what my complier shows as error: https://i.stack.imgur.com/DVYL6.png
You don't need to access the array address, it will be implicitly converted to char* when you pass it to the functions (both to scanf and hello).
I don't see the use of the functions pointers, so in order to simplify the code, I would rewrite it like this:
#include <stdio.h>
void add(int a, int b)
{
printf("Sum = %d\n", a + b);
}
void hello(char *name)
{
printf("Hello %s\n", name);
}
int main()
{
int a = 0, b = 0;
char name[20];
printf("Enter your Name:\n");
scanf("%s", name);
hello(name);
printf("Enter the two values\n");
scanf("%d%d", &a ,&b);
add(a, b);
return 0;
}
If you insist of using the pointers, this is how main should be written:
int main()
{
int a = 0, b = 0;
char name[20];
void (*add_ptr)(int, int) = &add;
void (*hello_ptr)(char *) = &hello;
printf("Enter your Name:\n");
scanf("%s", name);
hello_ptr(name);
printf("Enter the two values\n");
scanf("%d%d", &a, &b);
add_ptr(a, b);
return 0;
}
I have tried this program to take 3 integers and print the 2nd largest number:
#include<stdio.h>
int main()
{
int a,b,c,max2;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
max2=a;
if(a>b){
max2=b;
printf("")
}
return 0;
}
Now i am stuck here. I am unable to find the logic behind this code. What can I do?
This is not the Logic which you can understand, it'll not give you the right, expected result.
Code:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Values: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b && a>c)
{
if(b>c)
printf("2nd largest: %d", b);
else
printf("2nd largest: %d", c);
}
else if(b>c && b>a)
{
if(c>a)
printf("2nd largest: %d", c);
else
printf("2nd largest: %d", a);
}
else if(a>b)
printf("2nd largest: %d", a);
else
printf("2nd largest: %d", b);
return 0;
}
You should compare all the three variables to get the 2nd largest among those numbers.
Output:
Values: 32 31 12
2nd largest: 31
Explanation:
First pick any variable and compare it with the other two variables like if(a>b && a>c), if its true, it means a is the largest and any of the two variables b and c is the 2nd largest, so inside the if(a>b && a>c) block there's a comparison if(b>c), if true then b is the 2nd largest otherwise c is the second largest. Similarly, compare the other two variables for if they are the largest. e.g. else if(b>c && b>a) and else if(c>a && c>b).
One method is to sort these three numbers and then print the middle one:
#include <stdio.h>
static inline void swap_if_out_of_order (int *p, int *q)
{
if (*p > *q) {
int t = *p;
*p = *q;
*q = t;
}
}
int main (void)
{
int a, b, c;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3) {
swap_if_out_of_order(&a, &b);
swap_if_out_of_order(&b, &c);
swap_if_out_of_order(&a, &b);
printf("Second greatest: %d\n", b);
}
}
Or, without sorting, with at most three comparisons:
#include <stdio.h>
int main (void)
{
int a, b, c, m;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3) {
if (a > b) {
if (b > c) m = b;
else if (a > c) m = c;
else m = a;
} else if (a > c) m = a;
else if (b > c) m = c;
else m = b;
printf("Second greatest: %d\n", m);
}
}
or, likely the most efficient way with max and min functions:
#include <stdio.h>
static inline int min (int x, int y) { return x < y ? x : y; }
static inline int max (int x, int y) { return x > y ? x : y; }
int main (void)
{
int a, b, c;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3)
printf("Second greatest: %d\n", max(min(a, b), min(max(a, b), c)));
}
JS recursion:
function f(a,b,c) {
if(a>=b && c<b) return b;
if(a>b) return f(a,c,b);
return f(b,a,c);
}
f(2,12,0) // 2
#include <stdio.h>
main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
// largest //
if(a>b&&a>c)
printf("largest=%d",a);
if(b>a&&b>c)
printf("largest=%d",b);
if(c>b&&c>a)
printf("largest=%d",c);
// second largest//
if(a>b&&a<c)
printf("\nscenond largest=%d",a);
if(b>a&&b<c)
printf("\nscenond largest=%d",b);
if(c>a&&c<b)
printf("\nscenond largest=%d",c);
}
this will output the largest and the second largest number.
I'm trying to allocate memory through a function and store the proper data type in that memory, but my int didn't work without (*int), how do I apply that as a double? (*double) is not working
#include <stdio.h>
#include <stdlib.h>
int newInt ();
double newDouble ();
int main() {
int userChoice;
int userValue;
int *intPtr;
double *doublePtr;
printf("Please choose which type of value you'd like to store (1-4).");
puts("\n 1.) Int \n 2.) Double \n");
scanf("%d", &userChoice);
if (userChoice == 1) {
intPtr = (int*)newInt();
printf("You chose an Int, please enter an Int value\n");
scanf("%d", *&intPtr);
printf("Memory value is %p \n", intPtr);
printf("Stored value is %d", *intPtr);
} else if (userChoice == 2) {
doublePtr = (double*)newDouble();
printf("You chose a Double, please enter a Double value\n");
scanf("%lf", *&doublePtr);
printf("Memory value is %p \n", doublePtr);
printf("Stored value is %lf", *doublePtr);
return 0;
}
int newInt () {
malloc(sizeof(int));
}
double newDouble () {
malloc(sizeof(double));
}
I fixed it.
Corrections in the comments.
IDEOne Link
#include <stdio.h>
#include <stdlib.h>
int* newInt (); // Changed the return type
double* newDouble (); // Changed the return type
int main() {
int userChoice;
// int userValue; Removing unused variable.
int *intPtr;
double *doublePtr;
printf("Please choose which type of value you'd like to store (1-4).");
puts("\n 1.) Int \n 2.) Double \n");
scanf("%d", &userChoice);
if (userChoice == 1) {
intPtr = newInt(); // Removed cast
printf("You chose an Int, please enter an Int value\n");
scanf("%d", intPtr); // Removed extra *&
printf("Memory value is %p \n", intPtr);
printf("Stored value is %d", *intPtr);
} else if (userChoice == 2) {
doublePtr = newDouble(); // Removed cast
printf("You chose a Double, please enter a Double value\n");
scanf("%lf", doublePtr); // Removed extra *&
printf("Memory value is %p \n", doublePtr);
printf("Stored value is %lf", *doublePtr);
}
return 0;
} // Added a missing bracket.
int* newInt () { // Changed the return type
return malloc(sizeof(int)); // Added a return statement
}
double* newDouble () { // Changed the return type
return malloc(sizeof(double)); // Added a return statement
}
I have made a program to print out all ASCII character between two given character but then I write it in functions. The output of two these programs are different. I tried using pointer to pass variables by reference but the output is unlikely to be like the first program. What should I do for making it correctly ?
Here is the first program in C by using linear programming.
#include <stdio.h>>
int main()
{
char a,b,tmp;
int d;
scanf("%c%c",&a,&b);
if(a>b)
{
tmp=a;
a=b;
b=tmp;
}
d = b - a;
for (char c = a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
Here is the other program in functions.
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(&a,&b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
you should not pass the addresses of your variables to your function the correct one is
#include <stdio.h>>
void ascii(char a,char b);
int main()
{
char a,b,tmp;
int d;
printf("Enter 2 character => ");
scanf("%c%c",&a,&b);
ascii(a,b);
}
void ascii(char a,char b)
{
int d;
if (a>b)
{
char tmp= a;
a=b;
b=tmp;
}
d=b-a;
for (char c=a+1;c<b;c++)
{
printf("%c : %d, %o, %X\n",c,c,c,c);
}
}
I have begin learning pointers in C.
When I try to return pointer in a function, I'm getting segmentation fault error.
Here is the code :
#include<stdio.h>
int *sum(int *, int *);
int main(void)
{
int a, b;
int *ans = NULL;
printf("Enter number a : ");
scanf("%d", &a);
printf("Enter number b : ");
scanf("%d", &b);
ans = sum(&a, &b);
printf("Sum = %d", *ans);
return 0;
}
int *sum(int *p, int *q)
{
int *result = NULL;
*result = *p + *q;
return (result);
}
And the output :
Enter number a : 10
Enter number b : 20
Segmentation fault
Segmentation fault occurs in sum function, when result is declared as pointer. However, I am unable to figure out the reason for the same. Any help regarding this is really appreciable.
You are initing a pointer to NULL and then you are deferencing it: it is Undefined behavior
Change sum function to
int *sum(int *p, int *q)
{
int *result = malloc(sizeof(int));
// check if malloc returned a valid pointer before to dereference it
if (result != NULL)
{
*result = *p + *q;
}
return (result);
}
and add a free call to free the allocated memory.
// check if sum function allocate the pointer before to dereference it
if (ans != NULL)
{
printf("Sum = %d", *ans);
}
free(ans);
return 0;
}
You could also avoid to use pointer to return value:
#include<stdio.h>
int sum(int *, int *);
int main(void)
{
int a, b;
int ans;
printf("Enter number a : ");
scanf("%d", &a);
printf("Enter number b : ");
scanf("%d", &b);
ans = sum(&a, &b);
printf("Sum = %d\n", ans);
return 0;
}
int sum(int *p, int *q)
{
int result = *p + *q;
return (result);
}
The sum function could also be like:
int sum (int *p, int *q)
{
return (*p + *q);
}
EDIT
As #JonathanLeffler wrote in his answer you can do also:
#include<stdio.h>
void sum(int *, int *, int *);
int main(void)
{
int a, b;
int ans;
printf("Enter number a : ");
scanf("%d", &a);
printf("Enter number b : ");
scanf("%d", &b);
sum(&ans, &a, &b);
printf("Sum = %d\n", ans);
return 0;
}
void sum(int *result, int *p, int *q)
{
*result = *p + *q;
}
Allocate memory before trying to store anything, and check the return of malloc()
int *result = NULL;
result = malloc(sizeof(*result));
if(result != NULL)
*result = *p + *q;
else
printf("malloc returned error");
Also, check the return of the function in main() and exit accordingly.
int main(void)
{
.
.
.
ans = sum(&a, &b);
if(ans == NULL)
return 0;
printf("Sum = %d\n", ans);
free(ans); //free the memory then
return 0;
}
A third alternative is to declare ans as a normal int variable in the main function and pass a pointer to it to the sum function, like you do with the other two arguments. This is actually emulating call by reference.
#include<stdio.h>
int *sum(int *, int *);
int main(void)
{
int a, b;
printf("Enter number a : ");
scanf("%d", &a);
printf("Enter number b : ");
scanf("%d", &b);
int* ans = sum(&a, &b);
printf("Sum = %d", *ans);
return 0;
}
int *sum(int *p, int *q)
{
int plus = *p + *q;
int *ans = +
return ans;
}
Store the Sum in a Different variable plus and create a pointer *ans that points to that variable plus and return the pointer variable ans that holds the address of plus