I am trying to learn a pointer in C, and I am writing this exercise with a small integer, but have encountered an invalid sizeof application on an incomplete type problem int[] . Please tell me where I made a mistake and how to solve it. Thanks.
#include <stdio.h> int intA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int intB[]; void int_copy(const int *source, int *destionation, int nbr) { int i; for(i=0;i<nbr;i++) { *destionation++ = *source++; } } int main() { int *ptrA = intA; int *ptrB = intB; int sizeA = sizeof(intA); int nbrA = sizeof(intA)/sizeof(int); printf("\n\n"); printf("[Debug]The size of intA is:%d\n", sizeA); printf("[Debug]That means the number of elements is:%d\n", nbrA); printf("\n\nThe values of intA are:\n"); int i; for(i=0;i<nbrA;i++) { printf("[%d]->%d\n", i, intA[i]); } int_copy(ptrA, ptrB, nbrA); int sizeB = sizeof(intB); int nbrB = sizeof(intB)/sizeof(int); printf("\n\n"); printf("[Debug]The size of intB is:%d\n", sizeB); printf("[Debug]That means the number of elements is:%d\n", nbrB); printf("\n\nThe values of intB are:\n"); for(i=0;i<nbrB;i++) { printf("[%d]->%d\n", i, *ptrB++); } }
The strange thing that I observed when I ran gdb was that I controlled the copy function int_copy to work 9 times, which seems correct, but printing the intB after the copy function displays only one element in this array.
I am still fighting for pointers, so please help me and forgive my ignorance. Thank you very much.
source share