Perdes

Untitled

Nov 9th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(int argc, const char * argv[]) {
  4. printf("\n\n");
  5. // Code here
  6. int numberarray[5]; // Empty storage as slots don't have defined values.
  7. int *ptr = numberarray; // Starts at beginning of array rather than having to '&numberarray[0]'
  8. int value = 2;
  9.  
  10. for(;ptr<&numberarray[5]; ptr++)
  11. /*The ';' is there because the for loop doesn'tneed initialisation as the pointer is already pointing to the beginning of the array. If 'ptr' hasn't reached the address of element 5 (Represented by the '&' symbol) in the array then increment ptr value by 1 after every loop until it reaches the non-existent address of element 5. Arrays start from 0,1,2,3,4 which is 5 elements. When it reaches element 5 which doesn't exist, the loop breaks. Incrementation is represented by 'ptr++'*/
  12. {
  13. *ptr = value; // Follow the pointer '*' and set it to value.
  14. value = value * 2; // After each loop, 'value' is worth *2 its original value.
  15. }
  16.  
  17. int i;
  18. for(i=0;i<5;i++){
  19. printf("%d, ", numberarray[i]);
  20.  
  21. if(i==4){
  22. printf(".");
  23.  
  24. }
  25. }
  26.  
  27. // End of code
  28. printf("\n\n");
  29. return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment