Thread: Stuck, arrays

  1. #16
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    thanks again guys! i might not drop this course at all... i was about to i've been stuck for 2.5 days

    Code:
        rooms[num_rooms-1].description = "test element before last";
        printf(rooms[num_rooms-1].description);
        printf("%\n");
        rooms[num_rooms].description = "test element last";
        printf(rooms[num_rooms].description);
        printf("%\n");
        rooms[0].description = "test element 1";
        printf(rooms[0].description);
        printf("%\n");
    works

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by NoobieGecko View Post
    thanks again guys! i might not drop this course at all... i was about to i've been stuck for 2.5 days

    Code:
        rooms[num_rooms-1].description = "test element before last";
        printf(rooms[num_rooms-1].description);
        printf("%\n");
        rooms[num_rooms].description = "test element last";
        printf(rooms[num_rooms].description);
        printf("%\n");
        rooms[0].description = "test element 1";
        printf(rooms[0].description);
        printf("%\n");
    works
    No it doesn't. Read up on array indices again.

  3. #18
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    you're right, I get blanks, why is that?
    i'm going to try to malloc for character size to see if it works...

    Code:
    
        if (rooms==NULL){
            printf("panic");
        }
    
        rooms[num_rooms-1].description = "test element before last";
        rooms[num_rooms].description = "test element last";
        rooms[0].description = "test element 1";
    
        //printf(rooms[num_rooms-1].description);
        printf("%\n");
        rooms[num_rooms].description = "test element last";
        //printf(rooms[num_rooms].description);
        printf("%\n");
        rooms[0].description = "test element 1";
        //printf(rooms[0].description);
        printf("%\n");

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arrays start from 0, so if you request space for 10 structs, you get structs elements from 0 to 9.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. if you allocate num_rooms elements - you can access indexes from 0 to num_rooms-1

    rooms[num_rooms] will access out of bounds memory

    2. if you want do like this
    rooms[num_rooms-1].description = "test element before last";

    declare description as const char* pointer

    3. to print it use
    printf("%s", rooms[0].description );
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Thanks:
    Code:
        rooms[num_rooms-1].description = "test element last";
        rooms[0].description = "test element 1";
    
        printf("%s", rooms[num_rooms-1].description);
        printf("\n");
        printf("%s", rooms[0].description);
        printf("\n");
    output:
    test element last
    test element 1

    i might have to ask u guys more questions later, is that okay? i'm working on the next part! thanks again!

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure. You got question, you ask them and we answer them.
    It's a good thing. You'll learn plenty.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Todd Burch View Post
    Code:
    printf("sizeof(room) = %d\n", sizeof(myroom) );
    sizeof returns size_t, so use %zu instead:
    Code:
    printf("sizeof(room) = %zu\n", sizeof(myroom) );

  9. #24
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by robwhit View Post
    sizeof returns size_t, so use %zu instead:
    Code:
    printf("sizeof(room) = %zu\n", sizeof(myroom) );
    I can't find %z. What's %z? Got a link?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd Burch View Post
    I can't find %z. What's %z? Got a link?

    Todd
    From man printf:
    z

    A following integer conversion corresponds to a size_t or ssize_t argument. (Linux libc5 has Z with this meaning. Don't use it.)

  11. #26
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OK, I didn't find it under man printf, but did find it under man 3 printf.

    Thanks.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Note that z is not C89, but C99.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Stuck again: Arrays
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 12:37 AM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM