hello i m given a question in book i m reading that i copy element but using ptr to copy and other choice will be using normal integers to copy
i did normal integer but copy i think i got a problem with it

#include <stdio.h>
int copyarr(const int Mainarr[],int Target[], int * end);

int copyarr(const int Mainarr[],int Target[], int * end)
{
    while (Mainarr < end)
        *Target=*Mainarr++;
}

int main(void)
{
    int const Mainarr[]={1,2,3};
    int target[2];
    int *end;
    end=target+3;
    copyarr(Mainarr,target,end);
    printf("%d %d %d\n",target[0],target[1],target[2]);//lazy to do a for loop :P
    getchar();
    return 0;
}
#include <stdio.h>
int copyarr(const int Mainarr[],int Target[], int * end);

int copyarr(const int Mainarr[],int Target[], int * end)
{
    while (Target < end)  //change need to be done here
        *Target++=*Mainarr++;  //change need to be done here
}

int main(void)
{
    int const Mainarr[]={1,2,3};
    int target[2];
    int *end;
    end=target+3;
    copyarr(Mainarr,target,end);
    printf("%d %d %d\n",target[0],target[1],target[2]);//lazy to do a for loop :P
    getchar();
    return 0;
}

oh but why isnt Mainarr same size as Target why when u made Target<end and yah i forgot to increment target

also i made the function here but i get a variable added to the array which is -1 i dunno why its being added

#include <stdio.h>
#define SIZE 5
/*CopyElement.c*/
/*it will return num of elements copied and choice
/*if wether or not we want to use user pointers calculating or normal calculation*/

int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice) {
    int elments=0;
    switch(choice) {
        case 0:
        {
            int i;
            for(i=0;Mainarr[i]<end2;i++)
                target[i]=Mainarr[i];
            elments=i;
        }
            break;
        case 1:
            while(target<=end) {
                elments++;
                *target++=*Mainarr++;
            }
            break;
    }
    return elments;
}
void Showarr(int array[],int end) {
    int i;
    for(i=0;array[i]<=end;i++)
        printf("%d\n",array[i]);
}
int main(void)
{
    int  const Main[]={1,2,3,5,6,7};
    int Copy[SIZE];//will Copy Elements of Main
    int result;
    result=CopyElements(Main,Copy,Copy+SIZE,sizeof Main,1);
    printf("alright you copied %d elements\n",result);//says i copied only 6 elments not more
    puts("okay now showing it");
    Showarr(Copy,sizeof Copy);
    return 0;
}

also one last question i never i understood why when i have in a for loop and i wanna automatic end when it reached the sizeof the array like for example this code

#include <stdio.h>
int main(void)
{
    int arr[]={1,2,3};
    int i;
    //this works
    for(i=0;arr[i]<sizeof arr;i++)
         printf("%d ",arr[i]);
    //this doesnt
    for(i=0;arr[i]<sizeof arr;i++)
         printf("%d ",arr[i]);
    getchar();
    return 0;
}

i dunno why first one works and 2nd one doesnt ?maybe because first 1 which is i ofcoz it doesnt work coz i m compring i to a sizeof which i think has couple hundered so it will keep incrementing i ?
and 2nd one coz i m compring addresses ?
please someone enlighten me .

oh but why isnt Mainarr same size as Target why when u made Target<end and yah i forgot to increment target

(Assumption an int is taking 1 byte memory)
Here lets say memory location of Mainarr = 1000

target is 2000

end is 2003(2000+3)

Target and end are co-related by the number of elements in Mainarr.

so loop stops after 3 iterations.

while (Mainarr < end) //loop never stop here

above case loop will never stop.

The below code gis completely wrong one.

for(i=0;arr[i]<sizeof arr;i++)

arr is the element of the array arr.
sizeof arr is total bytes of memory reserved for arr.
******How can you compare in between total byte with the data value.

corrected code below:
======================

#include <stdio.h>
int main(void)
{
    int arr[]={11,27,3,4,5,6};
    int i=sizeof arr;
    printf("size : %d\n",i);  //check the total size
    //this works
    for(i=0;i<sizeof arr/sizeof arr[0];i++) //corrected place
         printf("%d ",arr[i]);
    getchar();
    return 0;
}

rule:Think in terms of address and while comparing ensure that both are of same type.

well i fixed it but i dunno why before i had -1

#include <stdio.h>
#include <windows.h>
#define SIZE 6
int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice);
/*CopyElement.c*/
/*it will return num of elements copied and choice
/*if wether or not we want to use user pointers calculating or normal calculation*/

int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice) {
    int elments=0;
    switch(choice) {
        case 0:
        {

            int i;
            for(i=0;i<end2;i++)
                target[i]=Mainarr[i];
            elments=i;
        }
            break;
        case 1:
            while(target<=end) {
                elments++;
                *target++=*Mainarr++;
            }
            break;
    }
    return elments;
}
void Showarr(int array[],int end) {
    int i;
    for(i=0;i<end;i++)
        printf("%d\n",array[i]);
}
int main(void)
{
    int  const Main[]={1,2,3,5,6,7};
    int Copy[SIZE]={0};//will Copy Elements of Main
    int result;
    result=CopyElements(Main,Copy,Copy+SIZE,sizeof Copy/sizeof Copy[0],0);
    printf("alright you copied %d elements\n",result);//says i copied only 6 elments not more
    puts("okay now showing it");
    Showarr(Copy,sizeof Copy/sizeof Copy[0]);
    return 0;
}
int CopyElements(int const Mainarr[],int target[],int *end,int end2,int choice) {
    int elments=0;
    switch(choice) {
        case 0:
        {

            int i;
            for(i=0;i<end2;i++)
                target[i]=Mainarr[i];
            elments=i;
        }
            break;
        case 1:
            while(target<=end) {   //it should be while(target<end)
                elments++;
                *target++=*Mainarr++;
            }
            break;
    }
    return elments;
}
This question has already been answered. Start a new discussion instead.