Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Integer array size

Reply
Thread Tools

Integer array size

 
 
Jeff
Guest
Posts: n/a
 
      11-12-2003
Hello everybody,

I'm kind of new to C programming, but here's a little question.
Usually, when you have an array of chars, you put a \0 at the end of
it to terminate the string. That way, it is possible with functions
like strlen to get the array size. But is it possible to do the same
thing with an array of integers or floats? And are there any functions
that do so ?

Any help will be appreciated,
Cheers!

- Joseph
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-12-2003
In article <(E-Mail Removed) >, Jeff wrote:
> Hello everybody,
>
> I'm kind of new to C programming, but here's a little question.
> Usually, when you have an array of chars, you put a \0 at the end of
> it to terminate the string. That way, it is possible with functions
> like strlen to get the array size. But is it possible to do the same
> thing with an array of integers or floats? And are there any functions
> that do so ?


Strictly speaking, strlen() doesn't return the length of the
array, only the length of the string stored in the array.


#include <string.h>
#include <stdio.h>

int main(void)
{
char text[] = "hello world";

printf("Length is %d\n", strlen(text));
text[2] = '\0';
printf("Length is %d\n", strlen(text));

return 0;
}


Note that even in the first printf() statement, strlen() lies
about the array length as it doesn't count the last element.


When it comes to arrays of other kinds it is very common to
keep track of the length in another variable. It is extremly
uncommon to not know exactly how long an array is at any
particular point in time. Functions taking arrays as arguments
almost always also takes an integer value describing its length.


--
Andreas Kähäri
 
Reply With Quote
 
 
 
 
sahukar praveen
Guest
Posts: n/a
 
      11-12-2003

"Jeff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hello everybody,
>
> I'm kind of new to C programming, but here's a little question.
> Usually, when you have an array of chars, you put a \0 at the end of
> it to terminate the string. That way, it is possible with functions
> like strlen to get the array size. But is it possible to do the same
> thing with an array of integers or floats? And are there any functions
> that do so ?
>
> Any help will be appreciated,
> Cheers!
>
> - Joseph


character array with trailing '\0' represent C strings. This is not
applicable to integer/float arrays.

Thanks
Praveen


 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      11-12-2003
Greetings.

In article <(E-Mail Removed)>, sahukar praveen wrote:
> "Jeff" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>> I'm kind of new to C programming, but here's a little question.
>> Usually, when you have an array of chars, you put a \0 at the end of
>> it to terminate the string. That way, it is possible with functions
>> like strlen to get the array size. But is it possible to do the same
>> thing with an array of integers or floats? And are there any
>> functions that do so ?

>
> character array with trailing '\0' represent C strings. This is not
> applicable to integer/float arrays.


Sure it is, in the general case. Adding a particular sentinel value to
the end of an array is useful when passing arrays to functions that
don't also take the array size as an argument.

To the OP: There are no standard sentinel values for non-char arrays,
and (partly therefore) no standard functions to return the number of
assigned elements in non-char arrays. However, it's a rather simple
matter to write your own functions to do so; simply have a pointer loop
over the array until it reaches the sentinel value. Either keep a loop
counter, or return the difference between the pointer value at the end
of the loop and the pointer value at the beginning of the loop.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      11-12-2003
Tristan Miller wrote:
> Greetings.
>
> In article <(E-Mail Removed)>, sahukar praveen wrote:


>>character array with trailing '\0' represent C strings. This is not
>>applicable to integer/float arrays.

>
>
> Sure it is, in the general case. Adding a particular sentinel value to
> the end of an array is useful when passing arrays to functions that
> don't also take the array size as an argument.


In the general case you cannot do this. In the specific cases of
knowing that certain values will never be present in the array
or being able to use home grown escape codes then this is possible
to do. But not in the general case.

With strings you know that 0 will never appear in the array so there
is no problem.

--
Thomas.

 
Reply With Quote
 
