Integer array size

J

Jeff

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
 
A

Andreas Kahari

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.
 
S

sahukar praveen

Jeff said:
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
 
T

Tristan Miller

Greetings.

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
 
T

Thomas Stegen

Tristan said:
Greetings.

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.
 
T

Tristan Miller

Greetings.

Tristan said:
Greetings.



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.
 
J

John Bode

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 */
}
 
D

Darrell Grainger

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".
 
J

Jeff

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top