arrays and pointer incrementing

Posted on 2006-11-01
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;}
Question by:krampovpi
  • 2
LVL 85

Expert Comment

ID: 17854251
{ar1[i] = ar2[i];}
LVL 45

Expert Comment

ID: 17855564
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.


Accepted Solution

Exceter earned 250 total points
ID: 17859116
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;


Expert Comment

ID: 17859148
>> 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.

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question