Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C sizeof a passed array [duplicate]

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;
}
like image 581
Untamed Avatar asked Mar 30 '11 22:03

Untamed


People also ask

How to find the size of an array in C++ program?

One solution is to write our own sizeof operator (See this for details) // C++ program to find size of an array by writing our. // sizeof. #include <bits/stdc++.h>. using namespace std; // User defined sizeof macro. # define my_sizeof(type) ((char *)(&type+1)-(char*)(&type)) int main()

Does sizeof () function warn you of array size in C++?

So it does not warn you. However, the result of sizeof will be the same either way, and it will not tell you the size of the array. Not the answer you're looking for?

Why can't I declare a parameter as an array size?

When you declare the parameter as int *x, which is the same as the array parameter after it is adjusted, the compiler knows you are thinking of the parameter as a pointer, so it expects you know that sizeof x will produce the size of the pointer, not the array. So it does not warn you.

Why sizeof (p) can't be passed as a parameter to a function?

Function parameters never actually have array type. So sizeof(p) is sizeof(double*). And yes, you'll have to pass the size as a parameter. The problem is that your function doesn't receive an array value; it receives a pointer value.


2 Answers

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.
like image 87
DigitalRoss Avatar answered Oct 18 '22 17:10

DigitalRoss


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.

like image 36
aschepler Avatar answered Oct 18 '22 17:10

aschepler