Thread: Merging Arrays!

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    7

    Merging Arrays!

    need help merging two arrays together using pointers! yikes i'm confused!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So am I. Merge them how? Are they sorted? Should the resulting array be sorted? Or do you want them interleaved, a[0], b[0], a[1], b[1]... regardless of sort order. Or do you just want to tack one on the end of the other, a[0]...a[n], b[0]...b[n]. Or something else entirely. Also, what do you mean by "using pointers"?

    Note, if you're looking for a free answer, you wont get one here. However, if you show us your code so far, and explain more clearly where you're stuck, we will help you.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    djwc77, are you talking about linked lists of statically allocated arrays?

    If its two static arrays, you need to find how many elements are in both, make an array of that size and begin tossing elements into it from one. Otherwise, I'm pretty sure it's impossible to 'merge' two static arrays. Hence I think why you're using pointers.

    Oh, are you allocating your arrays dynamically? Like,
    Code:
    int *x = malloc(number_of_elements*sizeof(*x));
    ?

    In that case, I think you could do a realloc, right?

    Either way, it's simple to insert a list inside another one but I think for dynamically allocated arrays, you'd need to allocate a new array or reallocate one of them.

  4. #4
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    If you store all your data in a list and store a reference to that data, then it's easy.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by djwc77 View Post
    need help merging two arrays together using pointers! yikes i'm confused!
    That's interesting.
    Perhaps if you could provide more information, or even some code of what you are dealing with, some other person might actually be able to provide some kind of useful feedback.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging arrays
    By ILovePrivacy in forum C Programming
    Replies: 1
    Last Post: 01-29-2012, 06:20 PM
  2. Merging two arrays into one
    By Thedon in forum C Programming
    Replies: 10
    Last Post: 10-30-2011, 01:34 PM
  3. Merging arrays help
    By key4life in forum C Programming
    Replies: 12
    Last Post: 12-05-2009, 06:46 PM
  4. merging arrays
    By acohrockz21 in forum C Programming
    Replies: 28
    Last Post: 03-09-2008, 02:28 AM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM