fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main()
  5. {
  6. int size;
  7. int *ia, *ip, *ia2;
  8. int i =0;
  9. char *ic;
  10.  
  11. //get the array size from the user
  12. printf("enter the array size: ");
  13. scanf("%d", &size);
  14. printf("\n entered %d\n", size);
  15.  
  16. //Allocate memory for the array
  17. ia = malloc(sizeof(int) * size);
  18. if (ia == NULL)
  19. {
  20. perror("Malloc ... ");
  21. exit(1);
  22. }
  23. printf("\n***************************\n");
  24. printf("\n***************************\n");
  25. for (i = 0; i < size; i++)
  26. ia[i] = rand() % 11;
  27.  
  28. //Example of pointer arithmetic and displaying array content using pointers
  29. //Pointers are incremented by the size of the object it’s pointing to
  30. for (ip = ia; ip < (ia + size); ip++)
  31. printf("0x%lx: \t %d\n", ip, *ip);
  32.  
  33. printf("\n***************************\n");
  34. printf("\n***************************\n");
  35.  
  36.  
  37. //display array content as characters
  38. ic = (char *)ia;
  39. for (i=0 ;i<size*4; i++)
  40. {
  41. printf("0x%lx: \t %c\n", ic, *ic);
  42. ic++;
  43.  
  44. }
  45.  
  46. //deallocate the memory for the array
  47. free(ia);
  48. }
  49.  
Success #stdin #stdout 0s 10320KB
stdin
2
stdout
enter the array size: 
 entered 2

***************************

***************************
0x2ae065fde030: 	 6
0x2ae065fde034: 	 10

***************************

***************************
0x2ae065fde030: 	 
0x2ae065fde031: 	 
0x2ae065fde032: 	 
0x2ae065fde033: 	 
0x2ae065fde034: 	 

0x2ae065fde035: 	 
0x2ae065fde036: 	 
0x2ae065fde037: