Pointer Problems...

This is a discussion on Pointer Problems... within the C++ Programming forums, part of the General Programming Boards category; Okay so i've just really started getting into pointers cause i've been working with linked lists, double-linked lists, and (im ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Pointer Problems...

    Okay so i've just really started getting into pointers cause i've been working with linked lists, double-linked lists, and (im now trying) binary trees. Here's my problem:
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\binary tree 2\binary tree 2\main.cpp(105) : error C2440: '=' : cannot convert from 'int *__w64 ' to 'int'
    There is no context in which this conversion is possible
    Here's my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(int numbers[]);		//Creates the Tree
    void addPly();						//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[]);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[]);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	if(assignPointersArray(numbers, pNum) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter 10 Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum);
    	cin.ignore();
    
    	createTree(numbers);
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(int numbers[])
    {
    	//Create Tree Function
    }
    
    void sortNumbers(int numbers[], int *pNum[])
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<(sizeof(numbers)); i++)
    	{
    		for(int s=0; s<(sizeof(numbers)); s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    	//Display Array
    	for(int j=0; j<(sizeof(numbers)); j++)
    	{
    		cout<<"Element "<<(j+1)<<" Equals: "<<numbers[j]<<endl;
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum)
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<(sizeof(numbers)); i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    What am i doing wrong? My Learn C++ in 21 Days book says that's how you set the pointer to the address of something. The fact that it's an array changes things maybe?

    Thanks for any help you can give!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Also my Sort Numbers function isn't working, i'm trying to get the computer to take the elements in the arrray and sort them from lowest to highest but it only seems to work for the first 4 elements, after that it doesn't.

    Thanks again!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > for(int i=0; i<(sizeof(numbers)); i++)
    1. sizeof() on an array only works as you might expect when you have a real array. When all you have is a pointer to the array (like inside any function), then you get the size of the pointer.
    2. To get the number of elements in an array (assuming a real array to begin with), it would be
    sizeof(array)/sizeof(array[0]);


    > error C2440: '=' : cannot convert from 'int *__w64 ' to 'int'
    At least comment the code to indicate which line that is.
    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.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    pNum[i] = &numbers[i];
    should be
    Code:
    pNum[i] = numbers[i];
    kurt

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    hey Junior, can u point me out where u found the tutorial, incase of a book, what is its name i also want to learn about lists etc but i cant seem to find a good tutorial.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay lets see...

    I made the fixes you guys suggested but now i get the same problem:
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\binary tree 2\binary tree 2\main.cpp(105) : error C2440: '=' : cannot convert from 'int' to 'int *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    And here's the code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(int numbers[]);		//Creates the Tree
    void addPly();						//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[]);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[]);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	if(assignPointersArray(numbers, pNum) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter 10 Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum);
    	cin.ignore();
    
    	createTree(numbers);
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(int numbers[])
    {
    	//Create Tree Function
    }
    
    void sortNumbers(int numbers[], int *pNum[])
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<(sizeof(numbers)); i++)
    	{
    		for(int s=0; s<(sizeof(numbers)/sizeof(numbers[0])); s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    	//Display Array
    	for(int j=0; j<(sizeof(numbers)); j++)
    	{
    		cout<<"Element "<<(j+1)<<" Equals: "<<numbers[j]<<endl;
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[])
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<(sizeof(numbers)); i++)
    		{
    			pNum[i] = numbers[i];
    		}
    		return true;
    	}
    }
    And for Epidemic, yeah i'll try to find the tutorial i used for linked lists, this program here though i used that basic information from the tutorial to try to implement a binary tree on my own haha.

    I'll get back to ya.


    Thanks!
    Last edited by Junior89; 04-02-2007 at 02:11 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > bool assignPointersArray(int numbers[], int *pNum)
    This doesn't match your prototype.

    Nor have you fixed any of the sizeof issues I mentioned earlier.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    http://richardbowles.tripod.com/cpp/...t/linklist.htm

    The first result from Google

    Good luck

    EDIT----

    Okay so i went back and changed the one part from pNum[i] = numbers[i] back to the &numbers[i].

    It compiles fine now.

    But as far as the sort list thing goes, it still only does the first 4 elements And i did fix the sizeofs this time:
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(int numbers[]);		//Creates the Tree
    void addPly();						//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[]);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[]);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	if(assignPointersArray(numbers, pNum) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter 10 Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum);
    	cin.ignore();
    
    	createTree(numbers);
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(int numbers[])
    {
    	//Create Tree Function
    }
    
    void sortNumbers(int numbers[], int *pNum[])
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<(sizeof(numbers)); i++)
    	{
    		for(int s=0; s<(sizeof(numbers)/sizeof(numbers[0])); s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    	//Display Array
    	for(int j=0; j<(sizeof(numbers)); j++)
    	{
    		cout<<"Element "<<(j+1)<<" Equals: "<<numbers[j]<<endl;
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[])
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<(sizeof(numbers)/sizeof(numbers[0])); i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    Last edited by Junior89; 04-02-2007 at 02:19 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Unfortunately you did not fix the sizeof problems, because you are using sizeof on an array passed to a function. That only works when you are in the same scope as the array was originally created.

    You need to pass the size of the array as a separate variable to the functions that take the numbers array. Or you could use vector which knows its own size.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Seeing as i know nothing about vectors (outside of mathematics and physics ha) i'll do the former, i'll let you know how it goes.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  11. #11
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Here's My Code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(int numbers[]);		//Creates the Tree
    void addPly();						//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[], int size);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[], int size);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	int size;			//Size of the two previous arrays
    	size = (sizeof(numbers)/sizeof(numbers[0]));
    	if(assignPointersArray(numbers, pNum, size) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter 10 Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum, size);
    	cin.ignore();
    
    	createTree(numbers);
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(int numbers[])
    {
    	//Create Tree Function
    }
    
    void sortNumbers(int numbers[], int *pNum[], int size)
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    	//Display Array
    	for(int j=0; j<size; j++)
    	{
    		cout<<"Element "<<(j+1)<<" Equals: "<<numbers[j]<<endl;
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[], int size)
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    I'm getting some very interesting results, and a runtime error.

    Okay...

    First off, it sorts the numbers right, but the first number in the sequence is an enourmous negative number, the other 9 are the first 9 of the array in the proper order.

    And after it sorts the numbers and it runs the createTree() function (probably), it says the stack with the numbers array is corrupted i believe.

    Thanks for any help ya can give!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if(sizeof(numbers) != sizeof(pNum))
    What is this check for?

    I don't see that you are using pNum in your sorting function... Why do you need this array at all?

    Your swapping segment has too much code - check the bubble sort example in the net to optimize it a little...


    in your loop when s= size-1 s+1 goes out of bounds of the array

    PS. and do not use globals
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    You access numbers[s+1], which is one past the end during the loop's last iteration. Maybe make s<size into s+1<size. [edit] Missed this:
    in your loop when s= size-1 s+1 goes out of bounds of the array
    [/edit]

    Also, you should delete any memory you allocate with new.

    Code:
    if(sizeof(numbers) != sizeof(pNum))
    What is this check for?
    I think the OP wants to make sure that numbers and pNum contain the same number of elements . . . but it doesn't work. sizeof(an_array) that has been passed to a function is always the same as sizeof(a_pointer) because arrays, when passed to functions, "degrade" (sorry, Prelude) into pointers.
    Last edited by dwks; 04-03-2007 at 01:31 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Haha, thanks! That fixed the sorting issues, and yeah i gotta delete that memory allocated for the new calls, i havent gotten around to coding the destroyTree() or whateveri called it funciton haha.

    Thanks! I'll let yall know if i have any more problems, thanks again!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay... So.... i wrote a part of the sortnumbers function which changes the numbers[] array from the original one the user entered in to the sorted one. Now my task (to me a bit daunting now cause i'm lost at how exactly to do it) is how to create the tree

    So here's my code thus far for creating the tree, i dunno where to go from here:
    Code:
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size &#37; 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)((size/2) - 0.5);
    	}
    
    	start->data = numbers[startPoint];
    }
    
    void addPly(node *parent, int numbers[], int cArrayPoint)
    {
    	node *nLeft;
    	node *nRight;
    
    	nLeft = new node;
    	nRight = new node;
    
    	if(numbers[cArrayPoint-1])
    	{
    		nLeft->data = numbers[cArrayPoint-1];
    		parent->cLeft = nLeft;
    	}else{
    		parent->cLeft = NULL;
    		delete nLeft;
    	}
    	if(numbers[cArrayPoint+1])
    	{
    		nRight->data = numbers[cArrayPoint+1];
    		parent->cRight = nRight;
    	}else{
    		parent->cRight = NULL;
    		delete nRight;
    	}
    }
    I don't know how to implement this effectively without having to 'manually' call the addPly() funtion over and over again (like having the function create tree look like this: )
    Code:
    ...
    addPly(root);
    addPly(currentL);
    addPly(currentR);
    etc.
    ...
    Thoughts?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Page 1 of 3 123 LastLast
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, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM

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