In your 'p' method, you're assigning pointer b to be pointer a, or, in otherwords, you're making b point to what a points to. Any changes to b will cause changes to a since they both wind up pointing to the same block of memory.

Use memcpy to copy blocks of memory. It would look something like this:

#include <string.h>
#include <stdlib.h>
void p(int *a){
   int *b = (int*)malloc(sizeof(int)*4);
   memcpy(b, a, sizeof(int)*4);

    //make changes to b.
   b[0] = 6;
   b[1] = 7;
   b[2] = 8;
   b[3] = 9;
}

int main(int argc, char **argv)
{
    int *a = (int*)malloc(sizeof(int)*4);

    // add values to a
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;

    p(a);

return 0;
}
Answer from Brandon Haston on Stack Overflow
๐ŸŒ
Coursecrux
coursecrux.com โ€บ posts โ€บ c-programs โ€บ pointers โ€บ 07-copy-array-to-another-pointers.html
Coursecrux | Copy one array to another using pointers
#include<stdio.h> #include<conio.h> #define MAX 50 void print_array(int arr[], int n); void main() { int arr1[MAX], arr2[MAX]; int n, i; int *ptr1, *ptr2; printf("Enter the size of the array\t:"); scanf("%d",&n); printf("\nEnter elements in the array\n"); for(i=0;i<n;i++) { scanf("%d",&arr1[i]); } ptr1 = arr1; //pointer ptr1 points to arr1[0] ptr2 = arr2; //pointer ptr2 points to arr2[0] printf("\nSource array before copying: "); print_array(arr1, n); printf("\nDestination array before copying: "); print_array(arr2, n); for(i=0;i<n;i++) { *ptr2 = *ptr1; *ptr1++; *ptr2++; } printf("\n\nSource array after copying: "); print_array(arr1, n); printf("\nDestination array after copying: "); print_array(arr2, n); getch(); } void print_array(int *arr, int n) { int i; for(i=0;i<n;i++) { printf("%d ",arr[i]); } }
Discussions

