Thread: sorting with pointers

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07: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, 06: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, 11:21 PM

Tags for this Thread