You are not logged in. Please login at www.codechef.com to post your questions!

×

Pointers in C.

What does the below C program mean?

void swap(int x,int y)
{
    int t;
    t = x;
    x = y;
    y = t;
}

This question is marked "community wiki".

asked 16 Sep '16, 22:30

mr_coder_5's gravatar image

0★mr_coder_5
511
accept rate: 0%

There is a star before each x & y.

(16 Sep '16, 22:34) mr_coder_50★

This will swap x and y but for parameters x and y (call by value) and this changes will not be reflected in the caller function. If you want it to be reflected call it by reference.

Call by Value

If data is passed by value, the data is copied from the variable used in for example main() to a variable used by the function. So if the data passed (that is stored in the function variable) is modified inside the function, the value is only changed in the variable used inside the function. Let’s take a look at a call by value example:

include <stdio.h>

void call_by_value(int x) { printf("Inside call_by_value x = %d before adding 10.\n", x); x += 10; printf("Inside call_by_value x = %d after adding 10.\n", x); }

int main() { int a=10;

printf("a = %d before function call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function call_by_value.\n", a);
return 0;

}

The output of this call by value code example will look like this:

a = 10 before function call_by_value. Inside call_by_value x = 10 before adding 10. Inside call_by_value x = 20 after adding 10. a = 10 after function call_by_value.

Ok, let’s take a look at what is happening in this call-by-value source code example. In the main() we create a integer that has the value of 10. We print some information at every stage, beginning by printing our variable a. Then function call_by_value is called and we input the variable a. This variable (a) is then copied to the function variable x. In the function we add 10 to x (and also call some print statements). Then when the next statement is called in main() the value of variable a is printed. We can see that the value of variable a isn’t changed by the call of the function call_by_value().

Call by Reference

If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:

include <stdio.h>

void call_by_reference(int y) { printf("Inside call_by_reference y = %d before adding 10.\n", y); (y) += 10; printf("Inside call_by_reference y = %d after adding 10.\n", y); }

int main() { int b=10;

printf("b = %d before function call_by_reference.\n", b);
call_by_reference(&b);
printf("b = %d after function call_by_reference.\n", b);

return 0;

}

The output of this call by reference source code example will look like this:

b = 10 before function call_by_reference. Inside call_by_reference y = 10 before adding 10. Inside call_by_reference y = 20 after adding 10. b = 20 after function call_by_reference.

Let’s explain what is happening in this source code example. We start with an integer b that has the value 10. The function call_by_reference() is called and the address of the variable b is passed to this function. Inside the function there is some before and after print statement done and there is 10 added to the value at the memory pointed by y. Therefore at the end of the function the value is 20. Then in main() we again print the variable b and as you can see the value is changed (as expected) to 20.

source- https://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference

link

answered 02 Oct '16, 15:59

smsubham's gravatar image

3★smsubham
675216
accept rate: 15%

It just swaps the values of a and b but there is no use as they are swaped local to that function if you want to swap them in the main function also then you should pass there address using pointers or you should pass there references in c++

link

answered 17 Sep '16, 12:18

srihari_v's gravatar image

2★srihari_v
111
accept rate: 0%

edited 17 Sep '16, 12:19

If you are facing markdown problems:

You should try using escape character \ before typing any other special character. This will stop/escape the normal functioning of that special symbol.(i.e. to print the desired thing using pointers write \*t=\*x on a comment)

PS: To copy paste a code in a post use 101010 formatting in formatting toolbar.This will take care of all necessities/symbols in a code.

About your function: here is a basic-prototype to swap two numbers.

If a function is used to swap numbers then that function should receive address of both numbers(call by address) otherwise it's like making changes in a photocopy (call by value).

#include <stdio.h>
void swap(int *x,int *y)
{
    int t;
    t = *x;
    *x = *y;
    *y = t;
}
int main(void) {
    int x,y;
    x=5;
    y=6;

swap(&x,&y);

    printf("%d %d",x,y);
    return 0;
}
link
This answer is marked "community wiki".

answered 17 Sep '16, 12:09

agrocks23's gravatar image

2★agrocks23
31617
accept rate: 4%

if you want swapping in pointers we have to do this way i.e using there references whtat you did is writing the code with out using pointers.I think you have learn more about pointers in function passing refer below url for more information http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work http://www.cprogramming.com/tutorial/function-pointers.html

void swap(int x,int y) { int t;

t=*x;

x=y;

*y=temp; }

link

answered 17 Sep '16, 00:55

saimurali333's gravatar image

2★saimurali333
11
accept rate: 0%

edited 17 Sep '16, 01:04

This function should be called as swap(&x,&y). As pointers store the address of the variable they point. Getting inside the function, t will store the value stores at &x,address &x stores value stored at &y and finally &y will store t.

link

answered 17 Sep '16, 00:41

garv_sin's gravatar image

0★garv_sin
1
accept rate: 0%

The program swaps(interchanges) the values of x and y (If there is a star, like you have mentioned), does nothing otherwise.

link

answered 17 Sep '16, 00:01

hemanth_1's gravatar image

6★hemanth_1
1.4k11
accept rate: 28%

this is meaningless programme. to swap two variable, you have to pass their references not values.

link
This answer is marked "community wiki".

answered 16 Sep '16, 22:33

gandu007's gravatar image

5★gandu007
1817
accept rate: 11%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×62

question asked: 16 Sep '16, 22:30

question was seen: 1,425 times

last updated: 02 Oct '16, 15:59