Click here to Skip to main content
15,887,888 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRight method of swapping arrays and classes using pointers Pin
Javier Luis Lopez19-Sep-16 21:35
Javier Luis Lopez19-Sep-16 21:35 
AnswerRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan19-Sep-16 22:19
mveRichard MacCutchan19-Sep-16 22:19 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
leon de boer20-Sep-16 0:41
leon de boer20-Sep-16 0:41 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Javier Luis Lopez17-Aug-17 23:11
Javier Luis Lopez17-Aug-17 23:11 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan17-Aug-17 23:32
mveRichard MacCutchan17-Aug-17 23:32 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Javier Luis Lopez20-Aug-17 20:19
Javier Luis Lopez20-Aug-17 20:19 
GeneralRe: Right method of swapping arrays and classes using pointers Pin
Richard MacCutchan20-Aug-17 21:46
mveRichard MacCutchan20-Aug-17 21:46 
AnswerRe: Right method of swapping arrays and classes using pointers Pin
leon de boer20-Sep-16 15:27
leon de boer20-Sep-16 15:27 
As I am sure this is homework I won't give you the answer, what I will tell you is the problem with the typecast

Using the dereference operator (the *) is like mathematics there is an order to things just like in mathematics.
If I gave you 2 + 3 * 4 and you wanted the add before the multiply you need brackets (2 + 3) * 4 the mult carries higher precedence normally.

Same problem happens in dereference operator when using arrays

double **a;
*a[2] has two possible breakdowns so lets use brackets (*a)[2] or *(a[2])
*a[2] is actually equivalent to the latter *(a[2])

I bet that is not what you were expecting and reading it as Smile | :)

Your code doesn't do anything like what your text comments say because you are missing brackets. You need the dereference completed prior to the array use and (*a)[2] is the correct use for you.

Lets give you a simple code
double myArray[6] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 };

double *p = &myArray[0];   // P points at my array
double d = p[3];           // I can get an array value from a double* it will be 3.0

double **q = &p;           // Q points to p


double s = (*q)[3];        // This will correctly return  3.0 .. *q evaluates first
double t = *q[2];          // This will crash and burn it thinks q is an array of double* and wants *item[2]

Does that last line look familiar?
It is the same if the you are derefencing an object or a class

*a.xyz may naively be intended as (*a).xyz or *(a.xyz)
*a->xyz may naively be intended as (*a)->xyz or *(a->xyz)

So look up and learn the precedence order of dereferencing and if you want the other USE BRACKETS. It's actually good practice to always use brackets if you are dereferencing complex items so you know for sure what the order will be be, you do the same with long chains with mathematics. It's a normal gotcha to not dereference what you think you are on complex items because of dereference precedence order (you can't just read it left to right and expect that order).
In vino veritas


modified 21-Sep-16 1:38am.

GeneralRe: Right method of swapping arrays and classes using pointers Pin
Saravanan Sundaresan24-Sep-16 21:01
professionalSaravanan Sundaresan24-Sep-16 21:01 
QuestionImage processing in C Pin
Ravi Shanakar Singh16-Sep-16 19:23
Ravi Shanakar Singh16-Sep-16 19:23 
AnswerRe: Image processing in C Pin
Jochen Arndt16-Sep-16 22:17
professionalJochen Arndt16-Sep-16 22:17 
AnswerRe: Image processing in C Pin
enhzflep18-Sep-16 15:34
enhzflep18-Sep-16 15:34 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 7:07
leon de boer19-Sep-16 7:07 
GeneralRe: Image processing in C Pin
enhzflep19-Sep-16 20:48
enhzflep19-Sep-16 20:48 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 23:54
leon de boer19-Sep-16 23:54 
AnswerRe: Image processing in C Pin
CPallini18-Sep-16 21:34
mveCPallini18-Sep-16 21:34 
GeneralRe: Image processing in C Pin
leon de boer19-Sep-16 7:09
leon de boer19-Sep-16 7:09 
GeneralRe: Image processing in C Pin
CPallini19-Sep-16 9:14
mveCPallini19-Sep-16 9:14 
AnswerRe: Image processing in C Pin
leon de boer19-Sep-16 7:17
leon de boer19-Sep-16 7:17 
QuestionCarlos Antollini ADO classes Pin
Member 1111024014-Sep-16 7:03
Member 1111024014-Sep-16 7:03 
AnswerRe: Carlos Antollini ADO classes Pin
Victor Nijegorodov14-Sep-16 7:26
Victor Nijegorodov14-Sep-16 7:26 
Questionvc630 vending machines Pin
Member 1273272111-Sep-16 8:04
Member 1273272111-Sep-16 8:04 
AnswerRe: vc630 vending machines Pin
NotPolitcallyCorrect11-Sep-16 8:08
NotPolitcallyCorrect11-Sep-16 8:08 
AnswerRe: vc630 vending machines Pin
Chris Losinger12-Sep-16 4:19
professionalChris Losinger12-Sep-16 4:19 
JokeRe: vc630 vending machines Pin
enhzflep12-Sep-16 14:22
enhzflep12-Sep-16 14:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.