473,473 Members | 2,131 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Integer array size

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
Nov 13 '05 #1
8 17519

"Jeff" <ag*******@netcourrier.com> wrote in message
news:6c**************************@posting.google.c 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
Nov 13 '05 #2
In article <6c**************************@posting.google.com >, 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
Nov 13 '05 #3
Greetings.

In article <3f******@usenet01.boi.hp.com>, sahukar praveen wrote:
"Jeff" <ag*******@netcourrier.com> wrote in message
news:6c**************************@posting.google.c 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
Nov 13 '05 #4
Tristan Miller wrote:
Greetings.

In article <3f******@usenet01.boi.hp.com>, 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.

Nov 13 '05 #5
Greetings.

In article <3f********@nntphost.cis.strath.ac.uk>, Thomas Stegen wrote:
Tristan Miller wrote:
Greetings.

In article <3f******@usenet01.boi.hp.com>, 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
Nov 13 '05 #6
ag*******@netcourrier.com (Jeff) wrote in message news:<6c**************************@posting.google. 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 */
}
Nov 13 '05 #7
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 vi************@whitehouse.gov
Nov 13 '05 #8
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
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: shailashri_sk | last post by:
Hi, int *p; p++; here p now increments itself with the size of integer. similarly, I wanted to know, how to declare an pointer to an array ( say array of integers) where in it we do a p++ it...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
7
by: henrytcy | last post by:
Hi, How can I convert integer, for example 12113, to char array but without specify an array size in C programming? Thanks!
16
by: kujahleague | last post by:
Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character,...
6
by: Kinbote | last post by:
Hi, I'm trying to make a function that opens a file, reads it in line by line, puts each line into an malloc'd array, and returns the array. I suspect I'm going about it in an atypical fashion, as...
5
by: desktop | last post by:
I have this code: void test(int* array) { int pp = sizeof(array); cout << pp << endl; }
17
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1,...
130
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.