Solved

arrays and pointer incrementing

Posted on 2006-11-01
4
548 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
ID: 17854251
{ar1[i] = ar2[i];}
0
 
LVL 45

Expert Comment

by:sunnycoder
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
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
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;
}

Cheers!
Exceter
0
 
LVL 8

Expert Comment

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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to define a string array with big size in c language 10 130
undefined reference to `bswap_128' 9 137
Raspberry Pi 3 to send text message 9 77
Why isn't object file created? 6 43
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 and use conditional statements in the C programming language.

863 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

20 Experts available now in Live!

Get 1:1 Help Now