Newbie Question: sizeof() problem in function

This is a discussion on Newbie Question: sizeof() problem in function within the C Programming forums, part of the General Programming Boards category; Hi. I'm a first year C student. I'm doing a homework assignment and I'm getting a strange result. We have ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6

    Newbie Question: sizeof() problem in function

    Hi. I'm a first year C student. I'm doing a homework assignment and I'm getting a strange result.
    We have to use an array to display our initials in rows and columns made out of the letters in the initials. My initials are SR, so I need to make a S out of S's and an R out of R's. Then we have to search the array to see how many letters are in the array.

    I made a function that would search the array using a for loop and a simple if statement. It will count each time it finds a specific character in the array. Then the function returns the total value.

    I want to use sizeof( array ); in my for loop initialization so it knows how far to go, but when I sizeof() inside the function, it only reports a size of "4". When I use sizeof() in my main function, it reports the proper size of "105". Since I'm only using one array, and I know how many elements are in it, I've cheated and just set my for loop to run 105 times.

    Anyone know what I'm missing? I have a feeling it's the way I've set up the array, but it's based on the examples we were shown in class on how to initialize arrays. Thanks for the help in advance!

    Code:
    /**
    *Program Name:  cis26Fall2009StevenRiedelLab1Ex5.c
    *Written By:    Steven Riedel
    *Discussion:    using char arrays
    */
    
    #include <stdio.h>
    
    /* Funcion is designed to take an array and a char
     * and then using a for loop, count the char's
     * in the array that match the key.
     */
    
    int searchArray( char search[], char key ) {
    
            /* line below to test the sizeof array to the passed array
             */
            printf( "size of array being passed: %d \n", sizeof( search ) );
    
            int i;
            int count = 0;
    
            /* the for statement I would like to use:
             * for ( i = 0; i < sizeof( search); i++ ) {
             */
    
            for ( i = 0; i < 105; i++ ) {
                    if ( search[ i ] == key )
                            count++;
            }
    return count;
    }
    
    
    int main( void ) {
    
            /* setting up counters to track the S's and R's
             */
    
            int rCount = 0;
            int sCount = 0;
    
            /* Making the SR out of a char array
             */
    
            char cAry1[ ] = " SSS   RRRRRR \n"
                            "S   S  R     R\n"
                            " S     R     R\n"
                            "  S    RRRRRR \n"
                            "   S   R    R \n"
                            "S   S  R     R\n"
                            " SSS   R     R";
    
            /* using puts to dispay SR
             */
    
            puts ( cAry1 );
    
            /* running array thought the searchArray() function.
             * and storing the returned value into sCount and rCount
             */
    
            /* Line below added to test the sizeof() function
             */
            printf( "Size of array in main method: %d \n", sizeof( cAry1 ) );
    
            sCount += searchArray( cAry1, 'S' );
            rCount += searchArray( cAry1, 'R' );
    
            /* print the final results of sCount and rCount
             */
    
            printf( "\nThere are %d S's and %d R's\n", sCount, rCount );
    
    return 0;
    
    }
    /*
    OUTPUT
     SSS   RRRRRR
    S   S  R     R
     S     R     R
      S    RRRRRR
       S   R    R
    S   S  R     R
     SSS   R     R
    Size of array in main method: 105
    size of array being passed: 4
    size of array being passed: 4
    
    There are 13 S's and 22 R's
    */

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    sizeof() doesnt work on an array inside a function as they are passed as a pointer. So, you will always get 4 bytes which is the size of a pointer.
    To get the sizeof an array where it is declared, you have to get the size of the entire array and divide it by the sizeof the first element.

    Like this -

    Code:
    size_t numElements = sizeof(myArray)/sizeof(*myArray);
    Spidey out!

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Unfortunately, you can't use sizeof() to determine the number of elements in an array which has been passed to a function.

    It works as you'd expect in the same function you declare an array. Applying sizeof() to this array
    Code:
    int array[10];
    would yield 10*4 = 40, and if you wanted the number of elements in the array, you could just divide by sizeof(array[0]). (This last step is, of course, unnecessary for arrays of chars.)

    But when you pass an array to a function, you're not *really* passing an array. You're really passing a pointer. You can see this by noting that
    Code:
    void function(char data[]);
    void function(char *data);
    are the same as far as the compiler is concerned. Passing entire arrays on the stack as parameters would be horribly inefficient, so you really just pass a pointer to the first element of the array . . . which is fine in most cases, but it can have unexpected results.

    You'll probably already have noticed that if you change an array in a called function, the changes are reflected in the caller. And it also means that when you apply sizeof() to an array which is passed into a function, you're really getting the size of a pointer (usually 4 on a 32-bit system).

    The solution is that you must pass in the size of the array to the function. If you made your function, for example,
    Code:
    int searchArray( char search[], int elements, char key ) {
    your loop could loop up to the elements variable and it would work as expected. In main(), of course, you could use sizeof() to determine the number of elements in the array, since the array is declared in main()'s stack space and so applying sizeof() to it will have the result you expect.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6
    Thank you guys!! Awesome help!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  3. Newbie problem with function coding..
    By jstevanus in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 08:12 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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