Advertisement
MusicFreak

Programiranje 22.05.2015 - Urosevic

Apr 22nd, 2015
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1.                                 POKAZIVACI
  2.  
  3.  
  4. #include <stdio.h>
  5.  
  6. main()
  7. {
  8.     short a[]={1, 4, 6, 8, 10, 12, 14, 16, 18, 20}, *p;
  9.     p = a+4;
  10.     printf("*p = %d\n", *p);
  11.     printf("p = %p\n", p);
  12.     p++;
  13.     printf("p = %p\n", p);
  14. }
  15.  
  16.  
  17. ALOKACIJA MEMORIJSKOG PROSTORA
  18. <stdlib.h>
  19.  
  20. 1) malloc(int) ---- pokusava da pronadje slobodnu zonu u memoriji velicine koje smo mi naveli u zagradama.
  21. int *p;
  22. p = malloc(100*sizeof(int));
  23. p[0] p[1] ... p[99]
  24. if (p == NULL)
  25.  
  26. 2) free(p)
  27.  
  28. 3) calloc(int, int)
  29.  
  30. Primjer malloc
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. main()
  34. {
  35.     int *q;
  36.     int i, j;
  37.     q = malloc(100*sizeof(int));
  38.     if (q == NULL){
  39.         printf("Nema dovoljno memorije\n");
  40.         exit(1);
  41.     }
  42.     for (i = 0; i < 100; i++)
  43.         q[i] = i * i + 10;
  44.     for (i = 0; i < 10; i++)
  45.         printf("*q[%d] = %d\n", i, q[i]);
  46.     free(q);
  47. }
  48.  
  49.  
  50.                                     FUNKCIJE
  51.  
  52.  
  53. tipR imeF (tip1 Arg1, tip2 Arg2, ... , tipk Argk)
  54. {
  55. ...
  56. }
  57.  
  58. 1)
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. int min3(int a, int b, int c)
  62. {
  63.     int r;
  64.     r = a;
  65.     if (b < a) r = b;
  66.     if (c < a) r = c;
  67.     return r;
  68. }
  69. main ()
  70. {
  71.     int x, y, z, u;
  72.     printf("x, y, z = ");
  73.     scanf("%d%d%d", &x, &y, &z);
  74.     u = min3(x, y, z);
  75.     printf("u = %d\n", u);
  76. }
  77.  
  78. 2)
  79. #include <stdio.h>
  80. #include <stdlib.h>
  81. int nzd(int p, int q)
  82. {
  83.     int d;
  84.     if (p < 0) p = -p;
  85.     if (q < 0) q = -q;
  86.     if (p == 0) return q;
  87.     if (q == 0) return p;
  88.     if (p < q) d = p; else d = q;
  89.     while ((p % d != 0) || (q % d != 0)) d--;
  90.     return d;  
  91. }
  92. main()
  93. {
  94.     int x, y;
  95.     printf("x, y = ");
  96.     scanf("%d%d", &x, &y);
  97.     printf("nzd je: %d\n", nzd(x, y));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement