Pointer Problems

This is a discussion on Pointer Problems within the C Programming forums, part of the General Programming Boards category; I am trying to implement bubble sort for a class assignment. They are trying to teach us how to use ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Pointer Problems

    I am trying to implement bubble sort for a class assignment. They are trying to teach us how to use pointers. The only problem is it is an online course and I don't quite get the pointer thing. I'm having trouble with the swap function and getting it the right pointer level to be passed. Please help!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 10
    
    void bubbleSort(char * const array[], const int size);
    
    int main()
    {
            char *a[SIZE] = {"Mahmuda", "Tim", "Andrew", "Jacob", "Vasant", "Kenneth", "Kendall", "John", "Vijayakumar", "Marion"};
    
            int i;
    
            printf("Data items in original order\n");
    
            //loop through array a
            for(i = 0; i < SIZE; i++)
            {
                    printf("%s ", *(a + i));
            }
    
            printf("\nData items in ascending order\n");
    
            bubbleSort(a, SIZE);
    
            for(i = 0; i < SIZE; i++)
            {
                    printf("%s ", *(a + i));
            }
            printf("\n");
            return 0;
    }
    
    void bubbleSort(char * const array[], const int size)
    {
            void swap(char **ptrElement1, char **ptrElement2);
            int stringGreaterThan(char string1[], char string2[]);
            int pass;
            int j;
    
            for(pass = 0; pass < size - 1; pass++)
            {
                    for(j = 0; j < size - 1; j++)
                    {
    
                            if(strcmp(*(array + j), *(array + j + 1)) > 0)
                            {
                                    swap(*(array + j), *(array + j + 1));
                            }
                    }
            }
    }
    
    void swap(char **ptrElement1, char **ptrElement2)
    {
          int hold = *ptrElement1;
          *ptrElement1 = *ptrElement2;
          *ptrElement2 = hold;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Pretty close
    Code:
    void bubbleSort(char * const array[], const int size)
    {
            void swap(char **ptrElement1, char **ptrElement2);
            int stringGreaterThan(char string1[], char string2[]);
            int pass;
            int j;
    
            for(pass = 0; pass < size - 1; pass++)
            {
                    for(j = 0; j < size - 1; j++)
                    {
    
                            if(strcmp(*(array + j), *(array + j + 1)) > 0)
                            {
                                    /* dropped a * from these */
                                    swap((array + j), (array + j + 1));
                            }
                    }
            }
    }
    
    void swap(char **ptrElement1, char **ptrElement2)
    {
          /* fixed the type */
          /*int hold*/ char *hold = *ptrElement1;
          *ptrElement1 = *ptrElement2;
          *ptrElement2 = hold;
    }
    
    
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `bubbleSort':
    foo.c:47: warning: passing arg 1 of `swap' discards qualifiers from pointer target type
    foo.c:47: warning: passing arg 2 of `swap' discards qualifiers from pointer target type
    
    $ ./a.exe
    Data items in original order
    Mahmuda Tim Andrew Jacob Vasant Kenneth Kendall John Vijayakumar Marion
    Data items in ascending order
    Andrew Jacob John Kendall Kenneth Mahmuda Marion Tim Vasant Vijayakumar
    You might want to reconsider your use of 'const'.
    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
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Thank you for your help. I appreciate it.
    Last edited by tbarsness; 11-09-2006 at 04:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM

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