Thread: Why should I set the number of elements as a parameter

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Why should I set the number of elements as a parameter

    Hello guys,

    I'm studying arrays... I came across this very common problem which is sorting an array. OK, Here's what I have got so far:
    Code:
    #include <stdio.h>
    
    void sort_list(int list[])
    {
        int i, j, temp, length = sizeof(list)/sizeof(int);
        for(i = 0; i < length - 1; i++)
        {
            for(j = i + 1; j < length; j++)
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
    }
    
    int main(void)
    {
        int values[] = {6, 2, 7, 11, 50, 12, 8};
        sort_list(values);
        int i = 0;
        for(i = 0; i < sizeof(values)/sizeof(int); i++)
            printf("%d\r\n", values[i]);
        return 0;
    }
    But the output was:
    Code:
    6
    2
    7
    11
    50
    12
    8
    I am surprised
    The output is like as there is not something called sort_list

    when I changed the version to be:
    Code:
    #include <stdio.h>
    
    void sort_list(int list[])
    {
        int i, j, temp, length = sizeof(list)/sizeof(int);
        for(i = 0; i < length - 1; i++)
        {
            for(j = i + 1; j < length; j++)
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
        for(i = 0; i < sizeof(list)/sizeof(int); i++)
            printf("%d\r\n", list[i]);
    }
    
    int main(void)
    {
        int values[] = {6, 2, 7, 11, 50, 12, 8};
        sort_list(values);
        int i = 0;
        for(i = 0; i < sizeof(values)/sizeof(int); i++)
            printf("%d\r\n", values[i]);
        return 0;
    }
    The output was more surprising:
    Code:
    6
    6
    2
    7
    11
    50
    12
    8
    a new 6 plus the first observation!!

    another version - which worked -:
    Code:
    #include <stdio.h>
    
    void sort_list(int list[], int n)
    {
        int i, j, temp;
        for(i = 0; i < n - 1; i++)
        {
            for(j = i + 1; j < n; j++)
            {
    			if(list[i] > list[j])
    			{
    				temp = list[i];
    				list[i] = list[j];
    				list[j] = temp;
    			}
            }
        }
        for(i = 0; i < n; i++)
            printf("%d\r\n", list[i]);
    }
    
    int main(void)
    {
        int values[7] = {6, 2, 7, 11, 50, 12, 8};
        sort_list(values, 7);
        int i = 0;
        return 0;
    }
    Now, the main difference between the working version and the others is passing the number of elements as a parameter to the sorting function... why is that so important that the function doesn't work unless it is present?

    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Yes, you need to pass the size to almost any function dealing with arrays.
    The only exception being the str... functions which mark the end of a char array with a \0.

    The sizeof()/sizeof() thing ONLY works when the array is in scope.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is another difference between your two snippets. The first sort function is missing the if so it's not really a sort function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jokes_finder View Post
    Now, the main difference between the working version and the others is passing the number of elements as a parameter to the sorting function... why is that so important that the function doesn't work unless it is present?

    Thanks in advance
    As the others would say ... the array name decays to a pointer when you pass it to the function.

    Basically the only time C knows the size of an array is in the scope block, marked by curly braces, where you created it... outside of that C doesn't have the first clue how big it is because it sees it as a pointer not the array itself. On your sort function sizeof() will return 4... the size of the pointer, not the size of the array. Dividing it by the size of an integer leaves you with 1 on a 32 bit machine or 2 on a 64 bit machine... not the number of elements in the array.

    And before you say it... yes, I agree... this is a serious shortcoming in the language....

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    this is the second bad point I come across.
    The first was the order of parameters.. consider the following example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int k = 35 ;
        printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
        return 0;
    }
    check your expected output with the real output.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jokes_finder View Post
    this is the second bad point I come across.
    The first was the order of parameters.. consider the following example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int k = 35 ;
        printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
        return 0;
    }
    check your expected output with the real output.
    For this code, expecting any output at all is the problem. I suppose it's not bad to know how your compiler handles this situation, but you'll want to be careful about trying to extrapolate this very far. But yes, this is one thing you need to keep in mind.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Code:
    printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
    I never have problems with code like this. Not because I understand how to write this kind of code, but precisely because I DON'T know how to write this kind of code - so I don't. I leave it for academics...

    To me it serves no purpose to gang up multiple, logically separate operations into single lines just because 'c' lets you. Half the time, what you end up with is compiler dependent or undefined, and EVERY time you end up with something that is nearly impossible to understand or troubleshoot! Even as puzzle fodder, I find little value. Why not useful, real-world tricks?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to multiply number elements in a string?
    By david.jones in forum C Programming
    Replies: 1
    Last Post: 05-04-2011, 05:50 AM
  2. Number of elements in multiarray
    By AcId9381 in forum C Programming
    Replies: 4
    Last Post: 12-10-2010, 12:54 PM
  3. number of non same elements
    By rafay_07 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2010, 12:40 PM
  4. number of elements in pointer
    By dayalsoap in forum C Programming
    Replies: 5
    Last Post: 05-11-2010, 11:40 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM