Like Tree3Likes
  • 3 Post By rags_to_riches

UBound in c

This is a discussion on UBound in c within the C Programming forums, part of the General Programming Boards category; Is there any way of writing VB ubound(returns the index of the last element in the array) to c instead ...

  1. #1
    Registered User
    Join Date
    Oct 2012

    UBound in c

    Is there any way of writing VB ubound(returns the index of the last element in the array) to c
    instead of using sizeof operator
    int arr[20];
    printf("%d",sizeof array/sizeof(int));
    how to write using queue?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    As in you're implementing a queue? Then keep track of the start and end of the queue, or use a linked list where you track the tail.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Join Date
    Dec 2007
    You really should consider spending less time trying to convert some VB code and more time learning the C language.
    qny, Salem and laserlight like this.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Assuming you're working with an array, the technique sizeof(array)/sizeof(*array) is the usual technique.

    If you're working with a pointer rather than an array (and that includes passing an array to a function) there is no way in C. When an array name is converted to a pointer, that pointer is associated with no information about the size of the original array. There is therefore no way to extract such information.

    As laserlight suggested, if you're implementing a queue, then your implementation of the queue needs to do bookkeeping to keep track of the required length.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Tags for this Thread

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