Created
January 17, 2018 00:19
-
-
Save wilsenhc/683a27eb572bd169956d4909a42d0f74 to your computer and use it in GitHub Desktop.
Prueba Incrementar Punteros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <math.h> | |
/* | |
Para compilar: | |
gcc -lm prueba-incrementar-punteros.c -o prueba | |
*/ | |
typedef struct { | |
int x; | |
int y; | |
} Punto; | |
int main() | |
{ | |
srand(time(NULL)); | |
int n = 10; | |
int *pIntDir; | |
char *pCharDir; | |
Punto *pPuntoDir; | |
// Declaramos un puntero a entero y reservamos espacio para n (= 10) enteros | |
int *arregloDinamicoEntero; | |
arregloDinamicoEntero = (int*) malloc(n * sizeof(int)); | |
// Llenamos el arreglo de valores aleatorios, utilizando notacion de array | |
for (int i = 0; i < n; i++) | |
arregloDinamicoEntero[i] = (rand() % 100) + 1; | |
// Recorremos e imprimimos el arreglo incrementando la direccion del puntero | |
printf("Con arreglo de Enteros creado dinamicamente:\n"); | |
printf("Tamaño del tipo de dato Enteros: %lu\n", sizeof(int)); | |
pIntDir = arregloDinamicoEntero; | |
for (int i = 0; i < n; i++) | |
{ | |
printf("pIntDir: %p - Valor: %d\n", pIntDir, *pIntDir); | |
pIntDir++; | |
} | |
// Declaramos un puntero a char y reservamos espacio para n (= 10) chars | |
char *arregloDinamicoChar; | |
arregloDinamicoChar = (char*) calloc(n, sizeof(char)); | |
// Llenamos el arreglo de valores aleatorios, utilizando notacion de array | |
for (int i = 0; i < n; i++) | |
{ | |
arregloDinamicoChar[i] = (rand() % 128); | |
} | |
// Recorremos e imprimimos el arreglo incrementando la direccion del puntero | |
printf("\nCon arreglo de Char creado dinamicamente:\n"); | |
printf("Tamaño del tipo de dato Char: %lu\n", sizeof(char)); | |
pCharDir = arregloDinamicoChar; | |
for (int i = 0; i < n; i++) | |
{ | |
printf("pCharDir: %p - Valor: %c\n", pCharDir, *pCharDir); | |
pCharDir++; | |
} | |
// Declaramos un puntero a entero y reservamos espacio para n (= 10) Punto | |
Punto *arregloDinamicoPunto; | |
arregloDinamicoPunto = (Punto*) calloc(n, sizeof(Punto)); | |
// Llenamos el arreglo de valores aleatorios, utilizando notacion de array | |
for (int i = 0; i < n; i++) | |
{ | |
arregloDinamicoPunto[i].x = (rand() % 100) + 1; | |
arregloDinamicoPunto[i].y = (rand() % 100) + 1; | |
} | |
// Recorremos e imprimimos el arreglo incrementando la direccion del puntero | |
printf("\nCon arreglo de Punto creado dinamicamente:\n"); | |
printf("Tamaño del tipo de dato Punto: %lu\n", sizeof(Punto)); | |
pPuntoDir = arregloDinamicoPunto; | |
for (int i = 0; i < n; i++) | |
{ | |
printf( | |
"pIntDir: %p - Valor: (%d, %d)\n", | |
pPuntoDir, | |
pPuntoDir->x, | |
pPuntoDir->y | |
); | |
pPuntoDir++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment