LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Array counting in C (https://www.linuxquestions.org/questions/programming-9/array-counting-in-c-577028/)

rubadub 08-14-2007 11:31 AM

Array counting in C
 
Hi, another pointless exercise (because we should have always counted the size of the array), but...

Here's a little demo:
Code:

#include <stdio.h>

int alen(int a[])
{       
        return (int)sizeof(a) / sizeof(int);
}

int main()
{
        int size = 6;
        int a[size];
               
        a[0] = 3;        a[1] = 2;        a[2] = 1;
        a[3] = 8;        a[4] = 7;        a[5] = 6;
       
        int n1 = (int)sizeof(a) / sizeof(int);
        printf("n1: %d \n", n1);
       
        int n2 = alen(a);
        printf("n2: %d \n", n2);
       
        return 0;
}

What's wrong with the way the array is passed?

cheers!

Wim Sturkenboom 08-14-2007 12:46 PM

My guess: in the function definition, 'a' is a pointer hence has the length of a pointer. Think it always returns 1.

wjevans_7d1@yahoo.co 08-14-2007 12:46 PM

Function alen()'s parameter is not actually an array; it's a pointer. This is not a bug; it's a feature. An array does not have stored, at runtime, its size or number of elements, so there's no way (as your code is written) for the function to know the size of the array.

To be consistent, C uses a pointer even if the size of the array would be known at compile time. This program, for example, outputs "4" as its result, which is the size of a pointer on my machine.
Code:

#include <stdio.h>

int
alpha(int param[3])
{
  return sizeof(param);
}

int
main(void)
{
  int local_array[3];

  local_array[0]=30;
  local_array[1]=31;
  local_array[2]=32;

  printf("%d\n",alpha(local_array));

  return 0;
}

Hope this helps.

rubadub 08-14-2007 12:56 PM

So if I pass a pointer (to an array) how can I tell when it's 'NULL' if I iterate through it?
Code:

while(*a++ != NULL)
  n++;


rstewart 08-14-2007 02:00 PM

Hi,

Quote:

So if I pass a pointer (to an array) how can I tell when it's 'NULL' if I iterate through it?
Code:

while(*a++ != NULL)
  n++;


You preinitialize it to a known state before using it...

rubadub 08-14-2007 02:08 PM

Even if I pre-init it, then assign it to point to the start of the array, that would overwrite out the previous state.

How do you tell when the array has finished...?

95se 08-14-2007 02:44 PM

Quote:

Originally Posted by rubadub (Post 2858830)
How do you tell when the array has finished...?

This is often done by passing the length of the array in as an argument, along with the array itself. Sometimes a sentinal value is used instead. For instance, if there is an array of positive integers, a negative integer could denote the end of the array. If it is an array of pointers, NULL could be the sentinal value instead.

rubadub 08-14-2007 02:55 PM

So, there's many ways of doing it, but no way of doing it without any pre-arrangement?
No way of checking for a valid pointer? A [-1] goes backwards and the pointers can always increment, so can't check there either.

Got to be in the know it seems. Not very dynamic, even though we should always know the size of things, or have a prearranged EOF/NULL/id, etc...

Cheers anyway...

paulsm4 08-14-2007 03:47 PM

Quote:

So, there's many ways of doing it, but no way of doing it without any pre-arrangement?
Absolutely correct :-)
Quote:

Not very dynamic...
Quite the contrary. It's actually *more* flexible than having somebody cram one way or another down your throat.

Because there are always tradeoffs ... always advantages and disadvantages ...

... and *you* should be able to choose the method that works *best* for your particular application.

Suppose, for example, the language had a length byte before the start of the array. Voila - you always know your array size. But you could only hold up to 255 elements (8 bits).

OK - suppose the language stored the length in an integer. Now your array could hold up to 4 billion elements ... but you'd be "wasting" a lot of extra space.

And what if you needed a "stretchy" array, that could grow or shrink?

The list goes on and on - I hope you get the idea.

IMHO .. PSM

rubadub 08-14-2007 04:06 PM

Yeah...
I'm just investigating the 'limits' of c, especially when it comes to arrays and pointers.

Even though at some point I will always 'know' the length of something, I just like the ability to find something out... or even prove it... y'know

95se 08-14-2007 04:35 PM

Quote:

Originally Posted by rubadub (Post 2858956)
Yeah...
I'm just investigating the 'limits' of c, especially when it comes to arrays and pointers.

Even though at some point I will always 'know' the length of something, I just like the ability to find something out... or even prove it... y'know

I wouldn't call these limitations in anyway. C simply abstracts less than other languages. You can build the abstractions you're used to yourself, or get libraries to handle that for you. C biggest disadvantage is really its poor namespacing. Java really hosed the competition in that category.


All times are GMT -5. The time now is 01:41 AM.