Hogy-Bear

usefulkinda

Mar 12th, 2018
225
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char const *argv[]) {
  4.  
  5.   /*
  6.   Okay so an actual practical example.
  7.   notice I only create 3 ints
  8.   but lets say i enter 4 as my input
  9.   then i will have used 4 extra numbers twice without
  10.   needing variable names.
  11.   All just memory magic baby!
  12.  
  13.  
  14.   WARNING: you may get a segmentation fault
  15.   but that doesnt actually impact the programs performace.
  16.   */
  17.  
  18.   int start;
  19.   int *ptr = &start;
  20.  
  21.   int total = 0;
  22.  
  23.   printf("how many numbers would you like to add: ");
  24.   scanf("%d", &start);
  25.   for (size_t i = 0; i < start; i++) {
  26.     ptr++;
  27.     printf("please enter a number: ");
  28.     scanf("%d", ptr);
  29.     total += *ptr;
  30.   }
  31.   printf("0");
  32.   for (size_t i = start; i > 0; i--) {
  33.     printf(" + %d", *ptr);
  34.     ptr--;
  35.   }
  36.   printf(" = %d\n", total);
  37.  
  38.   return 0;
  39. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment