why is my pointer passing function giving me a wrong output? - c

the following is given
#include <stdio.h>
void func2 (int num, int *result);
int main()
{
int num, result;
printf("Enter a number: \n");
scanf("%d", &num);
func2(num, &result);
printf("func2(): %d\n", result);
return 0;
}
void func2(int num, int *result)
{
//key in code
}
in the void func2 i wrote
int i=0;
result=&i;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
but the programming is not returning the value of variable i properly. what's wrong with my variable assignment?
expected output:
Enter a number:
24 (user enter)
func2(): 20
actual output:
Enter a number:
24 (user enter)
func2(): 32767

You need to assign indirectly through result, not set result to the address of another variable.
int i=0;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
*result = i;

Related

C programming parameters

#include <stdio.h>
void getScores(int a, char n[10][15], int s[10]) {
int score;
printf("Enter the number of students: ");
scanf("%d",&a);
for (int i=0; i < a;i++)
{
scanf("%s",n[i]);
scanf("%d",&score);
s[i]=score;
}
}
void printScores(int a, char n[10][15], int s[10] ) {
for (int i=0; i < a;i++)
{
printf("%s", n[a]);
printf(" ");
printf("%d\n",s[a]);
}
}
int main() {
char names[10][15];
int scores[10];
int num;
getScores(num,names,scores);
printScores(num,names,scores);
}
What I am trying to accomplish is have the parameter value of int a from the getScores function to be used in the printScores function as an array length as it is being used in getScores.
The arrays are saving its value when used in the print function but the a value is resetting to an unassigned number 896 when I need it to be what the user enters in the get function. Any tips?
Print scores will not print anything.
void getScores(int *a,
And the call
getScores(&num
You need also change accordingly the functions code

Why is the function not mapping the input to my variable? [duplicate]

This question already has answers here:
How to use a function containing scanf() in the main function in C
(3 answers)
Closed 3 years ago.
The function is not mapping the user input to the variable
#include <stdio.h>
void input(int x);
int main (void)
{
int h = 0;
input(h);
printf("%d\n", h);
}
void input(int x)
{
printf("Enter a number: ");
scanf("%d", &x);
}
When entering something like 5, I expect 5 to be printed
x was passing by value, scanf changed the local copy of x. You need to return user input instead:
int input();
int main (void)
{
int h;
h = input();
printf("%d\n", h);
}
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
This is because parameters in C are passed by copy. So the x that you modify is just a copy of what you passed, and at the end of input, that copy is discarded. Instead, you could pass a pointer. Since scanf already operates on a pointer, the fix is simple:
void input(int *x); // parameter is now a pointer to an int
int main(void)
{
int h = 0;
input(&h); // pass the address instead of the value
printf("%d\n", h);
}
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x); // no need to take the address again
}
Any decent book, tutorial or class should have mentioned that arguments in C are passed by value. That means the value is copied into the local argument variable, and any modifications to that local variable won't reflect on the original variable.
There are two possible solutions:
The simple one: Return the value from the function
int input(void)
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
...
int h = input();
printf("%d\n", h);
The complicated solution: Emulate pass by reference
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x);
}
...
int h;
input(&h); // Pass a pointer to the variable
printf("%d\n", h);
You call input(h); and then void input(int x) { ... }. The value of h is copied into x
If you want to get back the changed value, you can either :
return a value from the function input() :
// notice the int before function name
int input();
int main (void)
{
int h = 0;
// notice h = ...
h = input(h);
printf("%d\n", h);
}
// notice the int before function name
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
// notice the return
return x;
}
Or, pass the variable as reference (pointer) :
// notice int *
void input(int *x);
int main (void)
{
int h = 0;
// notice the &h this is used to pass addresses of variables
input(&h);
printf("%d\n", h);
}
// notice int *
void input(int *x)
{
printf("Enter a number: ");
// notice the & removed
scanf("%d", x);
}
The problem is that you didn't understand the basic workflow of functions. Maybe you should once again revise your basics and theory.
This is how your code should have been written (I have tried my best to make minimal changes to your code):
#include <stdio.h>
int input();
int main (void)
{
int y;
y = input();
printf("%d\n", y);
}
int input()
{
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
You should have set the return type of function input() as int. Then, a variable (y in this case) could get the value returned by input() and use it in the main() function to display the actual input that the user had given.

Call and Return function using pointers locally in c

Hello there I am learning pointers so I created a calculator.
I managed to return the value and the pointers from a function but by declaring them globally. How can I declare them locally?
#include <stdio.h>
#include <stdlib.h>
Declaration of all functions
int Addition();
int Subtraction();
int Devision();
int Multiplication();
Declaration of Global Variables, which I would like to declare them locally
int p;
int n;
int *r=&n;
int *b=&p;
Start of main Function
int main()
{
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
The user chooses an Arithmetic operation (function) by inputting one number(1,2,3,4 or 0 for Exit)
if (g==1)
{
s=Addition(r,b);
printf("The addition result is %d+%d=%d",*r, *b, s);
}
else if (g==2)
{
s=Subtraction(r,b);
printf("The Subtraction result is %d-%d=%d",*r, *b, s);
}
else if (g==3)
{
s=Devision(r,b);
printf("The Devision result is %d/%d=%d",*r, *b, s);
}
else if (g==4)
{
Multiplication(r,b);
printf("The Multiplication result is %d/%d=%d",*r, *b, s);
}
else
{
break;
}
return 0;
}
}
End of main function.
Below are all the other functions
Addition()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n+p;
return x;
}
Subtraction()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n-p;
return x;
}
Devision()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n / p;
return x;
}
Multiplication()
{
int x;
printf("Ennter first nr: ");
scanf("%d",&n);
printf("Ennter second nr: ");
scanf("%d",&p);
x=n * p;
return x;
}
1) Refresh the knowledge about pointers and pass parameters via pointers if you want to.
The indirection or dereference operator * gives the contents of an object pointed to by a pointer.
The unary or monadic operator & gives the address of a variable.
2) Avoid global variables declare them locally after main.
3) Use switch instead of if-else chains
#include <stdio.h>
#include <stdlib.h>
int Addition(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x = *n + *p;
return x;
}
int Subtraction(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x = *n - *p;
return x;
}
int main(void)
{
int n1 = 0;
int n2 = 0;
int *r = &n1;
int *b = &n2;
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
switch(g)
{
case 0:
printf("END\n");
return 0;
break;
case 1:
s = Addition(r, b);
printf("The addition result is: %d+%d=%d\n\n",n1, n2, s);
break;
case 2:
s = Subtraction(r ,b);
printf("The Subtraction result is: %d-%d=%d\n\n",*r, *b, s);
break;
}
}
return 0;
}
Test:
Please choose the Arithmetic operation:
Addition-> 1
Subtraction-> 2
Devision-> 3
Multiplication-> 4
Exit-> 0
2
Ennter first nr: 5
Ennter second nr: 3
The Subtraction result is: 5-3=2
Please choose the Arithmetic operation:
Addition-> 1
Subtraction-> 2
Devision-> 3
Multiplication-> 4
Exit-> 0
0
END
I used them Globally because it was easier for me to call and read them. That is why I want to learn how can I declare them locally.
Very simply, just declare them locally, as shown: (without your user input code to focus on your primary question)
int main(void)
{ // locally declared
double a = 4.5;
double b = 10.8;
double result_1, result_2, result_3, result_4;
result_1 = add(a, b);
result_2 = sub(a, b);
result_3 = mul(a, b);
result_4 = div(a, b);
return 0;
}
// example function (the others will be of similar form,
// a single return line with the appropriate math operator.
double add(double a, double b)//no pointers necessary
{
return a + b;
}
If you really want to use a pointer, then you can implement the function to return the result via an argument:
void add(double a, double b, double *result)
{
*result = a + b;
}
Usage: (call from main for example)
add(a, b, &result_1);// passing the address of result_1
// to allow the value at that address
// to be modified.
Well, not sure if this is what you are asking for, but let me suggest you this code:
#include <stdio.h>
#include <stdlib.h>
int Addition(int* n, int* p)
{
int x;
printf("Ennter first nr: ");
scanf("%d",n);
printf("Ennter second nr: ");
scanf("%d",p);
x=*n+*p;
return x;
}
int main()
{
int n1=0;
int n2=0;
int *r=&n1;
int *b=&n2;
int g,s;
while (1)
{
printf("Please choose the Arithmetic operation: \n");
printf("Addition-> 1 \nSubtraction-> 2 \nDevision-> 3 \nMultiplication-> 4 \nExit-> 0\n ");
scanf("%d",&g);
if (g==1)
{
s=Addition(r,b);
printf("The addition result is %d+%d=%d",*r, *b, s);
}
return 0;
}
}
As you can see, we are using pointers to store the value of the number written by the user, so you can keep them after calling the function.

