Thread: array

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    array

    Are both of these correct?



    print to the screen the contents of the 7th element of an array of doubles named myArray.

    Code:
    printf("%d", myArray[6]);

    populate the 2nd element of an array of doubles named myArray by scanning the input from the keyboard.

    Code:
    scanf("%lf", myArray[2]);

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by CASHOUT View Post
    Are both of these correct?
    No.

    What makes you think that that array indexes count differently for printf() and scanf()
    Kurt

  3. #3
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by ZuK View Post
    No.

    What makes you think that that array indexes count differently for printf() and scanf()
    Kurt
    I don't think that at all. Our review for the upcoming test has a bunch of errors like this one. This was not my work, but the work of my instructor.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    %d means integer. So print 7th element of array of integers named myArray.

    Since before the [6] yield the 7th element, how can [2] yield the 2nd one????
    Array indexing starts from zero.
    So, if you have array[i], this means the (i+1)th element!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by CASHOUT View Post
    I don't think that at all. Our review for the upcoming test has a bunch of errors like this one. This was not my work, but the work of my instructor.
    Well typos happen
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by std10093 View Post
    Well typos happen
    This is true

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Also in your first printf, you are using a %d format specifier, which is for an integer. You are using a double, which uses a %f.

  8. #8
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    here's another question

    Print to the screen only the starting address of an array of 10 doubles named myArray.

    Code:
    printf("%d", myArray);
    Shouldn't there be a & symbol before myArray to signify that we want the address of and not the value of??

  9. #9
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    An array in memory is a pointer to the beginning of the array;

    Code:
    char array[] = "string";
    char * pc = array;
    
    printf( "beginning array address: %p\n
              pointer to array: %p\n" , array , pc );

  10. #10
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    okay

  11. #11
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    so would this:
    Code:
    printf("%d", myArray);
    be the same as this:
    Code:
     printf("%d", &myArray[0]);
    ?

  12. #12
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    No, it wouldn't.

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by CASHOUT View Post
    so would this:
    Code:
    printf("%d", myArray);
    be the same as this:
    Code:
     printf("%d", &myArray[0]);
    ?
    Yes, they are the same thing.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  14. #14
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Oops, you're right. They are the same, just are both wrong. You're passing a pointer while using an int specifier.

  15. #15
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Thank you all. I appreciate you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM