Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Counting ekement in a array

Could someone tell me if there is a function that return the number of element in an array of char under C/C++??

I could just put a zero at the end of each of my arrays.... but this method would suck a lot......

Thanks
0
David MacDonald
Asked:
David MacDonald
  • 4
  • 2
  • 2
  • +5
1 Solution
 
captainkirkCommented:
If you declared the array like this:

   char a[MAX_ITEMS];

then the size of the array is MAX_ITEMS.

If you declared the array like this:

   char a[] = "some string";

then the size of the array is

   strlen(a);

Let's say you have an 2D array like this:

   char szDataItems[MAX_ITEMS][MAX_LEN];

then the size of the array (#elements) is:
   sizeof(szDataItems) / sizeof szDataItems[0];
0
 
WynCommented:
For pointers ,using strlen...
0
 
JmccpCommented:
Sure there is.  It is strlen().

Example:

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

main()
{

char buf[80];
size_t length;  

puts("Enter a character string: ");
gets(buf);

length = strlen(buf);

if (length != 0)
  printf("\nThe string is %u characters long.", length);
else
  printf("\nThe string is 0 characters long.");
}

Jim
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ShaunWildeCommented:
NOTE 1: strlen will give you the length of the string not the length of the array that contains the string.

e.g.

char buf[80]; // array length is 80

strcpy(buf,"hello world");

int nLen=strlen(buf); // will return 11 not 80

NOTE 2:
> I could just put a zero at the end of each of my arrays.... but this method would suck a lot

that is how strlen works it :) it counts the number of elements until it reaches '\0' (==0) - you should always make room for this terminator in your string array.

e.g.

to contain the string "hello world" requires an array of 12 not 11.

char buf[11];
strcpy(buf,"hello world"); // probable crash

char buf[12];
strcpy(buf,"hello world"); // okay


0
 
LucHoltkampCommented:
If you have an array like:

MyType array[const_expression];

you can find the number of elements like:

unsigned noe = sizeof(array) / sizeof(array[0]);

Luc
0
 
captainkirkCommented:
guys - all this has been said in my comment above...
0
 
WynCommented:
LOL:-)
0
 
David MacDonaldAuthor Commented:
Well, I worked it out with the "coyote method" (old inside joke among us) doing it with a loop....

The structure of the array changed a bit since I post the question... it has changed from a char *ptr to a

struct MyStruct **ptr[10][]....

Kind a complicated and there are other arrays that joins to that.... so thanks a lot for your help to you all.....
0
 
David MacDonaldAuthor Commented:
I'll try to delete the question, so until I find out how, just ignore it...hehehehe....
0
 
GoofyJoe99Commented:
if your array is a[] then its length is
int aLength = (sizeof a / sizeof *a);
0
 
David MacDonaldAuthor Commented:
Say?

Why the division of sizeof a and sizeof *a??
0
 
abusimbelCommented:
GoofyJoe99 it's just on right.

sizeof(array) gives you the memory size in bytes of the array.

sizeof(array[0]) gives you the memory size in bytes of one element of the array.

Goofy wins. ;-)
0
 
ShaunWildeCommented:
it is also what LucHoltkamp said

> unsigned noe = sizeof(array) / sizeof(array[0]);

0
 
David MacDonaldAuthor Commented:
Since LucHoltkamp is the first that gave the division method, i'll accept his answer. But thanks to you all....
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now