![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 48
| "duplicating" an array of pointers Code: typedef struct node
{
int exp;
float koef;
struct node *next;
} monomial;
monomial *p[5];
I hope you understand me |
| budala is offline | |
| | #2 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I suppose you are talking about a deep copy. So allocate memory to q using malloc, then use assignments in a loop (or memcpy). q[i] = malloc(...); memcpy(q, p, size_of_p);
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #3 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Just chuck it all in a loop. I'd recommend allocating as you go, otherwise you'll have to keep track of where you started from if you want to free the entire array in one go. |
| zacs7 is offline | |
| | #4 |
| Registered User Join Date: Apr 2009
Posts: 48
| thanks. works like a charm. put it in a loop Code: for (i=0; i<=n; i++)
{
q[i] = malloc(sizeof(monomial));
q[i] = p[i];
}
|
| budala is offline | |
| | #5 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| This is wrong. Remember that q and p are arrays of pointers. You're just overwriting q's newly acquired memory with the memory address of p. It should be *q[i] = *p[i]; (To dereference each pointer in the array and copy its contents.)
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #6 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| > q[i] = p[i]; I wouldn't be doing that. Copy each element manually, plus g[i]->next is the same as p[i]->next (not q[i]->next :-D |
| zacs7 is offline | |
| | #7 |
| Registered User Join Date: Apr 2009
Posts: 48
| oops. thanks. |
| budala is offline | |
![]() |
| Tags |
| array, duplicate, pointers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Returning an Array of Pointers to Objects | randomalias | C++ Programming | 4 | 04-29-2006 02:45 PM |
| two-dimensional dynamic array of pointers to classes | Timo002 | C++ Programming | 4 | 04-21-2005 06:18 AM |
| Passing pointers between functions | heygirls_uk | C Programming | 5 | 01-09-2004 06:58 PM |
| array of pointers to struct array | eth0 | C++ Programming | 1 | 01-08-2004 06:43 PM |
| array of pointers to structs | stumon | C Programming | 7 | 03-24-2003 07:13 AM |