I want to Prompt the user to enter 3 numbers. Then, swap the first number with the second one, the second number with the third and the third with the first by calling a function called "swap".
Functions in C cannot return more than one value so I decided to create a structure with pointers that I will later use in my function. Then, I created three-pointers that will store the address of each one of the numbers so I can dereference to these numbers in my function (as shown below)
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
Here's my code:
#include <stdio.h>
void swap(); // a = b, b = c, c = a
struct Numbers {
int *pa, *pb, *pc;
} ;
int main(void) {
struct Numbers Number; // Structure to hold the values of the three variables.
int a, b, c;
int *ppa, *ppb, *ppc;
printf("\n Please enter three integer numbers: ");
scanf("%d%d%d", &a, &b, &c);
ppa = &a; ppb = &b; ppc = &c;
swap(a, b, c, Number, *ppa, *ppb, *ppc);
printf("\n %d \t %d \t %d \n", Number.pa, Number.pb, Number.pc);
}
void swap(int a, int b, int c, struct Numbers Number, int *ppa, int *ppb, int *ppc) {
Number.pa = *ppb;
Number.pb = *ppc;
Number.pc = *ppa;
} ;
Most of the arguments to your swap function are either pointless or the work of sheer guessing (or both). The assignment effectively wants you to "rotate" values from a through c. So do that, and only that.
#include <stdio.h>
void swap(int *pa, int *pb, int *pc);
int main()
{
int a, b, c;
printf("\n Please enter three integer numbers: ");
if (scanf("%d %d %d", &a, &b, &c) == 3)
{
swap(&a, &b, &c);
printf("%d %d %d \n", a, b, c);
}
return 0;
}
void swap(int *pa, int *pb, int *pc)
{
int tmp = *pa;
*pa = *pb;
*pb = *pc;
*pc = tmp;
}
Stop reading more into an assignment than is there. If it sounds simple, it probably is. The warning was due to passing the value of dereferenced pointers to int (so int values) to a function expecting int pointers; not int values. As you can see, you don't need to do any of that (and didn't in the first place).
Related
I just started learning, I didn't understand the book, so I asked for advice.
I am a beginner and don't have a good English.
Function: Combine two two-digit positive integers A and B to form an integer in C
Middle. The method of merging is: the ten digits and single digits of the A number are placed on the thousand and ten digits of the C number, and the ten and single digits of the B number are placed on the single and hundred digits of the C number.
For example: when a=45, b=12. After calling this function, c=4251.
Here is my code
#include <stdio.h>
void fun(int a, int b, long *c);
int main()
{
int a,b;
long c;
int state = 1;
printf("Enter a: ");
printf("(q to quit)");
while( scanf("%d",&a)==state)
{
printf("Enter b: ");
printf("(q to quit)");
while( scanf("%d",&b)==state)
{
fun(a, b, &c);
printf("The result is: %ld\n", c);
}
}
return 0;
}
void fun(int a, int b, long *c)
{
/**********Program**********/
*c = 100*(a%100)+b%100;
/********** End **********/
}
I tried removing the * and found that the result was 16. It is wrong but not know why
The parameter long *c means c is an address of a variable (in this case the variable in main() is called c and you need to call the function like this fun(a, b, &c)).
When you want to update the value stored at that address c the syntax is *c = .... If you do c = ... you are updating the address of the variable which has no external effect.
Alternatively, you could change your function to return a value and the call would then look like this:
c = fun(a, b);
and the function would be:
int fun(int a, int b) {
return 100*(a%100)+b%100;
}
The function with the pass by value works like it ought to, I believe, as it prints out the value 0 whenever I enter two numbers. However the adderRef (pass by reference function) doesn't work. All it prints out is "the pass by reference of c is" and that's it. There is no value or anything. I just wanted to inquire whether there was something wrong with my syntax or something....
Okay guys sorry about the question being vague and for my errors. It's my first time asking on stackoverflow and I should have been more mindful. I'm aware of my error and why I made it. I got muddled in class when my teacher was altering the code a bit and I copied it down incorrectly/ Thanks everyone for your help. Yes i was indeed quite dumb .
#include <stdio.h>
#include <stdlib.h>
void adderval(int a, int b);
void adderRef(int *, int *);
int main()
{
int a, b, c = 0;
printf("enter two numbers.\n");
scanf("%d %d", &a, &b);
adderval(a, b);
printf("the pass by value of c is %d \n", c);
adderRef(&a, &b);
printf("the pass by reference of c is \n", c );
return 0;
}
void adderRef(int *a, int *b )
{
int c;
c = *a + *b;
}
void adderval(int a, int b )
{
int c;
c = a + b;
}
corrected as below.
#include <stdio.h>
#include <stdlib.h>
void adderval(int a,int b,int c);
void adderRef(int a,int b,int *c );
int main()
{
int a,b,c=0;
printf("enter two numbers.\n");
scanf("%d %d",&a,&b);
adderval(a,b,c);
printf("the pass by value of c is %d \n",c);
adderRef(a,b,&c);
printf("the pass by reference of c is %d \n",c );
return 0;
}
void adderRef (int a, int b, int *c )
{
*c = a + b;
}
void adderval (int a , int b,int c )
{
c=a+b;
}
I'm trying to learn call functions and I've come across a problem where I'm being told I need I need to declare a, b, c, and d, but the point of the program is to prompt the user for these numbers and then sum them.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int f(int a, int b, int c, int d);
int g(int b, int c, int d);
int h(int c, int d);
int i(int d);
int
main(int argc,char **argv)
{
int result;
result = f(a,b,c,d);
printf("Value of a?");
scanf("%d",a);
printf("Value of b?");
scanf("%d",b);
printf("Value of c?");
scanf("%d",c);
printf("Value of d?");
scanf("%d",d);
printf("Your result is %d",result);
return 0;
}
int
f(int a, int b, int c, int d)
{
return a + g(b,c,d);
}
int
g(int b, int c, int d)
{
return b + h(c,d);
}
int
h(int c, int d)
{
return c + i(d);
}
int
i(int d)
{
return d + d;
}
The specific warning is
call.c:16:13: error: ‘a’ undeclared (first use in this function)
result = f(a,b,c,d);
and it repeats for b, c, and d.
Can anyone tell me what I've done wrong? I put the function signatures at the top where int a, int b, int c, and int d are already defined so I'm confused as to what I've done incorrectly.
Edit: Question has been solved! The code should look like
int result;
int a;
int b;
int c;
int d;
printf("Value of a?");
scanf("%d",&a);
printf("Value of b?");
scanf("%d",&b);
printf("Value of c?");
scanf("%d",&c);
printf("Value of d?");
scanf("%d",&d);
result = f(a,b,c,d);
printf("Your result is %d",result);
return 0;
You need to declare those variables in main, try changing your code to
int result, a, b, c, d;
Also scanf expects pointers in it's variadic portion of arguments,
so you're going to need to use the & reference operator to take the address of a
Example: scanf("%d", &a);
And do this for all of your scanf calls.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Hi I have written a c program that reads in 2 values then swaps them and prints the new values except the second value keeps showing 0. For example it you enter 10 for 'a' and 8 tor 'b', then a will be 8 but b will be 0. Does anyone know the solution to fix this? Here is the code:
#include <stdio.h>
int getData()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
int a, b = getData();
swapValues(a, b);
return(0);
}
return (a, b);
doesn't do what you think it does, it's a misapplication of the comma operator.
The expression op1, op2 evaluates both op1 and op2 but gives you the value of op2. So it's not passing back a couple of values (although some languages like Python can do this sort of thing).
Similarly,
int a, b = getData();
won't grab the mythical two values returned from getData(). Rather it will set a to an indeterminate value and set b based on the single value returned from the function.
I would be looking at something like this:
#include <stdio.h>
int getData (char *which) {
int val;
printf ("Enter value for %s: ", which);
scanf("%d", &val);
return val;
}
void swapValues (int a, int b) {
printf("The swapped value of a is: %d\n", b);
printf("The swapped value of b is: %d\n", a);
}
int main (void) {
int a = getData ("a");
int b = getData ("b");
swapValues(a, b);
return 0;
}
You should also keep in mind that, if you actually want to swap the variables a and b and have that reflected back to main(rather than just print them as if they've been swapped), you'll need to pass pointers to them and manipulate them via the pointers.
C is a pass-by-value language meaning that changes to function parameters aren't normally reflected back to the caller. That would go something like this:
void swapValues (int *pa, int *pb) {
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
:
swapValues (&a, &b);
// a and b are now swapped.
You have unnecessarily complicated the whole thing.For one, something like return(a,b) is absurd in C.Further, if you intend to swap, why are you passing b as argument for the printf() meant to print 'a' and passing a to the printf() of 'b'?Anyways,here's a modified code that keeps it simple and gets the job done.
#include <stdio.h>
void swapValues()
{
int a, b,tem;
printf("Enter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
tem=a;
a=b;
b=tem;
printf("\nThe value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swapValues();
return(0);
}
First of all you can't return more than one value in C. The way around that is to return a struct or pass the values address.
void getData(int *a,int* b)
{
//int a, b;
printf("Enter first number: ");
scanf("%d", a); // look here you passed the address of a to scanf
// by doing that scanf can write to a
printf("Enter second number: ");
scanf("%d", b);
//return(a, b);
}
The old main:
int main()
{
int a, b = getData(); // b gets the return value from getData()
// but a is still uninitialized
//to call the new function you have to do the following
int a,b;
getData(&a,&b);
swapValues(a, b);
return(0);
}
You cannot return multiple values from a C function. I'm not even sure why the statement return(a, b) compiles.
If you want to return more than value from a function you should either put them into an array or a structure. I'm going to use a structure to demonstrate one way to do this correctly. There are many ways to do this, but this one modifies you code the least.
struct TwoNums{
int a;
int b;
};
TwoNums getData()
{
/* This creates a new struct of type struct TwoNums */
struct TwoNums nums;
printf("Enter first number: ");
scanf("%d", &(nums.a));
printf("Enter second number: ");
scanf("%d", &(nums.b));
return(a, b);
}
void swapValues(int a, int b)
{
printf("The value of a is: %d", b);
printf("\nThe value of b is: %d", a);
return;
}
int main()
{
/* Get the whole structure in one call */
struct TwoNums nums = getData();
/* Call the swap function using fields of the structure */
swapValues(nums.a, nums.b);
return 0;
}
The first:
getData() function is written incorrectly.
You can not return more than one parameter from the function in C. So you can to separate data reading, or use pointers as below:
void getData(int* a, int* b) {
printf("Enter first number: ");
scanf("%d", a);
printf("Enter second number: ");
scanf("%d", b);
}
In main()
int a, b;
getData(&a, &b);
The second:
swapValues(int a, int b) does not swap the data.
More correct:
void swapValues(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
without using temporary variable.
So try this code
#include <stdio.h>
int swape()
{
int a,b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
a=a+b;
b=a-b;
a=a-b;
printf("The value of a is: %d", a);
printf("\nThe value of b is: %d", b);
}
int main()
{
swape();
return(0);
}
Hii ,
I have written the following code to improve it for the higher datastructures .
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int display(int *a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("\n%d %d",a[0],a[1]); ------- point 1
printf("\n %d %d",*(a[0]),*(a[1])); ------- point 2
return 1;
}
int main()
{
int *a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d",b,c);
display(a,&b,&c);
getchar();
}
I get the addresses in point 1 , but i dont get the values in point 2....What have i done wrong ... If my program itself is wrong , please jus give me a sample code that can dereference an array of pointers to get the value pointed by the element of array...
This code shouldn't compile. The signature for display should be int display(int **a , int *b , int *c), because a is a pointer to int* (remember that arrays degrade to pointers). Then, you need to write printf("\n%d %d",*a[0],*a[1]) to dereference the pointers in the array.
#include <stdio.h>
int display(int** a, int* b, int* c)
{
// store the value of b and c on array a
a[0] = b;
a[1] = c;
//print the memory addresses stored in hex format
printf("0x%x 0x%x\n", (int)a[0], (int)a[1]);
//print the values
printf("%d %d\n", *a[0], *a[1]);
return 1;
}
int main()
{
int* a[5];
int b,c;
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
getchar();
return 0;
}
The signature of display() indicates that a is a pointer. In theory, this might work in C, but gcc gave me an error. What you want to tell the compiler is that you want an array of pointers. I accomplished this with int **a in the function signature. The code below shows how I did this. Also, I cleaned it up a bit, since some of your includes aren't needed, the pointers should be printed as unsigned, display would probably be better as a void, having the "\n" at the beginning of the line was irritating in that the last line of output ended up on my prompt line, and the getchar() serves no real purpose here.
#include<stdio.h>
void display(int **a , int *b , int *c)
{
a[0] = b;
a[1] = c;
printf("%u %u\n", a[0], a[1]);
printf("%d %d\n", *a[0], *a[1]);
}
int main(void)
{
int *a[5];
int b,c;
printf("Enter two integers: ");
scanf("%d %d",&b,&c);
printf("%d %d\n",b,c);
display(a,&b,&c);
}
You want store addresses (pointers) in the vector a. Define your function to accept a vector of pointers:
int display(int *a , int *b , int *c)
This has the advantage that the compiler compiles the code.
Or better: use names that help to remember what you mean.