Thread: sizeof in a function

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    sizeof in a function

    does not seem to matter how I pass it, I only get two elements in a function. BUT, I think the real question here is how to pass an array, then do sizeof on it, to elevate the ( an ) unknown size of the array?


    Code:
    #include <stdio.h>
    
    void fun_array(int  a[]) 
    //void fun_array(int* a) 
    {
        int size = 0, j;
        size = sizeof(&a)/sizeof(int);
        //size = sizeof(a)/sizeof(a[0]);
        printf("size %d\n", size);
        for (j = 0; j < size; j++)
            printf("%d\n", a[j]);
         
    }
     
    void fun_array2(int d[])
    {
     for (int s = 0; s < 6; s++)
            printf("d = %d\n", d[s]);
        
    }
    int main()
    {
        int a[10] = {1, 2, 3, 4, 5, 6};
        fun_array(a);
        
        for (int s = 0; s < sizeof(a)/sizeof(a[0]); s++)
            printf("\n\nin Main: %d\n", a[s]);
            
        fun_array2(a);
     
    return 0;
    }
    Code:
    userx@slackwhere:~/bin
    $ ./arrayPassFunction
    size 2
    1
    2
    
    
    in Main: 1
    in Main: 2
    in Main: 3
    in Main: 4
    in Main: 5
    in Main: 6
    in Main: 0
    in Main: 0
    in Main: 0
    in Main: 0
    
    d = 1
    d = 2
    d = 3
    d = 4
    d = 5
    d = 6

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    When you pass in an array, it is converted to a pointer to the first element, so when you do the sizeof trick inside a called function, it ends up being wrong.
    sizeof(int*)/sizeof(int) is different from say sizeof(int[10])/sizeof(int).

    The solution is to use a size parameter, and to make sure that you only use the sizeof trick where your array is defined.
    fun_array(a, sizeof(a)/sizeof(a[0]));

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    When you pass in an array, it is converted to a pointer to the first element, so when you do the sizeof trick inside a called function, it ends up being wrong.
    sizeof(int*)/sizeof(int) is different from say sizeof(int[10])/sizeof(int).

    The solution is to use a size parameter, and to make sure that you only use the sizeof trick where your array is defined.
    fun_array(a, sizeof(a)/sizeof(a[0]));
    that makes sense, as I seen something that is how it was set up, but to just put the length in the second pram, but I was trying to get around that. that trick still allows the unknown to be had. because even when I was casting to int it was messing up. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof function confusion
    By Moksha in forum C Programming
    Replies: 2
    Last Post: 01-17-2017, 03:41 AM
  2. sizeof from a second function
    By ShungTzu in forum C Programming
    Replies: 8
    Last Post: 04-21-2016, 10:49 PM
  3. sizeof function and pointers
    By johnmerlino in forum C Programming
    Replies: 2
    Last Post: 04-07-2014, 05:03 AM
  4. sizeof function
    By chrismiceli in forum C Programming
    Replies: 10
    Last Post: 02-28-2004, 08:28 PM
  5. SizeOf function
    By vanilly in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:18 PM

Tags for this Thread