Some code of pointer - c

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.

Related

Unexpected value by using printf (Call by reference in C)

#include <stdio.h>
int main()
{
int num1;
int *p;
p=&num1;
printf("Give a value\n");
scanf("%d", &num1);
printf("\n%d", num1);
f2(&num1);
printf("%d", *p);
return 0;
}
void f2(int *p)
{
*p *= *p;
}
A call by reference program just to return the square of a value
Well, the problem is that if I do not use printf the expected output is correct (e.g. 2*2=4)
However, if I include:
printf("\n%d", num1);
and run the programm I will take a non expected value (e.g. 2*2=24)
These two calls of printf result of outputting two values in the same line without a space.
printf("\n%d", num1);
f2(&num1);
printf("%d", *p);
If you want to make the output less confusing then for example write
printf("\n%d", num1);
f2(&num1);
printf("\n%d\n", *p);
There are two problems in your code.
You need to declare void f2(int* p) before using it. Depending on your platform you might get away with it. But a sane compiler should give you at least a warning (which should be considered as an error).
Sloppy format strings in your printfs make the output look wrong.
Try this:
#include <stdio.h>
void f2(int* p); // you need to declare this, otherwise you'll get a
// warning you should always conside as an error
int main()
{
int num1;
int* p;
p = &num1;
printf("Give a value\n");
scanf("%d", &num1);
printf("\nnum1 = %d\n", num1); // format string more explicit
f2(&num1); // warnig here if f2 is not declared as above
printf("*p = %d\n", *p); // format string more explicit
return 0;
}
void f2(int* p)
{
*p *= *p;
}
There are several problems with this code.
You need to either:
a. declare a prototype for f2. You can do this by putting the following code before your main
void f2(int *p);
b. put the entire f2 function before main (and this is the route that I'd probably choose - but questions of style are the cause of countless pointless wars.
The output is unclear. By not putting \n in your printf statement you're running the output of the two print statements together. Use this instead:
printf("\n%d\n", num1);
Pretty printing greatly improves readability. And don't be afraid of giving functions meaningful names.
On balance, I think I'd write your code like so:
#include <stdio.h>
void square(int *p) {
*p *= *p;
}
int main(int argc, char *argv[]) {
int num1;
int *p;
p = &num1;
printf("Give a value: ");
scanf("%d", &num1);
printf("\n%d squared is equal to: ", num1);
square(&num1);
printf("%d\n", *p);
return 0;
}
#include <stdio.h>
#include <math.h>
void f2 (int *p)
{
*p = pow(*p, 2); // equal with *p *= *p;
}
int main ()
{
int num1;
int *p; // p is pointer variable that points to num1
// type of num1 and p must be the same (int).
p = &num1; // p is address of num1
printf ("Give a value: ");
scanf ("%d", p); // p is address of num1
printf ("\nnum1 before: %d", *p); // *p is num1 (content of p)
f2 (p);
printf ("\nnum1 after: %d", *p); // missing \n in here
return 0;
}

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

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;

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.

Compare an int with double array

I'm trying to learn C and I want to make a program that compares the number I type with the numbers in my array. The only problem is that It doesn't actually do that. Even if I type a number that is from that array it shows that the number is not from that array.
#include <stdio.h>
void getMark(int findMark, double crswk1[]);
void changePartMark(double crswk1[], int findMark);
int main()
{
int findMark;
double crswk1[10]={67, 77, 80, 40};
getMark(findMark, crswk1);
changePartMark(crswk1, findMark);
}
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
void changePartMark(double crswk1[], int findMark)
{
int i;
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
The number you're reading in is never getting back to findMark in your main function.
void getMark(int findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
This function is saving a value in the local parameter findMark. Because all parameters in C are passed by value, changes to this local variable are not reflected in the caller, so findMark in main never changes.
You need to change this function to take the address of an `int
void getMark(int *findMark, double crswk1[])
{
printf("Enter the mark you want to change: ");
scanf("%d", findMark);
}
Then you call this function from main like this:
getMark(&findMark, crswk1);
By passing in the address of findMark, the function can write to that address.
Also, your changePartMark function doesn't search through the entire array. It only looks at index i. But even that is a problem because you never set i.
You need to loop through the array to check your value against each element in the array.
int i;
for (i=0; i<4 i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
Two main issues:
First, the number you enter is never passed back. When you write
void getMark(int findMark, double crswk1[]) {
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
}
then you read in the value in a local copy findMark, not in the one used by the caller. BTW: crswk1 is not used; So I'd suggest to write
int getMark() {
int findMark = 0;
printf("Enter the mark you want to change: ");
scanf("%d", &findMark);
return findMark;
}
Second, your void changePartMark(double crswk1[], int findMark) lacks a loop, and i is not initialized. Code could look like the following:
void changePartMark(double crswk1[], int findMark)
{
for (int i=0; i<4; i++) {
if(findMark == crswk1[i])
{
printf("It is equal");
}
else
{
printf("It is not equal");
}
}
}

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

Resources