c - Copying array to array using pointer - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Like a question below, i have to use the pointer concept to copy array from one to another in ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 13, 2017
Copying arrays using pointers? - C++ Forum
Why is this, and how should I be copying these arrays with pointers? (I'm obviously doing it wrong) If I have used incorrect terminology or syntax then tell me, I'm struggling when it comes to conceptualizing pointers and references. ... You forgot to have p1 point back to the beginning. In something like this, I wouldn't be modifying the pointer. You can ... More on cplusplus.com
๐ŸŒ cplusplus.com
How to copy contents of one array to another using pointers in C? - Stack Overflow
Sign up to request clarification or add additional context in comments. ... So in this, first m values are copied to X from Temp. Then I need to increment 1 place and assign the rest (everything but the last) to y. how do i go about that? 2015-10-13T05:40:05.873Z+00:00 ... memcpy() is copying only half the array ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 13, 2015
c - Copying Array Using Pointers - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am trying to create a copy of an array by only accessing an array using pointer arithmetic. More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 18, 2019
๐ŸŒ
ProCoding
procoding.org โ€บ c-program-to-copy-one-array-to-another-using-pointers
C program to copy one array to another using pointers | ProCoding
March 9, 2024 - Pointers play a vital role in array manipulation, enabling efficient memory management and dynamic allocation. To copy one array to another in C, we leverage pointers to iterate through the elements of both arrays and perform the copy operation.
๐ŸŒ
Quora
quora.com โ€บ Can-you-explain-how-to-use-pointers-in-C-to-copy-an-array-into-another
Can you explain how to use pointers in C to copy an array into another? - Quora
Answer (1 of 2): The fastest way to copy an array from one to another in C is to use memcpy. It typically uses one binary opcode in the CPU to do the copying. https://www.geeksforgeeks.org/memcpy-in-cc/# Just be very careful that the array you ...
๐ŸŒ
HowStuffWorks
computer.howstuffworks.com โ€บ tech โ€บ computer software โ€บ programming
Using Pointers with Arrays - The Basics of C Programming | HowStuffWorks
March 8, 2023 - C actually encourages you to move it around using pointer arithmetic . For example, if you say p++;, the compiler knows that p points to an integer, so this statement increments p the appropriate number of bytes to move it to the next element of the array. If p were pointing to an array of 100-byte-long structures, p++; would move p over by 100 bytes. C takes care of the details of element size. You can copy the array a into b using pointers as well.
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to copy one array to another using pointers
C program to copy one array to another using pointers - Codeforwin
July 20, 2025 - Declare a pointer to source_array say *source_ptr = source_array and one more pointer to dest_array say *dest_ptr = dest_array. Copy elements from source_ptr to desc_ptr using *desc_ptr = *source_ptr.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 141271-how-copy-content-array-into-pointer.html
How to copy the content of an array into a pointer
September 22, 2011 - If you want to copy the array to a pointer, there are two main steps 1) Ensure the pointer points at a valid area of memory that can hold a copy of all elements of the array. 2) Copy each element of the array, individually, to the memory pointed to For example, in some function (using malloc() ...
Find elsewhere
๐ŸŒ
Blogger
welcome2protec.blogspot.com โ€บ 2020 โ€บ 12 โ€บ C-program-to-copy-one-array-to-another-using-pointers.html
C program to copy one array to another using pointers | welcome2protec
Let's have a look at the below program how to store array elements by taking input form user and how to copy it to another array using pointers. See the below step by step logic to copy one array elements to another array. Required knowledge- C Basic programming | Array | pointer | Array and pointer | C function
๐ŸŒ
Quora
quora.com โ€บ How-do-I-write-a-C-program-that-uses-pointers-to-copy-an-array-of-integers
How to write a C program that uses pointers to copy an array of integers - Quora
Answer (1 of 4): Iโ€™m not going to give you the answer, but I will explain some concepts. You may be confused by how C does array access. You cannot copy an array by trying to do an assignment. Say you have two integer arrays, a and b. You cannot copy a into b by doing: [code]b = a; [/code]In fa...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 111007
Copying arrays using pointers? - C++ Forum
Why is this, and how should I be copying these arrays with pointers? (I'm obviously doing it wrong) If I have used incorrect terminology or syntax then tell me, I'm struggling when it comes to conceptualizing pointers and references. ... You forgot to have p1 point back to the beginning. In something like this, I wouldn't be modifying the pointer. You can treat the pointer like an array: p1[I]. Random Note: You forgot to call delete to free resources.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-copy-all-the-elements-of-one-array-to-another-array
C Program to Copy an Array to Another Array - GeeksforGeeks
July 23, 2025 - #include <stdio.h> void copyArr(int ... is not done). This method is similar to the loop method but here, pointers are used instead of array names....
๐ŸŒ
Quora
cstdspace.quora.com โ€บ How-to-copy-one-array-to-another-using-the-C-programming-language
How to copy one array to another using the C programming language - C Programmers - Quora
Answer (1 of 8): The best way to do this is to first know what you are copying. Letโ€™s call it an X. X could b a char, int, or a struct. Then we need to know how many elements. Letโ€™s call that N. The correct call is to use memcpy as that is a library function that is very optimised.
๐ŸŒ
Lawrence
www2.lawrence.edu โ€บ fast โ€บ GREGGJ โ€บ CMSC270 โ€บ Pointers โ€บ arrays_and_pointers.html
Arrays and Pointers
This pointer points to the first element in the array. You can dereference that pointer to access the array element. *ptr = 12; // ptr points to the first location in the array. // This will set the first location in the array to 12. int y = *ptr; // Copy the value that ptr points to into y
Top answer
1 of 1
1

Here you are

int * arrCopy( const int *a, size_t n )
{
    int *b = malloc( n * sizeof( int ) );

    for ( size_t i = 0; i < n; i++ )
    {
        *( b + i ) = *( a + i );
    }

    return b;
}

Or you could check whether the memory was successfully allocated.

int * arrCopy( const int *a, size_t n )
{
    int *b = malloc( n * sizeof( int ) );

    if ( b != NULL )
    {
        for ( size_t i = 0; i < n; i++ )
        {
            *( b + i ) = *( a + i );
        }
    }

    return b;
}

Here is a demonstrative program

#include <stdio.h>
#include <stdlib.h>

int * arrCopy( const int *a, size_t n )
{
    int *b = malloc( n * sizeof( int ) );

    if ( b != NULL )
    {
        for ( size_t i = 0; i < n; i++ )
        {
            *( b + i ) = *( a + i );
        }
    }           

    return b;
}

int main(void) 
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    int *b = arrCopy( a, N );

    if ( b != NULL )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", b[i] );
        }

        putchar( '\n' );
    }

    free( b );

    return 0;
}

Its output is

0 1 2 3 4 5 6 7 8 9

As for your code then for example this expression

*(b+(sizeof(int)*i))

is invalid. It selects the element of the array b equivalent to

b[sizeof(int)*i]

So for example when i is equal to 1 and sizeof( int ) equal to 4 then instead of getting the element b[1] you are getting the element b[4].

From the C Standard (6.5.2.1 Array subscripting)

2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
Array copy, Is there a way to do this with pointers and save SRAM? - Programming - Arduino Forum
November 22, 2023 - I have some code which includes functionality whereby an array has another array copied in to it, which array gets copied in is dependent on various conditions. I then process (with reads and writes) the array to which the copying was done. Please see my pseudocode, with requirements explained within its comments: uint8_t AArray[10]={0};//sizes are fixed, and all guaranteed to be smaller than ArrayMaxSize uint8_t BArray[38]={0}; uint8_t CArray[25]={0}; #define ArrayMaxSize 40 uint8_t CopiedA...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 199113
Copy array from pointer - C++ Forum
October 5, 2016 - If you assign one pointer to another they will both point to the same array. ... Thanks krako. This works. Let me know if I understood it right. Using 'new' allocates a different memory to the array and is named as p. Because the previous instance of p does not exist once the function ends (but the data stays at the address), there is no issue of same name for multiple variables. Isn't data lost once the function ends? ... When you use new it creates the array on the free store(heap) and gives you a pointer to the first element, the data will exist until you delete it, it's not effected by scopes i.e when the function ends.