# Pointer Problems...

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 04-02-2007
Junior89
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:
Quote:

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?

• 04-02-2007
Junior89
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! :D
• 04-02-2007
Salem
> 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.
• 04-02-2007
ZuK
Code:

`pNum[i] = &numbers[i];`
should be
Code:

`pNum[i] = numbers[i];`
kurt
• 04-02-2007
epidemic
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.
• 04-02-2007
Junior89
Okay lets see...

I made the fixes you guys suggested but now i get the same problem:
Quote:

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!
• 04-02-2007
Salem
> bool assignPointersArray(int numbers[], int *pNum)

Nor have you fixed any of the sizeof issues I mentioned earlier.
• 04-02-2007
Junior89

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;         } }```
• 04-02-2007
Daved
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.
• 04-02-2007
Junior89
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.
• 04-02-2007
Junior89
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!
• 04-03-2007
vart
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
• 04-03-2007
dwks
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.  Missed this:
Quote:

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.

Quote:

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.
• 04-03-2007
Junior89
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!
• 04-03-2007
Junior89
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?
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last