473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can i store address of array elements in an another array element

11 New Member
hi,
i am trying to implement a B+ tree. There is no specification in the implementation so i am using arrays to implement the B+ tree. However, there is a problem that i encountered which is I cannot store the address of an array element in an another array element. it is purpose is reaching the childs. The other thing that i want to mention is that the bucket size of the b+ tree is not specific. It will be determined at the beginning of the program. For example it can store 4,6 or 10 data. it does not matter. Here is my code any help will be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. #define length(a) (sizeof a / sizeof a[0])
  6. class Node{
  7. public:
  8.     int count;
  9.     int leaf;
  10.     int *data;
  11.     int *pointer;
  12.     Node(int a){
  13.         leaf = 1;
  14.         count = 0;
  15.         data = new int[a];
  16.         pointer = new int[a+1];
  17.  
  18.     }
  19. };
  20. void insert(Node *node, int data, int line){
  21.     Node *temp = new Node(line);
  22.     temp = node;
  23.     int i = 0;
  24.     if(temp->leaf == 1){
  25.         if(temp->count != line){
  26.             while(temp->data[i] < data){
  27.                 i++;
  28.             }
  29.             for (int j = line; j > i; j--){
  30.                 temp->data[j] = temp->data[j - 1];
  31.             }
  32.             temp->data[i] = data;
  33.             temp->count++;
  34.  
  35.         }
  36.         else{
  37.             Node *temp2 = new Node(line);
  38.             Node *temp3 = new Node(line);
  39.             //temp->pointer[0] = &(temp2->data[0]);
  40.             //temp->pointer[1] = &(temp3->data[0]);
  41.             for(int i = 0; i < 2; i++){
  42.                 temp2->data[i] = temp->data[i];
  43.             }
  44.             for(int i = 2; i < 5; i++){
  45.                 temp3->data[i] = temp->data[i];
  46.             }
  47.             for(int i = 0; i < 4; i++){
  48.                 temp->data[i] = 0;
  49.             }
  50.             temp->data[1]=temp3->data[1];
  51.  
  52.         }
  53.     }
  54.  
  55. }
  56. /*void deneme1(){
  57.     int a[3];
  58.     int *b[4];
  59.     b[1] = &(a[1]);
  60.     cout << b[1] << endl;
  61.     cout << &(a[1]) << endl;
  62.  
  63.  
  64. }*/
  65.  
  66. int main(){
  67.     Node *temp = new Node(4);
  68.     Node *temp2 = new Node(4);
  69.     temp2->data[0] = 2;
  70.     cout << &temp2->data[0] << endl;
  71.     temp->pointer[0] = (int)(&temp2->data[0]);
  72.     //long x = &temp2->data[0];
  73.     //temp->pointer[0] = &temp2->data[0];
  74.     temp->data[0] = 3;
  75.     temp->data[1] = 12;
  76.     temp->data[2] = 45;
  77.     insert(temp,34,4);
  78.     for(int i = 0; i < 4; i++)
  79.         cout << temp->data[i] << endl;
  80.  
  81.     cout << "benim  " << temp->pointer[0] <<endl;
  82.  
  83.     /*cout << sizeof(temp->data) << endl;
  84.     cout << length(temp->data) << endl;
  85.     cout << temp->data[0] << endl;
  86.     cout << temp->data[1] << endl;*/
  87.     return 0;
  88.  
  89.  
  90. }
  91.  
Thank you very much for your helps.
Oct 31 '08 #1
13 2690
AmeL
15 New Member
Could you please put the code bracket around your code for readability; and write us the error message which compiler produce ( with line number ) to make us easy to solve your problem, if that is a syntax error ?
Nov 1 '08 #2
archonmagnus
113 New Member
hi,
However, there is a problem that i encountered which is I cannot store the address of an array element in an another array element.
I'm not sure what you mean by your statement. When I compile your code (using GCC version 4.1.2), I get the following output:
Expand|Select|Wrap|Line Numbers
  1. 0x804a068
  2. 3
  3. 12
  4. 34
  5. 45
  6. benim 134520936
  7.  
