strswap. Explanation needed.

This is a discussion on strswap. Explanation needed. within the C Programming forums, part of the General Programming Boards category; Hi Everyone. I'm trying to understand pointers. I know a few things but this example really confuses me. Could you ...

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    strswap. Explanation needed.

    Hi Everyone. I'm trying to understand pointers. I know a few things but this example
    really confuses me. Could you explain me why strswap's parameters should be char **,
    or how it swaps a and b although the don't have the same size?
    Code:
    #include <stdio.h>
    #include <string.h>
    void strswap(char ** k,char ** l);
    
    int main()
    {
      char *a="Hello world";
      char *b="Good bye";
      printf("Before strswap a=%s , b=%s\n",a,b);
      strswap(&a,&b);
      printf("After strswap a=%s , b=%s\n",a,b);
    
    getchar();
    return 0;
    }
    
    /**********************************/
    void strswap(char** k,char** l)
    {
    char *temp;
    temp=*k;
    *k=*l;
    *l=temp;
    }
    Or why couldn't be like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    void strswap(char * k,char * l);
    
    int main()
    {
      char *a="Hello world";
      char *b="Good bye";
      printf("Before strswap a=%s , b=%s\n",a,b);
      strswap(a,b);
      printf("After strswap a=%s , b=%s\n",a,b);
    
    getchar();
    return 0;
    }
    
    /**********************************/
    void strswap(char* k,char* l)
    {
    char temp;
    temp=*k;
    *k=*l;
    *l=temp;
    }
    thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Try it with ints
    Compare
    void swap ( int *a, int *b );
    with
    void swap( int a, int b );

    It's all down to everything being passed by value, so if you have a T which needs swapping, you need a T* to be passed to swap.
    And since you start off with char*, this leads to needing char** in the function.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    I tried both of them in dev-c++. the first works fine while the other doesn't.
    Still don't get it... :-(

  4. #4
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Is k equal to &&a?
    Last edited by kantze; 10-21-2006 at 09:11 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,248
    Let's say you want to swap the values of two Things. For every type Thing, the swap procedure would look like this;
    Code:
    void Swap(Thing *k, Thing *l)
    {
        Thing temp;
        temp = *k;
        *k = *l;
        *l = temp;
    }
    A pointer to Thing needs to be passed to allow the values to be swapped.

    C-strings are the particular case where Thing is a pointer to char, or char *. If you want to swap strings, you need to pass a pointer to Thing (i.e. a pointer to pointer to char, which is a char **).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Understand the difference between char (a single character) and char* (a pointer to a character, which is often interpreted as the first in a string of characters, terminated by a character with the value 0) and you will understand why the function is written as it is written.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bit of explanation needed.
    By JacobN in forum C Programming
    Replies: 3
    Last Post: 11-24-2007, 04:41 PM
  2. Simple Explanation Needed
    By slowcoder in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 02:00 PM
  3. Explanation of a Compilation Error Needed
    By Zildjian in forum C Programming
    Replies: 2
    Last Post: 10-23-2003, 02:04 AM
  4. Explanation needed, what is this?
    By CAP in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 04:07 AM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21