Thread: pointers

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    pointers

    Write a program which sets up two arrays inside main. The arrays should be the same size. One array must be initialised to some arbitrary values. The other array does not need to be initialised. Pass pointers to the arrays to a function called copystuff() which will copy the elements of the first array into the second array. The function need not return a value, unless you want it to. Main() must then print out the two arrays.

    #include <stdio.h>
    int copy_stuff (int [ ], int [ ]);
    void main (void)
    {
    int a[5]={1,2,3,4,5},b[5];
    copy_stuff(a,b);
    printf("%d",b);
    }
    int copy_stuff (int x[], int y[])
    {
    int *pointer;
    pointer = &x;
    y = *pointer;
    return y;
    }

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    that actually only changes a pointer it does not coby a to b

    this will copy a to b

    Code:
    void copy_stuff(int x[],int y[])
    {
        for(int i = 0;i < 5;i++)
            y[i] = x[i];
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    OK, that's a little better.
    Code:
    #include <stdio.h> 
    void copy_stuff (int [ ], int [ ]); 
    int main (void)    /* Main returns an int, not void */
    { 
       int a[5]={1,2,3,4,5},b[5]; 
       copy_stuff(a,b); 
       printf("%d",b);    /*This isn't how to print an array - I'll let you figure that one out*/
       return 0;
    } 
    void copy_stuff (int *x, int *y) 
    { 
       for (int i = 0; i < 5 ; i++)
          y[i] = x[i];
    }
    (Or use no-one's)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    A very simple function to print those arrays:

    #include <stdio.h>
    void copy_stuff (int [ ], int [ ]);
    void array_printer(int *, int *);
    int main (void) /* Main returns an int, not void */
    {
    int a[5]={1,2,3,4,5},b[5];
    copy_stuff(a,b);
    array_printer(a,b);
    return 0;
    }
    void copy_stuff (int *x, int *y)
    {
    for (int i = 0; i < 5 ; i++)
    y[i] = x[i];
    }
    void array_printer(int *x, int *y)
    {
    int i;
    for(i=0;i<5;i++)
    printf("\n %d\t%d",x[i],y[i]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM