Arrays question

This is a discussion on Arrays question within the C Programming forums, part of the General Programming Boards category; New to C, so I'm kind of confused about how C works when it comes to arrays. If I'm given ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    7

    Arrays question

    New to C, so I'm kind of confused about how C works when it comes to arrays. If I'm given an array with n number of chars in it. How would I go about finding the size of the array, and then grabbing the last char of the array. I was told that C does not put a "null" at the end of an array so one could go on and on before actually finding a "\0" so I was wondering how I might do so using a for loop.

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    That's right. You can't find the size an array by any conventional means unless you store the size somewhere.
    For strings, C functions always put a \0 at the end of the array, so functions such as strlen can find the length of the string (in this case, the length of the array is length + 1).
    So if it's a string, then you can use strlen to get the length of the string and the array.
    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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    I was told that C does not put a "null" at the end of an array
    String literals automatically have a null character terminator. So "abc" is actually
    Code:
    {'a', 'b', 'c', '\0'}
    If I'm given an array with n number of chars in it. How would I go about finding the size of the array, and then grabbing the last char of the array.
    Consequently, it depends on how the array is to be interpreted. If the array is actually a null terminated string, you can loop through it until you find a '\0'. Note however that this is finding the string length, not the size of the array. strlen() is also available for use to avoid writing the loop yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    but what if I was given an array of binary numbers. Would it still fall under the same concept?
    ie.
    Code:
    {'1','0','1','0','\0'}

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    but what if I was given an array of binary numbers. Would it still fall under the same concept?
    "Binary numbers" is not a type. What you showed is effectively a string literal, "10100". If you are talking about say, an array of ints instead, then no, there will be no such null terminator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    oh sorry. So then who would i find the size/length of an array of ints?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    So then who would i find the size/length of an array of ints?
    As Elysia noted, you would store the size somewhere.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    I'm kind of confused. How would I store the size somwhere if the size varied everytime a value was passed to me?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tlovaj View Post
    I'm kind of confused. How would I store the size somwhere if the size varied everytime a value was passed to me?
    Whoever passed the array to you must also pass the size. Your function isn't psychic.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    You can use a struct, similar to C++'s vector.
    The struct stores the array and the size. Or you can just pass the array plus the size, which is pretty common in C to avoid buffer overruns.
    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.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    can you give me example code, its hard for me to imagine it.
    Last edited by tlovaj; 02-12-2008 at 12:29 PM.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    void printarray(int* arr, size_t arrsize)
    {
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by tlovaj View Post
    oh sorry. So then who would i find the size/length of an array of ints?
    Code:
    int arr[10];
    size_t s;
    s = sizeof (arr) / sizeof (*arr);
    printf("size is: %zu\n", s);

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Only works in the function you defined the array in, however, to due to the whole arrays decay into pointers ordeal (and you can't find the size of an array from a pointer).
    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.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Yes, but once it's passed to a function, it is no longer an array.

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

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM

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