1

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

I know this to find the size of array = sizeof(arr)/sizeof(arr[0])

But I have to implement the following (It's just a demo):

demo.h

#ifndef __DEMO_H
#define __DEMO_H
void heap_sort(int *);
#endif

demo.c

void heap_sort(int *ptrA)
{
//implementing heap sort 
But here it requires length of array 
}

main.c

#include "demo.h"
int main(void)
{
 int A[10];
 heap_sort(A)
 return 0;
}

FYI .. It's just a demo.. but here I have to implement it in some other scenarios in which there is restriction that "DON'T CHANGE ANYTHING IN HEADER FILE" which means i can't change the function signature . Then how to get the array length in demo.c For char it's easy to get by help of strlen() Isn't there anything similar to get the length of int,float double types

Community
  • 1
  • 1
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • The real problem is: you add an obscure restriction "nothing may be changed" and then expect a clean, proper solution to it. The proper solution would be to change the function to include the size, either through a struct or through an extra size parameter. If this isn't possible, then an ugly hack will have to suffice as the solution. – Lundin Nov 12 '12 at 10:34

4 Answers4

4

The only alternatives I see are:

  • use a special value as terminator (as strlen does).
  • use the Pascal trick, and place array length in the first element.
  • store the array size in a global external variable.
  • use a separate function.

E.g.:

int arraySize(int newSize)
{
    static int arraySize = 0;
    int oldSize;
    oldSize = arraySize;
    if (newSize)
        arraySize = newSize;
    return oldSize;
}

in main.c:

    arraySize(10);

in demo.c:

    arraylen = arraySize(0);
LSerni
  • 55,617
  • 10
  • 65
  • 107
  • I am also thinking about it to make size of array `N` as global and extern and access that to other file... but it's very serious problem also that making a global is vulnerable and it's better to avoid it – Omkant Nov 12 '12 at 09:32
  • You have a more serious problem in that you can't change a signature of a function which no longer satisfies its requirements. All other problems follow :-). You can increase security a bit by wrapping the global into a function, as shown. – LSerni Nov 12 '12 at 09:36
  • can't I do with the struct because creation of function will be an overhead. make one varibale n inside struct and in main i will create one object of struct and set the value of n and then in demo.c again by creating a struct object get the value of n using object.n but the problem is where to keep the struct defintion .. I dont have to change header file – Omkant Nov 12 '12 at 09:45
  • There is no struct definition: it is all kept inside the function. You just have to call the function. Seeing as it's an int returning int, you can even ignore prototype warnings. If you can't even add a function, well... **given enough requirements, everything can be made impossible**, and I'd say they have succeeded :-) – LSerni Nov 12 '12 at 11:20
2

if you can't change the function signature, then maybe you could pass the size of the array in the first element.

A[0] = 10;
heap_sort(A);

Or mark the end of the array with some special value, but I don't like this one because you'd have to iterate the whole array to find the length and you need to make sure this value is not used in the array:

A[LENGTH-1] = END//some value;

void array_length(A) {
    while (*A++ != END) {
          length++;
    }
}

This is just a solution for the restrictions you imposed, what I would normally do, is either pass the size of the array as a second argument, or use a struct for the array:

struct array_t {
    int *data; //allocate this
    int size;
};

Note: other horrible solutions include global variables.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • But for the others who are going to use the same function they will have to take extra care about it ..beacuse this functions is going to be used in other module of project and other programmers should take care about it .. don't you think ? – Omkant Nov 12 '12 at 09:34
  • @Omkant yes, I agree, it's just a workaround, what I would do normally is just pass the array size as a second argument. – iabdalkader Nov 12 '12 at 09:35
  • can't I do with the struct because creation of function will be an overhead. make one varibale `n` inside struct and in main i will create one object of struct and set the value of `n` and then in `demo.c` again by creating a struct object get the value of `n` using `object.n` but the problem is where to keep the struct defintion .. I dont have to change header file – Omkant Nov 12 '12 at 09:44
  • @Omkant yes you would have to change the header to use a `struct` but that's how I would it's normally done, not what you should do in your situation, one of the first two solutions is the only way. – iabdalkader Nov 12 '12 at 09:50
  • thanks i will discuss and gonna change the stucture of this – Omkant Nov 12 '12 at 09:56
1

Thinking of strlen() is going into the right direction.

Strings are character arrays with a '\0' as array termination, as last element.

You could take the same approach for any other type of array.

Just define one value as the value which indicates the last element in an array. Searching for this value then helps you to find the size of the array.


Update:

I like mux's idea of using the first element in an array.

Anyhow, using it to store the numbers of element in there might lead to problems in case the number of elements in the array is larger as what can be store in an array's element (a char array, for example, whould then be limited to 255 elements).

My approach on the other hand has the draw back that the value used as terminator to the array is not usable as real value in the arra itself.

The combining the former and the latter approaches, I propose to use the first element of the array to store the value which is used as terminator of the array.

alk
  • 69,737
  • 10
  • 105
  • 255
  • can't I do with the struct because creation of function will be an overhead. make one varibale n inside struct and in main i will create one object of struct and set the value of n and then in demo.c again by creating a struct object get the value of n using object.n but the problem is where to keep the struct defintion .. I dont have to change header file – Omkant Nov 12 '12 at 09:45
  • As far as I understood you, you are limited to passing an `int *`, as described by gthe API. So you can not pass a `struct foo *` without dirty tricks. @Omkant – alk Nov 12 '12 at 09:57
0

The constraint seems a bit odd, but whatever.

Why not use a global variable to store the size.

pike
  • 9
  • 1
  • To make size of array N as global and extern and access that to other file... but it's very serious problem also that making a global is vulnerable and it's better to avoid it – Omkant Nov 12 '12 at 09:41
  • Man, that's a dirty trick also ... :-/ – alk Nov 12 '12 at 09:59