Tristan Miller
Guest
Posts: n/a
 
      11-12-2003
Greetings.

In article <3fb21810$(E-Mail Removed)>, Thomas Stegen wrote:
> Tristan Miller wrote:
>> Greetings.
>>
>> In article <(E-Mail Removed)>, sahukar praveen wrote:

>
>>>character array with trailing '\0' represent C strings. This is not
>>>applicable to integer/float arrays.

>>
>>
>> Sure it is, in the general case. Adding a particular sentinel value
>> to the end of an array is useful when passing arrays to functions
>> that don't also take the array size as an argument.

>
> In the general case you cannot do this. In the specific cases of
> knowing that certain values will never be present in the array
> or being able to use home grown escape codes then this is possible
> to do. But not in the general case.


Yes. By "in the general case" in my post, I meant that the
null-terminator technique can be generalized to an arbitrary
terminator. That is, it's not possible to add a trailing '\0' to an
array of float, though it is possible to use some specific
floating-point value.

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      11-12-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Jeff) wrote in message news:<(E-Mail Removed). com>...
> Hello everybody,
>
> I'm kind of new to C programming, but here's a little question.
> Usually, when you have an array of chars, you put a \0 at the end of
> it to terminate the string. That way, it is possible with functions
> like strlen to get the array size. But is it possible to do the same
> thing with an array of integers or floats? And are there any functions
> that do so ?
>
> Any help will be appreciated,
> Cheers!
>
> - Joseph


Actually, strlen() returns the length of the string contained in the
array, not the array size itself. For example, if you have

char foo[10] = "bar";

strlen(foo) returns 3, not 10.

If you want to compute the number of elements in the array, you can do
the following:

size_t foo_len = sizeof foo / sizeof foo[0]

This divides the total number of bytes contained in the array by the
bytes contained in a single element, yielding the number of elements.
Note that this method will only work for objects of array type; it
will not work for dynamically allocated arrays, or for arrays passed
as arguments to a function, because in those cases you are dealing
with a pointer type, not an array type.

#include <stdlib.h>

int main (void)
{
int a1[20];
int *a2;
int e1, e2;

a2 = malloc (sizeof *a2 * 20);
e1 = sizeof a1 / sizeof a1[0]; /* 20 * sizeof int / sizeof int
== 20 */
e2 = sizeof a2 / sizeof a2[0]; /* sizeof int* / sizeof int !=
20 */
}
 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      11-12-2003
On Wed, 12 Nov 2003, Jeff wrote:

> Hello everybody,
>
> I'm kind of new to C programming, but here's a little question.
> Usually, when you have an array of chars, you put a \0 at the end of
> it to terminate the string. That way, it is possible with functions
> like strlen to get the array size. But is it possible to do the same
> thing with an array of integers or floats? And are there any functions
> that do so ?


The strlen function will not tell you the size of an array. It will tell
you the size of the string within the array. For example:

char s[10000] = "Bob";
int length = strlen(s);

The size of the array is 10000 but strlen will return the size of the
string "Bob". There is an operator called sizeof that will work for you.
For example:

int array_length = sizeof s;

This will set array_length to 10000.

Since you are new to C language I'd suggest you read and understand the
FAQ (Frequently Asked Questions) for this group. You can find it by
searching for "comp.lang.c FAQ".

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to (E-Mail Removed)
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      11-12-2003
Thank-you very much for all the help. I had indeed thought of putting
a certain value that would not appear normally, and then scan the
array for that value to determine it's length, but I wanted to check
if there was any 'standard'.

Thanks again,

- Joseph
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Size of Integer , long Integer, Long double Suresh V C++ 5 07-05-2010 08:09 AM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
Size of integer array desktop C++ 5 04-24-2007 05:00 PM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
How to convert integer to string array without specify array size? henrytcy@gmail.com C Programming 7 12-08-2005 06:02 AM



Advertisments