ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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.
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.