![]() |
|
#1
|
|||
|
|||
Pointer ProblemHi all,
I've got a function : CPP / C++ / C Code:
But when i call it in main: CPP / C++ / C Code:
Both A and B get assigned to the same address. Why is this so, don;t the malloc in padMatrix assign to diff address each time I call it? And, how can I overwrite the 2-D array in padMatrix propoerly using passing of ref and not copying value to value? Many thanks. |
|
#2
|
|||
|
|||
|
Quote:
If you want to change the value of A in the function padMatrix and you want that value to be transferred back to the main function, there are a couple of ways. Since A is a pointer-to-pointer to int, you can do either of these: 1. Make padMatrix return a pointer-to-pointer to int, and return whatever you want to be the new value of A. 2. Make the function argument be a pointer to A, and work with it in the function. (So the argument is a pointer-to-pointer-to-pointer to int). An additional note: Be careful of how you free A in the function. Didn't you allocate storage for it in the way that you allocated storage for R in the function? That is, shouldn't you do something like this to free storage for the old matrix: Code:
Otherwise you will have a memory leak. Regards, Dave |
|
#3
|
|||
|
|||
|
In addition to Dave's post above, something else to watch out for...
In your padMatix function, it seems you can expand the boundries, but if you do, you are going to have a problem here: Code:
If you originally had a 3x3 matrix, and expanded to a 7x7 one, you are going to have problems with A[5][5] in your copy... And my personal preference, regarding Dave's solution, is return an int** from padMatrix. Too much indirection is painful! |
|
#4
|
|||
|
|||
|
Thanks guys for ur replies.
Well I did what rdrast said, but the thing is these matrices are huge of order of thousands of integers that's why I wanna overwrite . Just wondering if I use "triple" pointers do i pass the address of A in as in: CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
|
Quote:
Yes. The concept of pointer-to-pointer-to-pointer is a little daunting for some of us, but if you really want learn how to do it this way, you could try something like this: CPP / C++ / C Code:
You could simplify the notation by using a typedef or by using a different variable in padMatrix(), but the above code will work as written. Be careful with the notation: (*A) is used to dereference A. Also, make sure that the matrix you represent with the first call padMatrix(&xxx, n) is an nxn matrix, dynamically allocated (with malloc) from a pointer-to-pointer to int. Regards, Dave |
Recent GIDBlog
More photos on Flickr by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| 2D arrays:dynamic allocation and freeing | bravetanveer | C Programming Language | 48 | 27-Nov-2007 15:55 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
| Please help! Newbie pointer problem! | robsmith | C Programming Language | 5 | 12-Mar-2005 04:48 |
| C Pointer problem | fwongmc | C Programming Language | 8 | 04-Dec-2004 12:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The