f(&a) is possible to run in C? - c

The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0

An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.

Related

why is my function outputting incorrectly?

I created a factorial function and my output for integer b is incorrect when it is set at 5, any ideas as to why? b should be equal to the integer 120 and I am getting the number -95449088 after I compile and run it.
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Here you are using the same x value in the condition check of for-loop which is a mess up. Instead you can store the variable x to a variable temp and use this temp variable for checking the condition.
Please see below the corrected code
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i,temp=0;
temp = x; //store the x to temp
for(i=1; i < temp; i++){
x *= i;
}
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Other have explained that you should avoid mixing input and output variables. This is good advice, and as a beginner you should try to observe it.
But this is a special case, and here you can re-use the input value, provided you use a decreasing loop:
int factorial(int x)
{
int i;
for (i = x-1; i >1; i--)
x *= i;
return x;
}
It works because it implicitly initializes the return value with x and then multiplies it by all numbers below it, which is a possible definition for the factorial.

Pointer to pointer as a function parameter

I have the following code:
#include <stdio.h>
#include <stdlib.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
int main(void) {
int c = 4;
printf("f(): %d\n", f(c, &c, &&c));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
How can I access **ppz correctly, because so I get an error message: "label 'c' used, but not defined".
An int** is a pointer to an int*. You need to create a variable of type int* to be able to pass a pointer to it somewhere. Here is what you should do:
int main(void) {
int c = 4;
int* pc = &c;
printf("f(): %d\n", f(c, pc, &pc));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
Refer #ikegami's answer for an explanation of the proper use of a pointer to a pointer.
You want to modify a variable of type int, so the parameter should be int *, not int **.
You'd use int ** if you wanted to modify a variable of type int * variable. That's not the case here.
For example,
void f(int **pp) {
*pp = malloc(10);
}
int main(void) {
int *p;
f(&p);
// ...
free(p);
}

Function to increment the value of a variable in C

I wrote the same code in PHP, the value of 5 got incremented, but
why doesn't the value get incremented in C?
int foo(int x){
x++;
}
int main( ){
int y = 5;
foo(y);
printf("value of y = %d\n", y);
}
In C passing arguments is done by value rather than by reference. That is, the number that the function is working with is unique to the one passed to and has its own space in memory. To get around this do
int foo(int* x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
You should pass the parameter by reference to function foo.
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Because x, y are local variables to their functions. The scope of the variable lies within the block of the function. So you can't change the value of those variables from outside the block.
Solution:
Either you declare those variables as global variables
int y;
int foo(){
y++;
}
int main( ){
y = 5;
foo();
printf("value of y = %d\n", y);
}
Or using reference you can do that
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Might be in PHP it is directly pass-by-reference. But C has different function calls.
If you want to increment the value in the function and it should reflect in the main function. There are two possible ways :
1.
int foo(int x)
{
return ++x; //returning the incremented value
}
2.
void foo(int *x)
{
++(*x);//this function is pass-by-reference.
}
Please let me know if there are any issue.
What you could also be doing if you want to avoid using pointers is as below
int addone(int n);
int main(void) {
int n=0;
printf("Before: %d\n", n);
n=addone(n);//assign the returned value from function to the variable 'n'
printf("After: %d\n", n);
return 0;
}
int addone(int n) {
n++;
return n;
}

Convert a printed number using printf as a variable

I have
int main ()
{
int x=69057;
int y=23
printf("%d", x);
printf("%d", y);
return 0;
}
And it prints 6905723. How can I convert the printed number into an integer? I can't do
int z=6905723
since in the original program I don't know what value x and y have.
What you essentially want to do here is multiply x by the smallest power of 10 that's larger than y, and then add y to it.
Assuming x and y are small enough to not overflow the long result, you could do something like this:
int x = 69057;
int y = 23
int temp = y;
long result = x;
while (temp > 0) {
result *= 10;
temp /= 10;
}
result += y;
printf("%ld\n", result);
Here's how you can get the answer into int z. It works even if you don't know x and y before. You just need to make sure that the answer will actually fit into an int.
int main ()
{
int x=69057;
int y=23
char buff[512];
sprintf(buff, "%d%d", x, y);
int z = atoi(buff); /* Now z is equal to 6905723 */
return 0;
}
You will run into a problem if y is a negative number, but it isn't clear from your question what you'd like to happen in that situation.
try this. very easy and simple
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int x,y,z,a,b;
x=69057;
y=23;
printf("x=%d y=%d\n",x,y);
a = log10(y) + 1;
for (b=0;b<a;b++)
{
x*=10;
}
z=x+y;
printf("z=%d",z);
return 0;
}
You can write a function to do it for you:
#include <stdio.h>
int my_function(int x, int y);
int main()
{
int x = 69057;
int y = 23;
int z = my_function(x, y);
printf("%d\n", z);
return 0;
}
int my_function(int x, int y)
{
int tmp = y;
do {
x *= 10;
} while (tmp /= 10);
return x + y;
}

Can you really redefine keywords in C language?

Can you explain the code below? How we can use #define for a keyword of C?
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
Output:
10 5
This #define int int* is a preprocessor macro. If you want to define your own synonyms for types then use typedef. You cannot create keywords in a language you did not create.
Sample:
#include <stdio.h>
typedef int * myIntPtr;
int main(void) {
int i = 10;
myIntPtr x = &i;
printf("%d", *x);
return 0;
}
Output:
10
Also semantically making an int to an int * makes no sense.
#define a b
will change all "a" to "b" in your source after the #define
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
will change to
int main(void) {
int **p;
int *q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}

Resources