Thread: "duplicating" an array of pointers

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    74

    "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

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    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];
    	}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > 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

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    74
    oops. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM

Tags for this Thread