In your code you have the following:
Expand|Select|Wrap|Line Numbers
  1. // This prints: "0x804a068"
  2. cout << &temp2->data[0] << endl;
  3. temp->pointer[0] = (int)(&temp2->data[0]);
  4. ...
  5. // This prints: "benim 134520936"
  6. cout << "benim " << temp->pointer[0] <<endl;
  7.  
The observant reader will note that 134520936_(Dec) =0x804a068_(Hex ). So it seems you are correctly storing the address of an array element into another element. Am I understanding your inquiry correctly?

To make the comparison a bit easier, you could have used the lines:
Expand|Select|Wrap|Line Numbers
  1. // This prints: "0x804a068"
  2. cout << &temp2->data[0] << endl;
  3. temp->pointer[0] = (int)(&temp2->data[0]);
  4. ...
  5. // This prints: "benim 0x804a068"
  6. cout << "benim 0x" << hex << temp->pointer[0] <<endl;
  7.  
Nov 1 '08 #3
tekinalp67
11 New Member
Could you please put the code bracket around your code for readability; and write us the error message which compiler produce ( with line number ) to make us easy to solve your problem, if that is a syntax error ?
Ok I will. But there is no error messages. The problem is cannot getting the address of desired element correctly.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #define length(a) (sizeof a / sizeof a[0]) class Node{ public:     int count;     int leaf;     int *data;     int *pointer;     Node(int a){         leaf = 1;         count = 0;         data = new int[a];         pointer = new int[a+1];     } }; void insert(Node *node, int data, int line){     Node *temp = new Node(line);     temp = node;     int i = 0;     if(temp->leaf == 1){         if(temp->count != line){             while(temp->data[i] < data){                 i++;             }             for (int j = line; j > i; j--){                 temp->data[j] = temp->data[j - 1];             }             temp->data[i] = data;             temp->count++;         }         else{             Node *temp2 = new Node(line);             Node *temp3 = new Node(line);             //temp->pointer[0] = &(temp2->data[0]);             //temp->pointer[1] = &(temp3->data[0]);             for(int i = 0; i < 2; i++){                 temp2->data[i] = temp->data[i];             }             for(int i = 2; i < 5; i++){                 temp3->data[i] = temp->data[i];             }             for(int i = 0; i < 4; i++){                 temp->data[i] = 0;             }             temp->data[1]=temp3->data[1];         }     } } void deneme1(){     int a[3];     int *b[4];     b[1] = &(a[1]);     cout << b[1] << endl;     cout << &(a[1]) << endl; } int main(){     Node *temp = new Node(4);     Node *temp2 = new Node(4);     temp2->data[0] = 2;     cout << &temp2->data[0] << endl;     temp->pointer[0] = (int)(&temp2->data[0]);     //long x = &temp2->data[0];     //temp->pointer[0] = &temp2->data[0];     temp->data[0] = 3;     temp->data[1] = 12;     temp->data[2] = 45;     insert(temp,34,4);     for(int i = 0; i < 4; i++)         cout << temp->data[i] << endl;     cout << "benim  " << temp->pointer[0] <<endl;     cout << "benim 00" << hex << temp->pointer[0] <<endl;     cout << *&temp2->data[0] << endl;     /*cout << sizeof(temp->data) << endl;     cout << length(temp->data) << endl;     cout << temp->data[0] << endl;     cout << temp->data[1] << endl;*/     deneme1();     return 0; }
In the line "temp->pointer[0] = (int)(&temp2->data[0]);"
thank you
Nov 1 '08 #4
tekinalp67
11 New Member
I'm not sure what you mean by your statement. When I compile your code (using GCC version 4.1.2), I get the following output:
Expand|Select|Wrap|Line Numbers
  1. 0x804a068
  2. 3
  3. 12
  4. 34
  5. 45
  6. benim 134520936
  7.  
In your code you have the following:
Expand|Select|Wrap|Line Numbers
  1. // This prints: "0x804a068"
  2. cout << &temp2->data[0] << endl;
  3. temp->pointer[0] = (int)(&temp2->data[0]);
  4. ...
  5. // This prints: "benim 134520936"
  6. cout << "benim " << temp->pointer[0] <<endl;
  7.  
The observant reader will note that 134520936_(Dec) =0x804a068_(Hex ). So it seems you are correctly storing the address of an array element into another element. Am I understanding your inquiry correctly?

To make the comparison a bit easier, you could have used the lines:
Expand|Select|Wrap|Line Numbers
  1. // This prints: "0x804a068"
  2. cout << &temp2->data[0] << endl;
  3. temp->pointer[0] = (int)(&temp2->data[0]);
  4. ...
  5. // This prints: "benim 0x804a068"
  6. cout << "benim 0x" << hex << temp->pointer[0] <<endl;
  7.  
first of thank you for your reply.
I have question about the "x". what is it for
for example there is an output that i generate.
000221D8
3
12
34
45
benim 139736
benim 0x221d8
2
in here 000221D8 is not same with the 0x221d8.
Thank you again for your helps.
Nov 1 '08 #5
tekinalp67
11 New Member
I want to ask another thing. :)
For example and assume that in my code this line
Expand|Select|Wrap|Line Numbers
  1. temp->pointer[0] = (int)(&temp2->data[0]);
get the address of the temp2->data[0] to the temp->pointer[0]
after getting the address of the element how can I get the actual value in that address I mean which statement will give it to me.
The second question is how can i store the address of the elements in the insert method.
In the code lines
temp->pointer[0] = &(temp2->data[0]);
temp->pointer[1] = &(temp3->data[0]);
thank you. very much.
I cannot put code tag around the codes because then there are &nbps which I cannot correct. Sorry for the inconvience.
Nov 1 '08 #6
JosAH
11,448 Recognized Expert MVP
For example and assume that in my code this line
Expand|Select|Wrap|Line Numbers
  1. temp->pointer[0] = (int)(&temp2->data[0]);
get the address of the temp2->data[0] to the temp->pointer[0]
after getting the address of the element how can I get the actual value in that address
That is all very unsafe what you're doing there: it assumes that sizeof(int) ==
sizeof(your pointer type). Better define a small union type that can safely hold
both (one of each) type.

kind regards,

Jos
Nov 1 '08 #7
tekinalp67
11 New Member
That is all very unsafe what you're doing there: it assumes that sizeof(int) ==
sizeof(your pointer type). Better define a small union type that can safely hold
both (one of each) type.

kind regards,

Jos
thank you very much. but i am beginner in c++.
Can you tell me how can i define a small union type.:)
if you can write that piece of code i will very happy:)
Thank you very much.
Nov 1 '08 #8
tekinalp67
11 New Member
Expand|Select|Wrap|Line Numbers
  1.  void deneme1(){
  2.     int a[3];
  3.     int *b[4];
  4.     b[1] = &(a[1]);
  5.     cout << b[1] << endl;
  6.     cout << &(a[1]) << endl;

In here one of the array is an int array the other is pointer of array. So there is no problem while storig the addresses.
How can I make the pointer of array to an array to not get the errors "1>------ Build started: Project: BPlusTreeSecond , Configuration: Debug Win32 ------
1>Compiling...
1>BPlusTree.cp p
1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 15) : error C2440: '=' : cannot convert from 'int *' to 'int'
1> There is no context in which this conversion is possible
1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 25) : error C2109: subscript requires array or pointer type
1>c:\users\asus \documents\visu al studio 2008\projects\b plustreesecond\ bplustreesecond \bplustree.cpp( 25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://c:\Users\asus\D ocuments\Visual Studio 2008\Projects\B PlusTreeSecond\ BPlusTreeSecond \Debug\BuildLog .htm"
1>BPlusTreeSeco nd - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
".
Expand|Select|Wrap|Line Numbers
  1. class Node{
  2. public:
  3.     int count;
  4.     int leaf;
  5.     int data;
  6.     int *pointer;
  7.     Node(int a){
  8.         leaf = 1;
  9.         count = 0;
  10.         data = new int[a];
  11.         pointer = new int[a+1];
  12.     }
  13. };
In the above on the line 5 data is an ordinary array. it is not a pointer of array. But then there are lots of errors I encountered. Is there anyone who can propose a better solution.
Thank you very much.
Nov 1 '08 #9
tekinalp67
11 New Member
Please some one help me...
I change my code so that I can store the address of the array elements in an array. However, there is another problem which i mentioned earlier is that I cannot reach the element that this address refers. This is my code:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. #define length(a) (sizeof a / sizeof a[0])
  6. class Node{
  7. public:
  8.     int count;
  9.     int leaf;
  10.     int *data;
  11.     int** pointer;
  12.     Node(int a){
  13.         leaf = 1;
  14.         count = 0;
  15.         data = new int[a];
  16.         pointer = new int*[a+1];
  17.     }
  18. };
  19. void insert(Node *node, int data, int line){
  20.     Node *temp = new Node(line);
  21.     temp = node;
  22.     int i = 0;
  23.     if(temp->leaf == 1){
  24.         if(temp->count != line){
  25.             while(temp->data[i] < data){
  26.                 i++;
  27.             }
  28.             for (int j = line; j > i; j--){
  29.                 temp->data[j] = temp->data[j - 1];
  30.             }
  31.             temp->data[i] = data;
  32.             temp->count++;
  33.         }
  34.         else{
  35.             Node *temp2 = new Node(line);
  36.             Node *temp3 = new Node(line);
  37.             //temp->pointer[0] = (int)&(temp2->data);
  38.             //temp->pointer[1] = (int)&(temp3->data);
  39.             for(int i = 0; i < 2; i++){
  40.                 temp2->data[i] = temp->data[i];
  41.             }
  42.             for(int i = 2; i < 5; i++){
  43.                 temp3->data[i] = temp->data[i];
  44.             }
  45.             for(int i = 0; i < 4; i++){
  46.                 temp->data[i] = 0;
  47.             }
  48.             temp->data[1]=temp3->data[1];
  49.         }
  50.     }
  51. }
  52. void deneme1(){
  53.     int a[3];
  54.     int *b[4];
  55.     b[1] = &(a[1]);
  56.     cout << b[1] << endl;
  57.     cout << &(a[1]) << endl;
  58. }
  59. int main(){
  60.     Node *temp = new Node(4);
  61.     Node *temp2 = new Node(4);
  62.     temp2->data[0] = 2;
  63.     cout << &temp2->data << endl;
  64.     temp->pointer[0] = (int*)(&temp2->data);
  65.     //long x = &temp2->data[0];
  66.     //temp->pointer[0] = &temp2->data[0];
  67.     temp->data[0] = 3;
  68.     temp->data[1] = 12;
  69.     temp->data[2] = 45;
  70.     insert(temp,34,4);
  71.     for(int i = 0; i < 4; i++)
  72.         cout << temp->data[i] << endl;
  73.     cout << "benim  " << temp->pointer[0] <<endl;
  74.     //cout << "benim 0x" << hex << temp->pointer[0] <<endl;
  75.     cout << *&temp->pointer[0] << endl;
  76.     /*cout << sizeof(temp->data) << endl;
  77.     cout << length(temp->data) << endl;
  78.     cout << temp->data[0] << endl;
  79.     cout << temp->data[1] << endl;*/
  80.     //deneme1();
  81.     return 0;
  82. }
on line 11 i changed the code.
Please some one help me to fix this.
Thank you.
Nov 1 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
1934
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
3
1566
by: Kai Wu | last post by:
Hello, It turns out that STL vector does not assure the constness of its stored element address (memory location), i guess by the time it dynamicly allocates new memory as new element get pushed in, it might copy stored elements to new locations. Is there any container implementation attempted doing so or how to instruct STL to keep the address of those stored elements ?
9
2319
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
12
5536
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the following program: #include<stdio.h> #define SIZE 1 int main()
33
3195
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
10
2376
by: | last post by:
If I have an array of int: int array; I suppose the correct way to clear it using memset() would be: memset(array, 0, 8 * sizeof(int)); However, I've seen the following in a piece of code: memset(&array, 0, 8 * sizeof(int));
11
3470
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among other pieces of information information and retrieve it later... like this: *********************** //supossing i have a istream
17
7261
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
36
3404
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an address? I keep feeling like I'm missing something obvious. -Jul To keep things in context, this is in reference to describing functions to a beginner.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.