Thread: How to return the last element of an array?

  1. #1
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24

    How to return the last element of an array?

    Hi guys,

    I'm trying to create a function that will return the last element of an array. I am working in ANSI C.

    Code:
    char *top(void) {
        int i;
        /* Check for the last element */
        for(i = 0; stack[i] != '\0'; i++) {
    	
        }
        /* Return the last element of the array */
        return ???;
    }
    I have no idea how to go about checking for the last element of the array.

    Help would be GREATLY appreciated!

    Kind regards,
    BuezaWebDev
    Last edited by BuezaWebDev; 03-25-2005 at 02:50 AM.

  2. #2
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    What is stack declared as? Is it an int, char, etc?

  3. #3
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24
    Oops, sorry. It's an integer.

    int stack[10];

  4. #4
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    int top(void) 
    {
        int i;
        for(i = 0; stack[i] != '\0'; i++)
    	;
    
        return stack[--i];
    }

  5. #5
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24
    Yay, l0cke!

    Thank you so much! So basically, the loop will keep iterating until the end (once it hits the null character), and then integer i will be set to the last index number?

    I would have never thought of that logic! Thank you!

  6. #6
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    i is set to the last index number + 1, that's why the return is return stack[--i], so it subtracts 1 from i before it returns it.
    Last edited by l0cke; 03-25-2005 at 03:31 AM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What if there are no elements in the array? What if array[0] == '\0' and you then try and return stack[-1]?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    or u can do this

    Code:
    return stack[strlen(stack)-1];
    s.s.harish

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by ssharish
    or u can do this


    Code:

    return stack[strlen(stack)-1];


    s.s.harish
    First, let me say that the use of '\0' in an integer array as an end of array indicator is suspect. It makes little sense.

    You would not want to use strlen with an integer array. Disregarding for the moment that the op is using an integer array (pretending that it is a NULL terminated character array), you have the same problem using strlen... what if strlen returns 0? Then you are still returning stack[-1]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    lol, sorry if it is an interger array please ignore my post

    s.s.harish

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int array[] = {1,2,3,4,5,6,7,8,9};
       int last_element = array [ sizeof array / sizeof *array - 1 ];
       printf("last_element = %d\n", last_element);
       return 0;
    }
    
    /* my output
    last_element = 9
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    From what I can understand, the array might not be filled to the end, and we want to get the last element of the stack. ( assuming you are implementing a integer stack. ).

    A return value of END ( -999) indicates that the stack is empty.

    Code:
    #define END -999
    
    int top(void) 
    {
         int i;
        /* Check for the last element */
    
        for(i = 0; stack[i] != END ; i++) ;
       
     /* Return the last element of the array */
        i--;
        if (i <0 )
             return END;
        else
           return stack[i];
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If this is a stack, why on earth aren't you storing where the top of it is? Increment it when you push, decrement it when you pop. Any other function call is a waste of time.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Web Developer
    Join Date
    Mar 2005
    Location
    Vancouver, Canada
    Posts
    24
    In my int main(void), I have variables that store the "top" integer.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why are you wasting time finding the last element instead of directly referencing top?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM