5

Is it compulsory to initialize t in the following code, before assigning value to t? Is the code correct?

void swap(int *x, int *y)
{
    int *t;
    *t = *x;
    *x = *y;
    *y = *t;
}
Palec
  • 12,743
  • 8
  • 69
  • 138
Baali
  • 83
  • 3
  • You should possibly reword your question a bit. There are some seemingly contradicting replies below, though all are correct. _Assigning_ a value to an uninitialized pointer is entirely legitimate, _dereferencing_ it is not. Your code snippet dereferences, but your question is about assignment. – Damon Mar 23 '11 at 10:43
  • Side comment, there is a standard defined `swap` function (well, template), but that has different semantics from what you are doing here. Are you sure that you want to confuse users by changing the semantics? (With your implementation: `swap(p,q)` is equivalent to `std::swap( *p, *q )` but not `std::swap( p, q )` the latter would swap the pointers, rather than the contents) – David Rodríguez - dribeas Mar 23 '11 at 11:10
  • i know that we can use a simple variable to store the value pointed by x , and then swapping is done . But i am just experimenting whether, can we use a pointer, instead of simple variable to store the value . – Baali Mar 24 '11 at 05:40
  • and this code works fine , if i initialize the pointer to 0. – Baali Mar 24 '11 at 05:40
  • i.e., int *t=0; *t=*x; *x=*y; *y=*t; – Baali Mar 24 '11 at 05:41

7 Answers7

9

You don't need pointer to begin with:

void swap(int *x,int *y)
{
    int t; //not a pointer!
    t=*x;
    *x=*y;
    *y=t;
}
int a = 10, b = 20;
swap( &a, &b); //<-----------note : Needed &

--

Or maybe, you want the following swap function:

void swap(int & x,int & y) //parameters are references now!
{
    int t; //not a pointer!
    t=x;
    x=y;
    y=t;
}
int a = 10, b = 20;
swap(a,b); //<----------- Note: Not needed & anymore!
Nawaz
  • 353,942
  • 115
  • 666
  • 851
8

is the following section of code correct?

Nopes! Your code invokes Undefined behaviour because you are trying to dereference a wild pointer.

 int *t;
 *t=*x; // bad

Try this rather

 int t; // a pointer is not needed here
 t=*x; 

or this

int *t = x; // initialize the pointer

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
5

That code contains undefined behavior:

int *t;
*t=*x; // where will the value be copied?

Besides that it makes no sense - you need a temporary variable to store the value, not the pointer.

int t; // not a pointer
t=*x;
*x=*y;
*y=t;
sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

It's correct for a pointer.

Only references need to be initialized upon declaration (or in a constructor for instance members).

EDIT: but you got errors in your code, you shouldn't be dereferencing your parameters (ie int *ptr = otherPtr; is fine, not int *ptr = *otherPtr;)

jv42
  • 8,521
  • 5
  • 40
  • 64
1

If you just want to make your pointer point to already-initialized data, then you don't need to initialize it. The way you do it, though, yes, you want to use one of the malloc functions to allocate enough heap space for an integer.

The proper, efficient way, to do swapping in C/C++ is

void swap(int *x, int *y) {
    int *t = x;
    x = y;
    y = t;
}
michel-slm
  • 9,438
  • 3
  • 32
  • 31
  • That's how I'd do it, too. I try to never leave a pointer uninitialized, and if I already have another pointer that I want to assign to it, then I just initialize to that value anyway. – Mephane Mar 23 '11 at 13:20
1

You can find the right way of doing this here

#include <stdio.h>

void swap(int *i, int *j)
{
    int t;
    t = *i;
    *i = *j;
    *j = t;
}

Basically the reason has been explained to you by sharptooth but there you will find some more details and explanations about what happens in background when you do such a swap. Hope it helps to clear your ideas.

0
int *t;
*t=*x;

t is not pointing to any valid location to be able to dereference.

is it compulsory to initialize , before assigning value to pointer t.

Yes, initializing / assigning to point to a valid memory location. Else where would it point to. It might some point to garbage and lead to undefined behavior on dereferencing.

Mahesh
  • 34,573
  • 20
  • 89
  • 115