Why my pointer code is giving me wrong output in C? - c

Why my pointer code is giving me wrong output ?.
Where, my actual code is :
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", x, y);
}
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}
My expected output is like this : 6 7 6 7
It's giving me output like this : 6 7 50 100
Thanks.

In fun1 the expression &x is a pointer to the pointer. It's of type int **.
You should not use the address-of operator there, since x and y already are pointers.
An easy way to get the compiler to warn you about this is to declare thje function fun2 before you use it:
// Declare fun2 prototype
void fun2(int *x, int *y);
void fun1(int *x, int *y)
{
...
}
// Define fun2 implementation
void fun2(int *x, int *y)
{
...
}

Before use void fun2(int*, int*), you must declare or define it
In funxtion fun1, the line printf("%d %d ", x, y); should be printf("%d %d ", *x, *y);, for x and y are int*
In function fun1, the line fun2(&x, &y); should be fun2(x, y); for x and y are int*
The following code could work:
#include <stdio.h>
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(x, y);
printf("%d %d\n", *x, *y);
}
int main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d\n",x,y);
return 0;
}

Method 1. Point To Point To Int
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", *x, *y);
}
void fun2(int **x, int **y){
**x = 6;
**y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}

Related

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);
}

Swap in C - Printing

I just implemented my swap function but it does not print anything. Do you know why does the line printf does not execute?
#include <stdio.h>
int swap(int x, int y) {
scanf("%d", &x);
printf("%d, x is",x);
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
int main() {
swap(6,5);
}
You should not take user input inside the swap function. Its purpose should be to swap two integers only. You can move the scanf statements to main function.
#include <stdio.h>
int swap(int x, int y){
int temp = x;
x = y;
y = temp;
printf("After Swapping in swap function: x = %d, y = %d", x, y);
return 0;
}
int main(void){
int x, y;
scanf("%d", &x);
printf("%d, x is", x);
scanf("%d", &y);
printf("%d, y is", y);
swap(x, y);
printf("After Swapping in main function: x = %d, y = %d", x, y);
}
But the above code has a major issue. Though the swap function prints the integers passed as they are swapped but the fact is x and y in the main remains unaffected.
In this case to make it work, using pointers would be helpful
void swap(int *ptrx, int *ptry){
int temp = *ptrx;
*ptrx = *ptry;
*ptry = temp;
}
In the main function call the swap as swap(&x, &y);
Use this code for swapping.
#include <stdio.h>
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("After Swapping: x = %d, y = %d", x, y);
}
int main()
{
swap(6,5);
return 0;
}
And I don't understand why you need to scan x & y

Using Pointers In C swapping three numbers

#include <stdio.h>
int main() {
int a, b,c;
/* Input a and b */
scanf("%d %d %d", &a, &b,&c);
while(a != -1) {
int *x = &a;
int *y = &b;
int *z = &c;
printf("Original inputs: a:%d\tb:%d\tc:%d\n", a, b,c);
reorder(a,b,c);
swap(a,b);
printf("Rearranged inputs: a:%d\tb:%d\tc:%d\n\n", a, b,c);
break;
}
}
void reorder(int *x, int *y, int *z){
if(*x > *y)
{
int temp = *x;
*x = *y;
*y = temp;
}else if(*y > *z){
int temp = *y;
*y = *z;
*z = temp;
}else if(*x > *z){
int temp = *x;
*x = *z;
*z = temp;
}
}
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
I am new to C and learning pointers am not sure how to implement pointers to swap 3 numbers in ascending order
This might give you a way to start:
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* x, int* y, int* z) {
if (*x > *y) {
swap(x, y);
}
if (*y > *z) {
swap(y, z);
}
if (*z > *x) {
swap(z, x);
}
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int *x = &a;
int *y = &b;
int *z = &c;
reorder(x, y, z);
}
You can use this code for your purpose :
#include <stdio.h>
void reorder(int *, int *, int *);
void swap(int *, int *);
void main()
{
int a, b, c;
printf("Enter three numbers : ");
while (scanf("%i %i %i", &a, &b, &c)==3)
{
reorder(&a, &b, &c);
printf("Now a is %d, b is %d and c is %d.\n\n", a, b, c);
printf("Enter three numbers : ");
}
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* a, int* b, int* c) {
if (*c<*a&&*c<*b)
swap(a, c);
if (*b<*a&&*b<*c)
swap(a, b);
if (*c<*b)
swap(b, c);
}

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;
}

