LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2007, 11:31 AM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
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!
 
Old 08-14-2007, 12:46 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
My guess: in the function definition, 'a' is a pointer hence has the length of a pointer. Think it always returns 1.
 
Old 08-14-2007, 12:46 PM   #3
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 08-14-2007, 12:56 PM   #4
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
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++;
 
Old 08-14-2007, 02:00 PM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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...
 
Old 08-14-2007, 02:08 PM   #6
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
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...?
 
Old 08-14-2007, 02:44 PM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by rubadub View Post
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.
 
Old 08-14-2007, 02:55 PM   #8
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
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...
 
Old 08-14-2007, 03:47 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 08-14-2007, 04:06 PM   #10
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
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
 
Old 08-14-2007, 04:35 PM   #11
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by rubadub View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does it return zero when counting? PenguinMan Programming 5 12-21-2017 12:12 AM
Counting characters in C++ ckoniecny Programming 6 09-08-2006 12:58 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
And counting... MasterC General 18 09-24-2003 05:07 AM
counting dummyagain Programming 3 09-24-2003 02:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration