473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Can't make sense of this arrays and pointers assignment

62
Hello guys,

I have a problem again with my lecturer's task she has given, I feel it is wrong at all.
Here is what she gave us:

in the Account.h attributes

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Transaction **Transactions; // she called this 'declaring an array Transactions'
  3.  
  4.  
but so far this is just a pointer to pointer, isn't it?

then in Account.cpp :

Expand|Select|Wrap|Line Numbers
  1. Account :: Account()
  2. {
  3.         Transactions = new Transaction *[SizeOfArray];
  4.         for (int i=0; i<SizeOfArray; i++)
  5.                 Transactions[i] = new Transaction();   ///is this right access of array elements?
  6. }
  7.  
  8. Account :: ~Account()
  9. {
  10.         for (int i=0; i<SizeOfArray; i++)
  11.                 delete Transactions[i];
  12.  
  13.         delete Transactions;
  14. }
  15.  
Can anyone explain what's going on here? I understand that she is declaring an array or pointers through a pointer Transactions, and then allocates an object for each pointer, but is it right to access the array like that?
Dec 1 '07 #1
8 1635
Ganon11
3,652 Expert 2GB
Everything I see here is correct. You are correct in seeing that Transactions is a pointer to a pointer to a Transaction. So, when you initialize the 'first' pointer, you make it point to an array of its type - pointers:

Expand|Select|Wrap|Line Numbers
  1. Transactions = new Transaction*[size];
There is nothing special about this - Transactions is just a regular array like you've always seen. What changes is the type of data held in the array - pointers, each of which can point to a whole new Transaction object or array of Transaction objects.

When initializing the Transaction pointers, you have to access each pointer held in Transactions, and set each of these to a new Transaction.

When deleting, it is not enough to delete [] Transactions. All of the pointers also allocated memory, which must be de-allocated. The only way to get the memory locations is by using the still-intact Transactions array. Once this is done, you can de-allocate the pointers held in Transactions.

The only note I would make is I'm not sure you should put the parentheses after new Transaction - it may give you some funny errors. If you do get errors around this part of the function, try removing them.
Dec 1 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Transaction **Transactions; // she called this 'declaring an array Transactions'
Your instructor is incorrect. A Transaction** is a pointer to a Tranaction* and a Trancaction* is a pointer to a single Transcation. There is nothing about an array here.

This is a common scew-up where arrays are concerned.

Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
You might show this to your instructor.
Dec 2 '07 #3
Your instructor is incorrect. A Transaction** is a pointer to a Tranaction* and a Trancaction* is a pointer to a single Transcation. There is nothing about an array here.
This is a common scew-up where arrays are concerned.
You might show this to your instructor.
Listen Weakness....please be sure you clearly understood what you are talking about before claiming yourself to be right.
you should know that new operator return an address,and a Transaction object contructed trhrough a new operation is itself a pointer (i.e. the variable stores an address).
Remember that an array is nothing but a variable,a variable wich represents the first address of a set of contiguous memory blocks;so an array variable is itself a pointer.
Therefore declaration: Transaction** transactions declares transactions as the first address of a set of memory regions storing addresses.Do you still believe the instructor is wrong?
Dec 2 '07 #4
oler1s
671 Expert 512MB
And MarshMallow, before you accuse someone of being wrong, it's probably a good idea to check that you aren't making a mistake yourself. See the C-FAQ. Or grab a draft of the standards and check for yourself.

It does not follow that if you can manipulate arrays through pointer notation, that pointers and arrays are equivalent. An array is not a pointer. Referring to an array is syntactically equivalent to referring to the address of the first element.

I don't know how you get the implication that a pointer unconditionally becomes a reference to an array. Like the instructor, you too have confused the word "equivalence".
Dec 2 '07 #5
And MarshMallow, before you accuse someone of being wrong, it's probably a good idea to check that you aren't making a mistake yourself. See the C-FAQ.
The code posted by the instructor is right.let's stop it.
Taking a C-whatsoeverFAQ as the reference is not a good way to study Oler.
Dec 2 '07 #6
oler1s
671 Expert 512MB
Taking a C-whatsoeverFAQ as the reference is not a good way to study Oler.
Apparently, you aren't aware of where this C-FAQ is coming from. The C-FAQ and C++-FAQ sites are in a way connected to the Usenet groups comp.lang.c and .c++. If you don't understand the implications of what I'm saying, well, let's just say you should use Google to find out about them.

EDIT: I realize my post might have come off as an attack. It's not. You really should know about the C-FAQ and C++-FAQ. If you post a Usenet question in the comp.lang groups for those languages, and it's answered by something in one of the FAQs, you will be pointed there. That's why you would do well to read the material in the FAQ. They genuinely address a lot of the mistakes that people make in C and C++.

Using the C FAQ, C++ FAQ, and the Usenet groups is an excellent way to study.
Dec 2 '07 #7
weaknessforcats
9,208 Expert Mod 8TB
The code posted by the instructor is right.let's stop it.
Taking a C-whatsoeverFAQ as the reference is not a good way to study Oler.
The instructor's code is not correct. Do a Google on decay of array to pointer and you will see what I mean.
Dec 3 '07 #8
jewel87
62
ok, guys, thank you all anyways, don't fight, it's ok.
Dec 7 '07 #9

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

Similar topics

5
by: Ben | last post by:
Hi all, I know what I need to do if data_ in the code below were a uchar*, but what about when it's an uchar array? Do I need to specify my own copy-constructor and assignment operator? ...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
4
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
33
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);
18
by: usr.root | last post by:
Is there any differece of this two function: int f1(int first); int f2(int *second);
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
11
by: Mr. Ken | last post by:
For example, int arr; arr = {1,2,3,4,5};
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.