Why does this sort when I do not use pointers?

This is a discussion on Why does this sort when I do not use pointers? within the C++ Programming forums, part of the General Programming Boards category; So all this program does is sort...at least some of the time. If you look at Version 1, I DO ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    32

    Question Why does this sort when I do not use pointers?

    So all this program does is sort...at least some of the time.

    If you look at Version 1, I DO NOT use a pointer in my structure as char data1. This version works just fine. Enter [q w e r t] and you get back [e q r t w]...it works as planned.

    With Version 2 I have changed char data1 to char* data1. With this version if you enter [q w e r t] it spits back [q w e r t]. I cannot tell if it is my function prototype or something else that is doing this.

    Any thoughts...I compile with Visual C++ 6.0?

    VERSION 1: (this one sorts)
    Code:
    #include<iostream.h>
    
    struct ds_node
    {
    	char data1;
    	ds_node* next;
    };
    
    class ds_class
    {
    	private:
    		ds_node* head;
    	public:
    		ds_class();
    		~ds_class();
    		void DisplayAll(ds_node*& head);
    		ds_node* SortedInsert(ds_node*& head, char data1);
    };
    
    
    //----------------------------------------------Constructor
    
    ds_class::ds_class()
    {
    	cout << "Constructor Called" << endl;
    	head = NULL;
    }
    
    //----------------------------------------------Destructor
    
    ds_class::~ds_class()
    {
    	cout << "Destructor Called" << endl;
    	delete head;
    	head = NULL;
    }
    
    //----------------------------------------------Display All Data
    
    void ds_class:: DisplayAll(ds_node*& head_ptr)
    {
    	while(head_ptr != NULL)
    	{
    		cout << head_ptr->data1 << endl;
    		cout << "----------------------------------------------" << endl;
    		head_ptr = head_ptr->next;
    	}
    	cout << endl;
    }
    
    //----------------------------------------------Sort / Insert Data
    
    ds_node* ds_class::SortedInsert(ds_node*& head, char data1)
    {
    	ds_node* newNode;
    
    	// populate the new node that we're adding.
    	newNode = new ds_node;
    	newNode->data1 = data1;
    	newNode->next = NULL;
    
    	// figure out where to add that node in
    	if((head == NULL) || (head->data1 >= newNode->data1))
    	{
    		// this element should go at the head of the list
    		newNode->next = head;
    		head = newNode;
    	}
    	else	// this is not the first element in the list
    	{
    		// create a pointer that will traverse the list to find
    		// where to insert the new element
    		ds_node* current;
    		current = head;
    
    		// move current down the list till we find where our new node is supposed to go
    		while((current->next != NULL) && (current->next->data1 < newNode->data1))
    		{
    			current = current->next;
    		}
    		newNode->next = current->next;
    		current->next = newNode;
    	}
    	return head;
    }
    
    //----------------------------------------------Client Program
    
    
    void main()
    {
    	ds_class ds;
    	ds_node* head  = NULL;
    	char     temp;
    	char     response;
    	
    	do	
        {
    		cout << "Enter first letter: ";
    		cin >> temp;
    		cin.ignore(100, '\n');
    				
    		head = ds.SortedInsert(head, temp);
    
    		cout << "Do Again...(y) or (n): ";
    		cin >> response;
    		cin.ignore(100, '\n');
        }while(response == 'y');
    
        cout << "You entered these letters:" << endl;
        ds.DisplayAll(head);
    }



    VERSION 2: (this one will not sort)
    Code:
    #include<iostream.h>
    #include<string.h>
    
    struct ds_node
    {
    	char* data1;
    	ds_node* next;
    };
    
    class ds_class
    {
    	private:
    		ds_node* head;
    	public:
    		ds_class();
    		~ds_class();
    		void DisplayAll(ds_node*& head);
    		ds_node* SortedInsert(ds_node*& head, char* data1);
    };
    
    
    //----------------------------------------------Constructor
    
    ds_class::ds_class()
    {
    	cout << "Constructor Called" << endl;
    	head = NULL;
    }
    
    //----------------------------------------------Destructor
    
    ds_class::~ds_class()
    {
    	cout << "Destructor Called" << endl;
    	delete head;
    	head = NULL;
    }
    
    //----------------------------------------------Display All Data
    
    void ds_class:: DisplayAll(ds_node*& head_ptr)
    {
    	while(head_ptr != NULL)
    	{
    		cout << head_ptr->data1 << endl;
    		cout << "----------------------------------------------" << endl;
    		head_ptr = head_ptr->next;
    	}
    	cout << endl;
    }
    
    //----------------------------------------------Sort / Insert Data
    
    ds_node* ds_class::SortedInsert(ds_node*& head, char* data1)
    {
    	ds_node* newNode;
    
    	// populate the new node that we're adding.
    	newNode = new ds_node;
    	newNode->data1 = data1;
    	newNode->next = NULL;
    
    	// figure out where to add that node in
    	if((head == NULL) || (head->data1 >= newNode->data1))
    	{
    		// this element should go at the head of the list
    		newNode->next = head;
    		head = newNode;
    	}
    	else	// this is not the first element in the list
    	{
    		// create a pointer that will traverse the list to find
    		// where to insert the new element
    		ds_node* current;
    		current = head;
    
    		// move current down the list till we find where our new node is supposed to go
    		while((current->next != NULL) && (current->next->data1 < newNode->data1))
    		{
    			current = current->next;
    		}
    		newNode->next = current->next;
    		current->next = newNode;
    	}
    	return head;
    }
    
    //----------------------------------------------Client Program
    
    
    void main()
    {
    	ds_class ds;
    	ds_node* head  = NULL;
    	char*    one;
        char     temp[20];
    	char     response;
    	
    	do	
        {
    		cout << "Enter first letter: ";
    		cin.get(temp, 20, '\n');
    		cin.ignore(100, '\n');
    		one = new char[strlen(temp) + 1];
    		strcpy(one, temp);
    		strcpy(temp, " ");
    		
    		head = ds.SortedInsert(head, one);
    
    		cout << "Do Again...(y) or (n): ";
    		cin >> response;
    		cin.ignore(100, '\n');
        }while(response == 'y');
    
        cout << "You entered these letters:" << endl;
        ds.DisplayAll(head);
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    You're working with the char pointer, and not the char variable. When you're checking to see if one is less than the other, your code is checking the pointer addresses, and not the values of the variables.

    Basically, dereference (or is it reference?) each time you work with data1.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    I'll give it a try
    Last edited by Zalbik; 03-02-2003 at 10:50 PM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    Ok...I read up on it ; tried it; and have yet to figure it out. I read that you use the * operator to dereference the ptr. So I have been trying different versions of *data1; in different locations but I take it there is more to it than that.

    I thought that in the SortedInsert function using:

    newNode->data1 = *data1;

    would do it, but I guess not because I get some error about a C-style cast.
    Last edited by Zalbik; 03-03-2003 at 12:24 AM.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Wrong. Look, when you write a struct like this:

    struct x { // 1024 + any alignment written
    char bytes[1024];
    };

    The entire struct is fed into the file.

    struct x { // 4 + any alignment written
    char * bytes;
    };

    This struct may have it's bytes pointing to 1024 real chars, and the address of those bytes will write to the file. Worse, dereferencing the pointer only places 1 byte in the file (the size of the character that it points to.

    Using fwrite() for instance, this would be done by writing each member individually, and whenever you reach a pointer:

    fwrite(x.bytes, sizeof(char), 1024, file);

    This is true all of the time and this is a common mistake thst programmers make.

    The easiest work around of course is to use real structure for reading/writing:

    class RealData; // forward declaration...

    class FileData {
    public:
    char buffer[1024];
    int results[256];
    operator = (RealData& t)
    {
    memcpy(buffer, t.buffer, 1024 * sizeof(char));
    memcpy(results, t.results, 256 * sizeof(int));
    }
    };

    class RealData {
    public:
    char * buffer;
    int * results;
    operator = (FileData& t)
    {
    memcpy(buffer, t.buffer, 1024 * sizeof(char));
    memcpy(results, t.results, 256 * sizeof(int));
    }
    };

    There are other options too...



    ITSA
    Socket Library!

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    I got.

    I changed:
    Code:
    newNode = new ds_node;
    newNode->data1 = data1;
    newNode->next = NULL;
    to this:
    Code:
    newNode = new ds_node;
    newNode->data1 = new char[strlen(data1) + 1];		
    strcpy(newNode->data1, data1);			
    newNode->next = NULL;
    and then I changed the two lines:
    Code:
    if((head == NULL) || (head->data1 >= newNode->data1))
    
    while((current->next != NULL) && (current->next->data1 < newNode->data1))
    to:
    Code:
    if((head == NULL) || (strcmp(head->data1, newNode->data1) >= 0))
    
    while((current->next != NULL) && (strcmp(current->next->data1, newNode->data1) < 0))
    Both of you helped...thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. pointers and vectors
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 04-01-2006, 11:06 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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