I keep getting the message loadParameters.exe has triggered a breakpoint?

This is my assignment I am supposed to do however whenever I run my code I get the error stated in my question.
Create a function,
return type: char
parameters: int *, int *
Inside the function, ask for two integers. Set the user responses to the int * parameters.
Prompt the user for a character.
Get their response as a single character and return that from the function.
In main:
Define two variables and call your function.
Print the result of your function and the values of your variables.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
char load(int *x, int *y);
void main()
{
int integer1 = 0;
int integer2 = 0;
int *pointer1;
int *pointer2;
pointer1 = &integer1;
pointer2 = &integer2;
char returnValue;
returnValue = load(*pointer1, *pointer2);
printf("%c", returnValue);
system("pause");
}
char load(int *x, int *y)
{
char input;
printf("Please enter two integers: ");
scanf_s("%d %d", x, y);
printf("Please enter a character:");
scanf_s(" %c", &input);
return input;
}

Some code of pointer

The code need to send to the function an int must to send just int and show the changes after the function. so I tried this:
int main(void)
{
int number = 0;
printf("Please enter some number: \n");
scanf("%d", &number);
printf("the number that you entered: %d\n", number);
inc(number);
printf("After the 'inc' function, your number is: %d\n", number);
system("PAUSE");
return 0;
}
/*
*/
void inc(int x)
{
int* px = &x;
*px= *px+1;
}
it's prints just the same number and doesn't change at all. help?
you must pass the parameter as a pointer
void inc(int *px)
{
*px= *px+1;
}
in your code you are just modifying the functions local copy of x
A hacktacularly terrible hackaround:
inc((int)&number);
// ...
void inc(int n)
{
++*(int*)n;
}
Don't do this. It won't work on all but certain archaic architectures.

Resources