Im trying to learn how to use pointers and want to write a function where I have two arrays of integers and want to copy an array of integers for a specific number(nbr) of integers to copy.
int A[]= {1,2,3,4};
int B[] = {6,7,8,9};
int main(void)
{
int_copy(&A, &B, 3);
}
void int_copy(int *ptrA, int *ptrB, int nbr)
{
int * p = ptrA;
for (int i = 0; i<nbr; i++)
{
*ptrA++ = *ptrB++;
}
printf("%d\n", *ptrA);
printf("%d\n", *ptrB);
return;
}
I want to output 6,7,8,4 for array A, but i get the following error messages:
intcopy.c:10:14: warning: incompatible pointer types passing 'int (*)[4]' to parameter of type 'int *' [-Wincompatible-pointer-types]
int_copy(&A, &B, 3);
^~
intcopy.c:6:20: note: passing argument to parameter 'ptrA' here
void int_copy(int *ptrA, int *ptrB, int nbr);
^
intcopy.c:10:18: warning: incompatible pointer types passing 'int (*)[4]' to parameter of type 'int *' [-Wincompatible-pointer-types]
int_copy(&A, &B, 3);
^~
intcopy.c:6:31: note: passing argument to parameter 'ptrB' here
void int_copy(int *ptrA, int *ptrB, int nbr);
^
int_copy(&A[0], &B[0], 3);int_copy(A, B, 3);Ais a pointer to int,&Ais a pointer to array of 4 ints.Ais an array of four ints; it decays to a pointer when used in a context where a pointer is required...