Hogy-Bear icon

usefulkinda

Hogy-Bear | PRO | 03/12/18 10:25:49 PM UTC | 0 ⭐ | 186 👁️ | Never ⏰ | []
C |

810 B

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
int main(int argc, char const *argv[]) {
 
  /*
  Okay so an actual practical example.
  notice I only create 3 ints
  but lets say i enter 4 as my input
  then i will have used 4 extra numbers twice without
  needing variable names.
  All just memory magic baby!
 
 
  WARNING: you may get a segmentation fault
  but that doesnt actually impact the programs performace.
  */
 
  int start;
  int *ptr = &start;
 
  int total = 0;
 
  printf("how many numbers would you like to add: ");
  scanf("%d", &start);
  for (size_t i = 0; i < start; i++) {
    ptr++;
    printf("please enter a number: ");
    scanf("%d", ptr);
    total += *ptr;
  }
  printf("0");
  for (size_t i = start; i > 0; i--) {
    printf(" + %d", *ptr);
    ptr--;
  }
  printf(" = %d\n", total);
 
  return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