i want to copy element of first array to the second array with pointer but after compiling i get message that is A problem caused the program to stop working correctly.windows will close the program and notify you if a solution is avilable
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[30],arr1[30];
int n,i;
int *p,*q;
p=arr;
q=arr1;
printf("Enter the no. of elements of array:-\n");
scanf("%d",&n);
printf("Enter the array element of 1st array:-\n");
for(i=0;i<n;i++)
{
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
printf("elements of array 1=");
for(i=0;i<n;i++)
{
printf("%d , ",*(p+i));
}
printf("\nelements of array 2=");
for(i=0;i<n;i++)
{
printf("%d , ",*(q+i));
}
for(i=0;i<n;i++)
{
printf("%d , ",arr1[i]);
}
return 0;
}
Change
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
To
for(i=0;i<n;i++)
{
q[i]=p[i];
}
As of now you are treating int
as int *
and assigning it to pointer leading to undefined behaviour.
Also *(p+i)
is same as p[i]
and (p+i)
is same as &p[i]
. But latter ones are more readable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.