Thread: Need help understanding swapping of integers

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32

    Need help understanding swapping of integers

    Edit: Apologies, thread should have been named as "Need help understanding swapping of characters"

    Hello All,

    I found a program on the internet that gets all possible combinations of a given string:

    The code is as follows:

    Code:
    # include <stdio.h>
     
    /* Function to swap values at two pointers */
    void swap (char *x, char *y)
    {
        char temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
      
    /* Function to print permutations of string
       This function takes three parameters:
       1. String
       2. Starting index of the string
       3. Ending index of the string. */
    void permute(char *a, int i, int n)
    {
       int j;
       if (i == n)
         printf("%s\n", a);
       else
       {
            for (j = i; j <= n; j++)
           {
              swap((a+i), (a+j));
              permute(a, i+1, n);
              swap((a+i), (a+j)); //backtrack
           }
       }
    }
     
    /* Driver program to test above functions */
    int main()
    {
       char a[] = "ABC"; 
       permute(a, 0, 2);
       getchar();
       return 0;
    }
    I don't understand what does swap((a+i), (a+j)); do?
    'a' is supposed to be string and we add an integer to it? Is this correct? What result does this produce?

    To understand (a+i) I wrote a small code as follows:
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void)
    {
        char a[] = "ABC";
        printf("%s", a);
        a+3;
        printf("\n\n%s", a);
        getch();
        return 0;
    }
    Both the print statements give ABC as the output. Could some one please clarify?

    Thanks in advance.
    Last edited by Shyam Raj; 10-13-2012 at 06:59 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If a is a pointer (or an array) and i is an integer, then (a + i) is equivalent to (or a shorthand for) &a[i] - the address if the i'th element of a. This is true for all types in C (char, int, structs, float, double, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about

    Code:
        char a[] = "ABC";
        printf("%s\n", a);
        swap(a+0,a+2);
        printf("%s\n", a);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    India
    Posts
    32
    Quote Originally Posted by grumpy View Post
    If a is a pointer (or an array) and i is an integer, then (a + i) is equivalent to (or a shorthand for) &a[i] - the address if the i'th element of a. This is true for all types in C (char, int, structs, float, double, etc).
    Thanks grumpy, I understood that now...

    And thanks Salem as well .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  2. swapping help
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 10:38 AM
  3. Replies: 8
    Last Post: 10-24-2005, 11:26 AM
  4. swapping chars
    By Markallen85 in forum C Programming
    Replies: 7
    Last Post: 12-24-2002, 04:38 AM
  5. swapping
    By metallicaau in forum C Programming
    Replies: 2
    Last Post: 10-08-2002, 08:17 AM