C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-13-2009, 06:01 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 48
"duplicating" an array of pointers

I have an array of pointers to a struct called p.

Code:
typedef struct node
	{
		int exp;
		float koef;
		struct node *next;
	} monomial;

monomial *p[5];
Some of the pointers are NULL some aren't and have value. I would like to have an array q which has the exact same values as p does, but not in a way that q[i] points to the same memory location as p[i]. I want q[i]->koef and q[i]->exp to have the same value as p[i]->koef and p[i]->exp but not to be in the same memory space.

I hope you understand me
budala is offline   Reply With Quote
Old 08-13-2009, 06:13 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 08-13-2009, 06:24 AM   #3
Woof, woof!
 
zacs7's Avatar
 
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.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 08-13-2009, 06:46 AM   #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   Reply With Quote
Old 08-13-2009, 06:49 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 08-13-2009, 06:50 AM   #6
Woof, woof!
 
zacs7's Avatar
 
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
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 08-13-2009, 08:16 AM   #7
Registered User
 
Join Date: Apr 2009
Posts: 48
oops. thanks.
budala is offline   Reply With Quote
Reply

Tags
array, duplicate, pointers

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22