By Untamed


2011-03-30 22:33:04 8 Comments

Possible Duplicate:
How to find the sizeof( a pointer pointing to an array )

I understand that the sizeof operator is evaluated and replaced with a constant at compile time. Given that, how can a function, being passed different arrays at different points in a program, have it's size computed? I can pass it as a parameter to the function, but I'd rather not have to add another parameter if I don't absolutely have to.

Here's an example to illustrate what I'm asking:

#include <stdio.h>
#include <stdlib.h>

#define SIZEOF(a) ( sizeof a / sizeof a[0] )


void printarray( double x[], int );

int main()
{
        double array1[ 100 ];


        printf( "The size of array1 = %ld.\n", SIZEOF( array1 ));
        printf( "The size of array1 = %ld.\n", sizeof array1 );
        printf( "The size of array1[0] = %ld.\n\n", sizeof array1[0] );

        printarray( array1, SIZEOF( array1 ) );

        return EXIT_SUCCESS;
}


void printarray( double p[], int s )
{
        int i;


        // THIS IS WHAT DOESN"T WORK, SO AS A CONSEQUENCE, I PASS THE 
        // SIZE IN AS A SECOND PARAMETER, WHICH I'D RATHER NOT DO.
        printf( "The size of p calculated = %ld.\n", SIZEOF( p ));
        printf( "The size of p = %ld.\n", sizeof p );
        printf( "The size of p[0] = %ld.\n", sizeof p[0] );

        for( i = 0; i < s; i++ )
                printf( "Eelement %d = %lf.\n", i, p[i] );

        return;
}

7 comments

@DigitalRoss 2011-03-30 22:36:45

There is no magic solution. C is not a reflective language. Objects don't automatically know what they are.

But you have many choices:

  1. Obviously, add a parameter
  2. Wrap the call in a macro and automatically add a parameter
  3. Use a more complex object. Define a structure which contains the dynamic array and also the size of the array. Then, pass the address of the structure.

@Andrea993 2018-05-10 21:49:48

You can pass a pointer to the array printarray( double (*p)[100]), calling printarray(&array1) so you can refer to your array by *p and use sizeof *p

@John Bode 2011-03-30 23:10:13

The problem is that your function doesn't receive an array value; it receives a pointer value.

Except when it is the operand of the sizeof or unary & operators, or it is a string literal being used to initialize another array in a declaration, an expression of type "array of T" will be converted to type "pointer to T" and its value will be the address of the first element of the array.

Thus, when you call printarray, the type of array1 is implicitly converted from "100-element array of double" to "pointer to double." Thus, the type of the parameter p is double *, not double [100].

In the context of a function parameter declaration, T a[] is identical to T *a.

This is why you have to pass the array size separately;

@bobDevil 2011-03-30 22:39:14

You answered your own question. It's computed at compile-time, so how can 'sizeof p' possibly have more than one value during runtime?

Passing the length as a parameter is a fine solution, otherwise you can make sure your arrays always end with some special value (like strings and the null byte).

@aschepler 2011-03-30 22:38:22

Function parameters never actually have array type. When the compiler sees

void printarray( double p[], int s )

or even

void printarray( double p[100], int s )

it converts either one to

void printarray( double* p, int s )

So sizeof(p) is sizeof(double*). And yes, you'll have to pass the size as a parameter.

@Eric 2011-03-30 22:37:05

If your array is null terminated then you can iterate through the array and find the last value. Many string operators in C work that way

Otherwise you don't have much choice.

sizeof will return the size of p, which is a pointer and will equals to your int size

@Scott Duckworth 2011-03-30 22:36:46

You must either

  1. Pass the size of the array as a parameter to the function
  2. Ensure that the array ends with a known value, and stop when you reach that value.

@Robert S. Barnes 2011-03-30 22:36:17

You can use a sentinel value which is the way strings are dealt with. The drawback is that you have to iterate over the entire array to find it's size.

For example, when you have a string such as:

char s1[] = "foobar";

What you actually have is an array of length 7, with the seventh spot being a null terminating char '\0' which serves to indicate the end of the array / string.

Another way you could do it is to create a struct which consists of a size followed by the array.

Related Questions

Sponsored Content

49 Answered Questions

93 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 6253848 View
  • 7809 Score
  • 93 Answer
  • Tags:   javascript arrays

38 Answered Questions

[SOLVED] For-each over an array in JavaScript?

43 Answered Questions

[SOLVED] Loop through an array in JavaScript

48 Answered Questions

[SOLVED] How to check if an object is an array?

44 Answered Questions

[SOLVED] Sort array of objects by string property value

17 Answered Questions

[SOLVED] How to insert an item into an array at a specific index (JavaScript)?

34 Answered Questions

[SOLVED] Create ArrayList from array

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

30 Answered Questions

[SOLVED] How to append something to an array?

Sponsored Content