Solved

Counting ekement in a array

Posted on 2000-04-20
14
167 Views
Last Modified: 2010-04-02
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
Comment
Question by:David MacDonald
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +5
14 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 2734919
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
 
LVL 5

Expert Comment

by:Wyn
ID: 2734959
For pointers ,using strlen...
0
 
LVL 1

Expert Comment

by:Jmccp
ID: 2735047
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2735122
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
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 10 total points
ID: 2737744
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2737768
guys - all this has been said in my comment above...
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2737951
LOL:-)
0
 
LVL 1

Author Comment

by:David MacDonald
ID: 2741132
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
 
LVL 1

Author Comment

by:David MacDonald
ID: 2741139
I'll try to delete the question, so until I find out how, just ignore it...hehehehe....
0
 

Expert Comment

by:GoofyJoe99
ID: 2746375
if your array is a[] then its length is
int aLength = (sizeof a / sizeof *a);
0
 
LVL 1

Author Comment

by:David MacDonald
ID: 2748371
Say?

Why the division of sizeof a and sizeof *a??
0
 
LVL 3

Expert Comment

by:abusimbel
ID: 2758548
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
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2758589
it is also what LucHoltkamp said

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

0
 
LVL 1

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 104
Why isn't object file created? 6 93
Can Microsoft Visual C++ 2005 Distributable be removed form my Exchange server? 3 27
visual C++ 1 9
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question