I have the following assumptions about the pointers. They can be interpreted as arrays as long as they are initialized to occupy the memory spaces required for "an array". I would like to understand a little more about how pointers improve the dynamics of a C program.
Here is a program that I think shows this transformation from a pointer to an array and how - a one-dimensional array is a constant pointer, a two-dimensional array will be a pointer to a constant pointer (type **). How could I print the array we have made, with a single for loop and using pointers?
/* matriz de n filas y cada fila de un número variable de elementos */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **p;
int n, m, i;
do {
printf("\n Numero de filas: "); scanf("%d", &n);
}while(n <= 0);
p = (int**) malloc(n*sizeof(int*));
for(i = 0; i < n; i++)
{
int j;
printf("Número de elementos de fila %d: ", i+1);
scanf("%d", &m);
p[i] = (int*)malloc(m*sizeof(int));
for(j = 0; j < m; j++)
{
printf("Valor del elemento p[%d][%d]: ", i+1, j+1);
scanf("%d", &p[i][j]);
}
}
// printf("%d", **p ); //Aqui va nuestra impresión de matriz
return 0;
}
As soon as I have time maybe I can post the answer. Thanks.
-----------------------------EDITION-------------------- ----------
Regarding the responses and comments I have received. I give an example where I illustrate a little more thoroughly my point of view. And also, show how memory can be freed, something I said to the air as a comment, not an assertion within my program.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float *pf = NULL;
int num, i;
do{
printf("Número de elementos del vector: ");
scanf("%d", &num);
}while(num < 1);
/* Asigna memoria: num*tamaño bytes */
pf = (float *) calloc(num, sizeof(float));
if(pf == NULL)
{
puts("Error en la asignación de memoria.");
return 1;
}
printf("Introduce %d valores \n", num);
for(i = 0; i < num; i++)
{
printf("Valor del elemento %i: ", i);
scanf("%f", &pf[i]);
/* proceso del vector */
}
for(i = 0; i < num; i++){
printf("pf[%i]: %f\n", i+1, pf[i]); printf("\tTamaño de pf[%i]: %d\n", i+1, sizeof(pf[i]));}
for(i = 0; i < num; i++){
printf("pf[%i]: %f\n", i+1,*pf); printf("\tTamaño de pf[%i]: %d\n", i+1, sizeof(*pf)); *pf++;}
/* liberación de la memoria ocupada */
free(pf);
for(i = 0; i < num; i++){
printf("pf[%i]: %f\n", i+1,*pf); printf("\tTamaño de pf[%i]: %d\n", i+1, sizeof(*pf)); *pf++;}
for(i = 0; i < num; i++){
printf("pf[%i]: %f\n", i+1, pf[i]); printf("\tTamaño de pf[%i]: %d\n", i+1, sizeof(pf[i]));}
return 0;
}
EXECUTION
N├║mero de elementos del vector: 2
Introduce 2 valores
Valor del elemento 0: 2
Valor del elemento 1: 2
pf[1]: 2.000000
Tamaño de pf[1]: 4
pf[2]: 2.000000
Tamaño de pf[2]: 4
pf[1]: 2.000000
Tamaño de pf[1]: 4
pf[2]: 2.000000
Tamaño de pf[2]: 4
pf[1]: 0.000000
Tamaño de pf[1]: 4
pf[2]: 0.000000
Tamaño de pf[2]: 4
pf[1]: 0.000000
Tamaño de pf[1]: 4
pf[2]: 0.000000
Tamaño de pf[2]: 4
Thanks for the valuable feedback. Especially PaperBirdMaster . Now it's a little clearer.