Solved

arrays and pointer incrementing

Posted on 2006-11-01
4
543 Views
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;}
      
}
0
Comment
Question by:krampovpi
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
{ar1[i] = ar2[i];}
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
http://www.lysator.liu.se/c/c-faq/c-2.html

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.

Cheers!
sunnycoder
0
 
LVL 8

Accepted Solution

by:
Exceter earned 125 total points
Comment Utility
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;
}

Cheers!
Exceter
0
 
LVL 8

Expert Comment

by:Exceter
Comment Utility
>> 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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now