Thread: "ARRLEN" Role

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    21

    "ARRLEN" Role

    Hi everyone,
    I'm new to this forum and to C programming,so i'm looking forward for someone who can kindly help me with my currently newbie path...
    Can someone explain me how ARRLEN (array length) works?
    Is that a prototype or is a builded function that can be used?
    I almost studied C from the beginning to his algorythms and i can imagine that ARRLEN is a function that should be used to get back the size of the array, but i'm not really sure and i'm doing crazy to find the answer.
    Please help!! I'm sure i'll find kind people that knows the answer and maybe explaing better to me.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It being all uppercase, I'm going to make an informed guess and say it's a simple macro that simply uses "sizeof" on a string literal or on a static array, like this:
    Code:
    #include <stdio.h>
    #define ARRLEN(s) (sizeof(s)/sizeof(*s))
    
    int main(void)
    {
        int array[] = { 1, 2, 3, 4, 5 };
    
        printf("Array length: %u", ARRLEN(array));
    
        return 0;
    }
    Maybe it's some built-in macro of your compiler or maybe the code you're currently reviewing has it defined somewhere.
    Last edited by GReaper; 08-22-2017 at 09:04 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    21
    Thank you for help. When you declare ARRLEN why that (s) nearby? ( /*ARRLEN(s)*/ ).
    Can we use this even with pointers as you did? ( /* /sizeof(*s) */ ).
    And at least,what do you mean by "has it defined somewhere"?
    By the way i want to clear out that I'll have soon an C prog exam so i need to clear all my doubts.
    If that's a macro,so a symbolic costant, the scary ARRLEN name is a whatsoever name that can even be called "MOMMY" right?
    If you or someone else continue explaining you'll give me a big help
    Last edited by Alcatraz; 08-23-2017 at 02:43 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Macros can take arguments, that's what the "(s)" stands for in this context. "s", being the representation of the passed argument, is being substituted with the value you gave it when you use it. For example, in the code I posted:
    Code:
    printf("Array length: %u", ARRLEN(array));
    // Becomes
    printf("Array length: %u", (sizeof(array)/sizeof(*array)));
    Yes, you can use sizeof with anything. You can use it with variables as well as types. To get the correct size for arrays though, it must be a static array in a context the compiler already knows about. For example, if you pass a static array to a function and call sizeof there, the compiler only sees a pointer, it can't know the real size.

    By "defined elsewhere" I mean exactly that, it may be defined in a header file, another part of the source file or even in the compiler's global defines.

    The macro name is of course whatever its creator wants it to be.
    Last edited by GReaper; 08-23-2017 at 05:25 AM.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    21
    Indeed I thought that when i red about "reserved words" for C and connecting that it's just a sizeof
    I've been searching everywhere for this little mind trick. Thank you for clearing my doubts!
    Can I write again if I have some problems in this preparation days?

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You are welcome to ask any questions you want, but keep in mind that sometimes it's better to solve something without help.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread