sorting with pointers

This is a discussion on sorting with pointers within the C Programming forums, part of the General Programming Boards category; I am trying to perform bubble sort on a memory that contains a certain amount of structures of the same ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    sorting with pointers

    I am trying to perform bubble sort on a memory that contains a certain amount of structures of the same type.I am a little bit confused with the pointer logic.
    For e.g i have a list with 12 offices.
    Code:
    void sortList(struct child *p,int noofchildren){
        struct man *out;
        struct man *in;
        struct man *temp;
        	 for(out= 12 * sizeof(struct man);out>1;out--){
        		 for(in=p;in<out;in++){
        			 if(strcmp(p->name,p->name+1)==1){
        				 temp=p;
        				 p=p+1;
        				 (*p)+1=temp;
        			 }
                             else
                                 if(strcmp(p->name,p->name+1)==1){
                                     if(p->age>p->age+1){
                                         temp=p;
                                         p=p+1;
                                         *(p)+1=temp;
                                     }
                                 }
        				
        		 }
        	 }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Please indent your code properly. I suggest that you write a function to compare two struct child elements, similiar to how you would write a function to compare two struct child elements such that you can use qsort to sort an array of struct child objects. This would clarify your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Please indent your code properly. I suggest that you write a function to compare two struct child elements, similiar to how you would write a function to compare two struct child elements such that you can use qsort to sort an array of struct child objects. This would clarify your code.
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting STL Vector of Pointers (Split)
    By Giules in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2010, 06:35 AM
  2. Sorting list of shared pointers
    By Elysia in forum C++ Programming
    Replies: 10
    Last Post: 10-23-2010, 03:03 PM
  3. Sorting an STL Vector of pointers
    By Beamu in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2009, 05:25 AM
  4. Sorting using Pointers
    By aspand in forum C Programming
    Replies: 3
    Last Post: 04-26-2002, 05:53 PM
  5. sorting with pointers
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-17-2002, 10:21 PM

Tags for this Thread


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