newbie: array-length

M

merman

Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};

Thanks for help.

o-o

Thomas
 
T

Thomas Matthews

merman said:
Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};

Thanks for help.

o-o

Thomas

For an array, you can get the number of elements
by this formula:
num_elems = sizeof(array) / sizeof(array[0]);

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Martin Ambuhl

merman said:
Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};


/* This is a FAQ. Check the FAQs _before_ posting */
#include <stdio.h>

int main(void)
{
char *test[] = { "a", "b", "c", NULL };

printf("WARNING: The numbers produced by this program\n"
"are implementation-dependent.\n"
"The test array has a total size of %lu,\n"
" is composed of elements of size %lu.\n"
"So there are %lu elements.\n",
(unsigned long) sizeof test,
(unsigned long) sizeof *test,
(unsigned long) (sizeof test / sizeof *test));
return 0;
}

WARNING: The numbers produced by this program
are implementation-dependent.
The test array has a total size of 16,
is composed of elements of size 4.
So there are 4 elements.
 
M

Method Man

merman said:
Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};

Thanks for help.

As others have said, the formula '(sizeof array) / (sizeof *array)' will get
you the length (of type size_t) of an array.

But note: If you were to pass an array into a function, the array will decay
to a pointer to its first element, making it impossible [1] to calculate its
length within the function scope. In such cases, you should pass in the
array size as a parameter.

[1] The length of a null-terminating char* array may be calculated manually
using a loop.
 
D

damian birchler

merman said:
Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};

Thanks for help.

o-o

Thomas
From your example I assume that you are talking about an array of
chars (a zero terminated string). You could also write it like that:

char test[] = "abc";

Note that when working with strings you wouldn't compare to NULL, but
to '\0'. They are exactly the same, but it is common practice to
compare to NULL with pointers and to compare to '\0' with strings.
The length of any such array can be determined with the strlen
function:


#include <string.h> /* for strlen */
#include <stddef.h> /* for size_t */

size_t strlen(const char *s);
 
K

Keith Thompson

merman said:
how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};
[...]
From your example I assume that you are talking about an array of
chars (a zero terminated string). You could also write it like that:

char test[] = "abc";

No, test is an array of pointers to strings, not an array of chars.
The trailing NULL is a null pointer, not a '\0' character.
Your 'char test[] = "abc";' is a a completely different thing.
 
B

Barry Schwarz

merman said:
Hi,

how can I get the length of an array:

char *test[] = {"a", "b", "c", NULL};

Thanks for help.

o-o

Thomas
From your example I assume that you are talking about an array of
chars (a zero terminated string). You could also write it like that:

char test[] = "abc";

Note that when working with strings you wouldn't compare to NULL, but
to '\0'. They are exactly the same, but it is common practice to

NULL need not be defined in a manner which can be compared with '\0'.
Attempting to compare a char to NULL on many systems will produce a
compiler diagnostic.
compare to NULL with pointers and to compare to '\0' with strings.
The length of any such array can be determined with the strlen
function:

strlen will only show you how much of the array is consumed by the
string. It will not tell you how large the array is.
#include <string.h> /* for strlen */
#include <stddef.h> /* for size_t */

Unnecessary. Including string.h will insure that size_t is defined.
size_t strlen(const char *s);



<<Remove the del for email>>
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top