We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


arrays and pointer incrementing

krampovpi asked
Medium Priority
Last Modified: 2008-02-01
I know this is homework I just need help to get to the next step

Write a program that initializes and array-of-double and then copies the contents of the array into 2 other arrays.  (All three arrays should be declared in the main program.)  To make the 1st copy, use a function with array notation.  To make the 2nd copy use a function with pointer notation and pointer incrementing.  Have each function take as arguments the name of the target array and the number of elements to be copied.  That is the function calls would like thi, given the following declarations:

 double source[5] = {1.1,2.2,3.3,4.4,5.5};
 double target1[5];
 double target2[5];
 copy_arr(target1, orig, 5);  
 copy_ptr(source,target, 5);

Below is what I have so far.  I need help getting started with the pointer part

#include <stdio.h>

void copy_arr(double source[], double target1[],int i);
void copy_ptr(double source[], double target1[],5);
int main(void)
    double orig[5] = {1.1,2.2,3.3,4.4,5.5};
    double target1[5];
      copy_arr(target1, orig, 5);  
    return 0;

void copy_arr(double ar1[] , double ar2[], int n)
    int i;
    for (i = 0; i < n; i++)
      {ar1 = ar2;}
Watch Question

Most Valuable Expert 2014
Top Expert 2015

{ar1[i] = ar2[i];}
Top Expert 2006

ozo correctly pointed out that you need to copy individual elements of the array inside the loop. It is not possible to assign an entire array to another

Function with pointers would be very similar to the one using arrays ... In fact a 1 D array, when passed as an argument, is received as a pointer with in the called function

These C FAQs should clarify the concept

To implement copy function using pointers, you need two pointers to traverse source and destination arrays respectively. This traversal would be in a loop very similar to the one you have in copy_arr function. Within the loop, you can either increment the pointers and assign or add loop counter to initial pointer values and then assign.

Note that incrementing a pointer makes it point to the next member of its *type*. e.g. if double are 4 bytes long and double * p is pointing to memory location 1000, then p+1 would point to memory location 1004 and NOT to 1001.

This is a good excersize to learn how to work with pointers. However, you can make your life somewhat easier with the library function memcpy. It behaves in an almost identical fashion when compared to your function. The only difference between the useage of the two is that memcpy can be used to copy the contents of any array. Not just arrays of type double. Consequently, memcpy specifies length in bytes and not array elements. So to copy 5 elements of type double you would use 5*sizeof(double) as the length argument in the function call. sizeof() is a preprocessor macro that returns the size of the given datatype in bytes. Since double is typically 4 bytes in length, depending on your compiler and hardware platform, 5 * sizeof(double) would probably return 20. So, for example, to copy the contents of the buffer, in your example, you would say,

memcpy( target1, orig, 5 * sizeof(double) );

Here's a complete example with strings.

#include <stdio.h>
#include <string.h>

int main ()
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  memcpy (str2,str1,strlen(str1)+1 * sizeof(char));
  memcpy (str3,"copy successful",16 * sizeof(char));
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

>> Since double is typically 4 bytes in length

I meant to say...

Since double is typically 8 bytes in length, depending on your compiler and hardware platform, 5 * sizeof(double) would probably return 40.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.