C: How to return separate multiple values from function to main

Is there any possibility to return multiple values from function to main so that i can use separately in main function. I need to use this concept in a project I am working on. As that code is huge, I am giving a simple code showing my requirements.
#include <stdio.h>
int func ()
{
int a[3] = { 31, 32, 33};
static int x, y, z;
char b[20];
x = a[0];
y = a[1];
z = a[2];
printf ("%d\n", x);
printf ("%d\n", y);
printf ("%d\n", z);
return 0;
}
int main()
{
int x, y, z ;
func ();
printf ("%d\n", x);
printf ("%d\n", y);
printf ("%d\n", z);
return 0;
}
I invite multiple solutions, but please do explain your concept with proper code. Appreciate your time
You can use structures.
#include <stdio.h>
struct data_t {
int x, y, z;
};
struct data_t func (void)
{
int a[3] = { 31, 32, 33};
struct data_t data;
data.x = a[0];
data.y = a[1];
data.z = a[2];
printf ("%d\n", data.x);
printf ("%d\n", data.y);
printf ("%d\n", data.z);
return data;
}
int main(void)
{
struct data_t data;
data = func ();
printf ("%d\n", data.x);
printf ("%d\n", data.y);
printf ("%d\n", data.z);
return 0;
}
Alternative way using pointers:
#include <stdio.h>
void func (int* x, int* y, int* z)
{
int a[3] = { 31, 32, 33};
*x = a[0];
*y = a[1];
*z = a[2];
printf ("%d\n", *x);
printf ("%d\n", *y);
printf ("%d\n", *z);
}
int main(void)
{
int x, y, z;
func (&x, &y, &z);
printf ("%d\n", x);
printf ("%d\n", y);
printf ("%d\n", z);
return 0;
}
Briefly...You can do that by many methods below which is described:-
1) Using global variable...but not a good practice i would say as the above cmnt...
2)Using and passing variables in argument by reference (i.e. call by reference) . EX:
void f(int *p , int *q)
3)having the return type as array ... or structure or union . Ex:
temp f(<the parameters here>);
4)having return type as a array would be useful when you are dealing with homogeneous data types even better than using struct...since using array is more easy then using struct...the main advantage... we could iterate through them if needed using an counter variable in a loop...and also can do easy data manipulation of your returned datas...
I know C does not allow directly returning of arrays...for that you can do like the following "sample code" i m posting down here
`
#include <stdio.h>
/* function to generate and return random numbers */
int * getRandom( ) {
static int r[10];
int i;
/* set the seed */
srand( (unsigned)time( NULL ) );
for ( i = 0; i < 10; ++i) {
r[i] = rand();
printf( "r[%d] = %d\n", i, r[i]);
}
return r;
}
/* main function to call above defined function */
int main () {
/* a pointer to an int */
int *p;
int i;
p = getRandom(); // this is the line where i want your attention to be
for ( i = 0; i < 10; i++ ) {
printf( "*(p + %d) : %d\n", i, *(p + i));
}
return 0;
}
`
5)Using Hybrid mixture of them...like returning a pointer of a structure...passing argument as call by reference for a struct data type...
int *func()
int a[3] = {31, 32, 33};
int x,y,z;
x = a[0];
y = a[1];
z = a[2];
printf ("%d\n", x);
printf ("%d\n", y);
printf ("%d\n", z);
return a;
}
int main()
{
int x, y, z;
int b[3];
b=func ();
printf ("x:%d\n", b[0]);
printf ("y:%d\n", b[1]);
printf ("z:%d\n", b[2]);
return 0;
}
I have not compiled this but I think it might help you.

Resources