Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:41:26+00:00 2026-06-01T16:41:26+00:00

I want to merge 2 arrays into 1. For example: A1= 1,1 A2= 2,2

  • 0

I want to merge 2 arrays into 1. For example:

  • A1= 1,1
  • A2= 2,2
  • then A3 = 1,2,1,2

For example:

  • A1= 1
  • A2= 2,2,2,2
  • then A3 = 1,2,2,2,2

For example:

  • A1= 1,1,1,1
  • A2= 2,2
  • then A3 = 1,2,1,2,1,1

In last example, when I ran my code, I got 1,2,1,2,1,20.

In the second last, I got 1,2,32767,2,2.

So I guess I have a wrong code. Right after the I finished taking the element of the shorter array and fill up all the rest of the A3 with whoever is longer. But I couldn’t figure out why — can you help me?

code:

int *p3=arr3;   //arr3 is A3 for example, arr1 = A1..etc, all sizes are defined
int index;
int index1=0;
int index2=0;

for(index = 0; index< sizeofArr3 ; index++)
{
    if(index%2==0)
    {
        if(index1<=sizeofArr1)
            *(p3++) = arr1[index1++];
        else
            *(p3++) = arr2[index2++];
    }
    else 
    {
        if(index2<=sizeofArr2)
            *(p3++) = arr2[index2++];
        else
            *(p3++) = arr1[index1++];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:41:27+00:00Added an answer on June 1, 2026 at 4:41 pm

    It’s this line:

    if (index1 <= sizeofArr1)
    

    and the equivalent one for index2 and sizeofArr2. You should be using < rather than <=.

    The reason has to do with C’s zero-based arrays. For an array of size N, the element indexes are 0 through N-1 inclusive. Because you’re allowing it to access element N (the N+1th element), you’re actually invoking undefined behaviour.

    Theoretically, the implementation can do anything in that case, up to and including destruction of the universe. I guess you’re lucky that it just decided to give you results that were slightly awry 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 same-length arrays and I want to merge them into first array
I have two arrays, I want to merge these two arrays into single array.
I have 2 images and I want to merge them into one on my
I have 2 arrays that I want to merge based on a key's value
I have two sequential (non-associative) arrays whose values I want to combine into a
I have 2 arrays like below and want to merge them together and not
I have two arrays array1 and array2. I want to merge these two arrays
If I have two arrays both with 10000 items, now I want to merge
I want to merge two arrays and replace the text with strtr function. I
I have to array and I want to merge this two array with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.