Size of an array ,C++

Checking an array's size

Page 1 of 1

8 Replies - 47197 Views - Last Post: 30 June 2008 - 12:55 PM Rate Topic: -----

#1 babasmith  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 02-April 06

Size of an array ,C++

Posted 15 April 2006 - 12:33 PM

Hello;

How do I check for the length/size of an array (C++)?

Example:
x is a pointer to an array of booleans and I want to know its length/size (like when writing in Java 'x.length').

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Size of an array ,C++

#2 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: Size of an array ,C++

Posted 15 April 2006 - 02:05 PM

I dont think thats possible. You'd just have to pass the size of the array to the function as a parameter.
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Size of an array ,C++

Posted 15 April 2006 - 02:27 PM

You'll likely have the arrya length stored in avariable, so you can pass that to a function. If you want to use something similar to the .length property in java, declare that array as an array of vectors.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Size of an array ,C++

Posted 15 April 2006 - 05:42 PM

If you are looking to travers an array, based on a relative size, you do not need the size, use a while loop instead of a for loop within a try/catch block, it is not neat, nor is it proper, but it will get the job done with any size of array.
The easiest thing to do would be to implement it as a linked list, thus no size limits and you can use the while loop as a while(element){} then continually update element to by element->nextElement kind of deal :)
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Size of an array ,C++

Posted 15 April 2006 - 06:04 PM

I would suggest that implementing an array of booleans as a linked list might be overkill...the structure itself would more than double the amount of memory required.

A while loop is a good approach, but I would still tend to think that the size of the array is available...either from intial declaration of the array or from reallocation if done dynamically. In either case, the length is available,and can be used in a for loop.

If an encompassing structure were required, a vector could be used...it is already available in the language, and has the requisite methods already defined.
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Size of an array ,C++

Posted 16 April 2006 - 01:14 AM

It's a real crude way but do it just in case you have no other option.
Increment the pointer and ensure that the values are either 0 or 1.Maintain another counter variable and keep incrementing it as long as the pointer points to a ZERO or a ONE.
When any other character is found, terminate the loop and the length will be stored in the counter variable.
Was This Post Helpful? 0
  • +
  • -

#7 fadibasma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 08

Re: Size of an array ,C++

Posted 30 June 2008 - 12:38 PM

View Postbabasmith, on 15 Apr, 2006 - 12:33 PM, said:

Hello;

How do I check for the length/size of an array (C++)?

Example:
x is a pointer to an array of booleans and I want to know its length/size (like when writing in Java 'x.length').

Thanks.



try sizeof(array name)

fadi
Was This Post Helpful? 0
  • +
  • -

#8 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Size of an array ,C++

Posted 30 June 2008 - 12:43 PM

This is a two year only topic... Why did you did it up?
Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Size of an array ,C++

Posted 30 June 2008 - 12:55 PM

First post, too -_-
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1