Usually it is a bad practice to increment the pointer. Rather, one
should use the indexing variable to dereference the pointer at some
index. One more bug in your code was, you are allocating the space for
only 3 ints for "p" and storing 4 ints. Here is the code with some
modification.

#include <stdio.h>
int main(){

        int *p,i=0;
        void *x;
        p=(int *)malloc(4*sizeof(int));
        x=malloc(24);
        p[0]=1;
        p[1]=2;
        p[2]=3;
        p[3]=4;
        while(i<4){
                *(((int *)x)+i) = p[i];
                i++;
        }

        for(i=0;i<4;i++){
                printf("%d\n",p[i]);
                printf("%d\n",((int *)x)[i]);
        }
        return 0;
}


On Sun, Jun 5, 2011 at 6:19 PM, Piyush Sinha <ecstasy.piy...@gmail.com> wrote:
> @Amit Jaspal...there is a fundamental error in the code related to
> pointers....u have incremented the pointers p and x..so before the printf
> loop, pointer p (as well as x) is pointing to 4th location...I hope I am
> clear...
>
> this can be done by creating two more pointers that will initialised as the
> base location of p and x...
>
> #include <stdio.h>
> int main(){
>
>        int *p,*a,i=0;
>        void *x,*b;
>        p=(int *)malloc(3*sizeof(int));
>        a =p;
>        x=malloc(24);
>        b = x;
>        p[0]=1;
>        p[1]=2;
>        p[2]=3;
>        p[3]=4;
>        while(i<4){
>                *((int *)x)=*p;
>                ((int *)x)++;
>                p++;
>                i++;
>        }
>
>        for(i=0;i<4;i++){
>                printf("%d\n",a[i]);
>                printf("%d\n",((int *)b)[i]);
>        }
>
>
>     system("pause");
>         return 0;
> }
>
>
> On Sun, May 29, 2011 at 11:38 PM, amit <amitjaspal...@gmail.com> wrote:
>>
>> Suppose I want to copy an integer array to another array pointed by a
>> void pointer of different size.
>> Some thing like this is done probably in realloc function.
>>
>> The problem is it is not working for me ... here's the code
>>
>> #include <stdio.h>
>> int main(){
>>
>>        int *p,i=0;
>>        void *x;
>>        p=(int *)malloc(3*sizeof(int));
>>        x=malloc(24);
>>        p[0]=1;
>>        p[1]=2;
>>        p[2]=3;
>>        p[3]=4;
>>        while(i<4){
>>                *((int *)x)=*p;
>>                ((int *)x)++;
>>                p++;
>>                i++;
>>        }
>>
>>        for(i=0;i<4;i++){
>>                printf("%d\n",p[i]);
>>                printf("%d\n",((int *)x)[i]);
>>        }
>>        return 0;
>> }
>>
>>
>> Can anyone suggest what is happening wrong in this code.....or what
>> can be a better way to do this.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Piyush Sinha
> IIIT, Allahabad
> +91-8792136657
> +91-7483122727
> https://www.facebook.com/profile.php?id=100000655377926
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to