• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

Understanding arrays, pointers with an example

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Understanding arrays, pointers with an example

Tagged: arrays, define command, index, pointers

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • November 9, 2021 at 9:02 am #117
    admin
    Keymaster


      Copy Code
      Copied
      Use a different Browser

      
      
      /* day_mon1.c -- prints the 
      days for each month */ 
      #include <stdio.h>
      #define MONTHS 12 
      int main(void)
      { 
      int days[MONTHS = {31,28,31,30,31,30,31,31,30,31,30,31}; 
      int index; for (index = 0;
      index < MONTHS; index++) 
      printf("Month %d has %2d 
      days.\n", index +1,
      days[index]);
      return 0; 
      }

      While trying to understand array, pointer in Week 2, I came across the above code (Source: C Primer Plus, Stephen Prata). Particularly, I cannot understand why days[index].

      How is that int days [MONTHS] is replaced by days[index]? Is it that an array which is MONTHS in this case can be replaced by other variables (index in this case) while carrying out printf?

      Not sure why this command will not compile:

      printf(“Month %d has %2d days.\n”, index +1, days[MONTHS]);

      To me, using days[MONTHS] instead of days[index] looks more plausible. I know I am missing in some key concept.


      Reply


      https://cs50.stackexchange.com/questions/42132/understanding-arrays-pointers-with-an-example[learn_press_profile]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.