Copying Pointer Array data to another

This is a discussion on Copying Pointer Array data to another within the C Programming forums, part of the General Programming Boards category; If i want to copy the data from one spot in the structure to another and copy data from one ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    19

    Copying Pointer Array data to another

    If i want to copy the data from one spot in the structure to another and copy data from one structure to another.

    Code:
    Struct * Struct_1;
    Struct * Struct_2;
    
    Struct_2[2] = Struct_2[1];
    Struct_2[1] = Struct_1[1];
    This gives me an error during runtime.

    Suggestions?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are those pointers actually pointing at anything you've allocated?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Yes all of the data that is being stored was initialized, that part is working.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I will use my incredible powers of ESP to look at your code then and determine the problem.

    *ESP NOISES*
    AH HAH! I HAVE IT!

    Now you can use YOUR incredible powers of ESP to read my mind to get the answer of where your problem is.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Code:
      int i = 0;
      int j = 0;
      int Sorted = 0;
      widget *Widget_to_Sort_Ptr;
      widget  Widget_to_Sort;
    
      Widget_to_Sort_Ptr = &Widget_to_Sort;
    
        for(i = 0; i < array_size; i++)
        {
             j = i;
             Sorted = 0;
             while(Sorted == 0)
    	 {
    	     if(j == 0)
    	     {
    	          Widget_to_Sort_Ptr[j] = widget_array[j];
    	          Sorted = 1;
    	     }
    	     	     else if(widget_array[j].Pattern < Widget_to_Sort_Ptr[j-1].Pattern)
    	     {
    			  Widget_to_Sort_Ptr[j] = Widget_to_Sort_Ptr[j-1];
    	          Widget_to_Sort_Ptr[j-1] = widget_array[j];
    	     }
    	     else
    	     {
    	          Widget_to_Sort_Ptr[j] = widget_array[j];
    	          Sorted = 1;
    	     }
    	     j--;
    	 }
        }
    I get a buffer overflow runtime error when it reaches:
    Code:
      Widget_to_Sort_Ptr[j] = Widget_to_Sort_Ptr[j-1];
    	          Widget_to_Sort_Ptr[j-1] = widget_array[j];

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      widget *Widget_to_Sort_Ptr;
      widget  Widget_to_Sort;
    
      Widget_to_Sort_Ptr = &Widget_to_Sort;
    Unless 'Widget_to_Sort' is an array, then Widget_to_Sort_Ptr[j] is wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    So in essence, all you really want to do is to sort an array?

    You don't need to use pointers at all, and you certainly shouldn't point them at some stack variable.
    Your other problem is that you don't seem to understand how to swap two items using a third as a temporary. It requires three assignment statements.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Well I typically would want to just use an array for sorting obviously that would be simpler however the assigned prototype:

    void sorter( widget* widget_array, integer array_size);

    where widget_array is the incoming unsorted array, the widget structure just has a number and an ID as the two components of the structure. I could just throw everything from widget_array into an array however I dont think that is how its intended to be done.

    I think I figured out what to do for this situation, dynamic memory allocation. However when I try to allocate memory during runtime:

    Widget_to_Sort_Ptr = (widget *)malloc(sizeof(widget[array_size]));

    It gives me an error saying that I can not use a non-constant value. So how do I allocate memory during runtime to this pointer based off of the input array size into the function?
    Last edited by Panda125; 03-02-2011 at 09:05 AM.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Try Widget_to_Sort_Ptr = (widget *)malloc(array_size * sizeof(widget *));

  10. #10
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    Got everything working now, thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain this to me (beginner w/ pointers)?
    By klawson88 in forum C Programming
    Replies: 7
    Last Post: 10-02-2010, 11:07 PM
  2. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

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