Stuck, arrays

This is a discussion on Stuck, arrays within the C Programming forums, part of the General Programming Boards category; thanks again guys! i might not drop this course at all... i was about to i've been stuck for 2.5 ...

  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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #20
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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 );
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    Katy, Texas
    Posts
    2,309
    OK, I didn't find it under man printf, but did find it under man 3 printf.

    Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

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

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09: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, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21