Thread: sizeof(array) returns differnt values for same array?

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Unhappy sizeof(array) returns differnt values for same array?

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void printArray(int array[]) {
    	cout<<"size of array: "<<sizeof(array)<<endl;
    	for (int i=0;i<sizeof(array);i++) 
    		cout<<array[i]<<" ";
    	cout<<endl;
    }
    
    
    int main () {
    	int array[10];
    	srand ( time(NULL) );
    
    	for (int i=0;i<sizeof(array);i++) 
    		array[i] = rand() % 100;
    	cout<<"size of array: "<<sizeof(array)<<endl;
    	printArray(array);
    	return 0;
    }

    prints:
    size of array: 40
    size of array: 4
    7 89 51 6 (or some other random numbers, key thing is that there are only 4 displayed)

    why is the array differnt sizes for the function and main body?
    also what is a mathematical way of using sizeof() to get the length of the array (I want to get 10 from it).

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>why is the array differnt sizes for the function and main body?
    That's because arrays are passed to functions as pointers, in main sizeof returns the size of the array, but in the other function sizeof returns the size of a pointer to int, in your case 4 bytes :-)

    >>also what is a mathematical way of using sizeof() to get the length of the array (I want to get 10 from it).
    Code:
    int len = sizeof(array) / sizeof(array[0]);
    *Cela*

  3. #3
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    that makes sense.

    would i have to use:
    Code:
    sizeof(&array) / sizeof(&array[0]);
    in the function to get 10 as the length?

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Your code is a c++ code, you're in a C board and not C++.
    I'll answer you about the way to get the sizeof the array like you told, you want to receive 10.

    You have an array of 10 elements and they're as Integer (4 bits), so just do:
    Code:
    (sizeof(array)/sizeof(int))
    Of course if it's an array of float change to sizeof(float) and so on..

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>in the function to get 10 as the length?
    You can't use sizeof on an array that was passed as a function parameter because it's just a pointer, sizeof won't work like you want. You have to pass the size with it :-)
    Code:
    #include <stdio.h>
    
    void print_array(int array[], int size)
    {
      int i;
    
      for (i = 0; i < size; i++)
      {
        printf("%d\n", array[i]);
      }
    }
    
    int main(void)
    {
      int a[] = {1,2,3,4,5};
    
      print_array(a, sizeof(a) / sizeof(a[0]));
    
      return 0;
    }
    *Cela*

  6. #6
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    pardon my mistake, thought i was in C++

    must have been a wrong click

  7. #7
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    thnx cela

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. 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
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM