If you don't like pointers-to-pointers, you could always modify the function's signature so that it returns the new pointer-value to assign to A, and sets the dimension value via a pointer instead:
[...]
int dimension;
int * A = cargaArreglo(&dimension);
[...]
int * cargaArreglo(int * retDimension){
int i = 0;
char bandera = 's';
int * B = malloc( 1*sizeof(int) );
while( bandera != 'n' ){
printf( "Ingrese un numero" );
scanf( "%d", &B[i] );
printf( "Desea seguir ingresando?" );
i++;
fflush( stdout ); /* fflush( stdin ) is UB */
scanf( "%c", &bandera );
if( bandera != 'n' )
B = realloc( B, (i+1)*sizeof(int) );
}
*retDimension = i;
return B;
}
I mean, the teacher said that is impossible, because without the
double pointer I cant change the value of the global variable. This
made me confuse respect to the pointers.
What the teacher meant was that when you call a function in C, a temporary copy of each argument is pushed onto the stack for use inside the function. So if you change the value of the argument inside the called function, it doesn't change the value back in the calling function... for example, running this code:
void foo(int x, int * A)
{
x = 6;
A = NULL;
}
int x = 5;
int * A = &x;
printf("BEFORE x=%i A=%p &x=%p\n", x, A, &x);
foo(x, A);
printf("AFTER x=%i A=%p &x=%p\n", x, A, &x);
... will print out:
BEFORE x=5 A=0x2345678 &x=0x2345678
AFTER x=5 A=0x2345678 &x=0x2345678
... which is to say that foo() did not actually change the value of either x or A in the calling function, because foo() modified only the copy of x and the copy of A that were passed to it, and not the original variables.
However, foo() can use the A pointer to (indirectly) modify the value of calling function's x, because the function's copy of A still points to the calling function's x, so you could implement foo do this:
void foo(int * A)
{
*A = 666;
}
int x = 5;
printf("BEFORE x=%i\n", x);
foo(&x);
printf("AFTER x=%i\n", x);
... and you'd get this output:
BEFORE x=5
AFTER x=666
... and if the above makes sense to you, then the pointer-to-a-pointer case is really just the same thing as above, except that instead of A pointing to an int, the type of the value it is pointing to is a pointer:
void foo(int ** x)
{
*x = NULL;
}
int y = 5;
int * x = &y;
int ** A = &x;
printf("BEFORE x=%p\n", x);
foo(A);
printf("AFTER x=%p\n", x);
... would print out:
BEFORE x=0x2345845 (actual value will vary depending on your computer)
AFTER x=NULL