473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to find length of array in visual c++ ?

like if we have an array
int [] a = new int [20]
int []b;
b= new int [a.length];
FOR(INT I=0; I<a.length ; i++)
{
b[i]=a[i];
}
this program is not working in visual c++ it keep saying me that there is no such method as (a.length) in visual c++
Feb 5 '12 #1
7 18150
weaknessforcats
9,208 Expert Mod 8TB
There is no a.length because a is the address of an int. Addresses don't have methods.

There is no way to find the length of an array in C or C++. The most you can do is to find the sizeof a variable on the stack. In your case you used the new operator which creates the array on the heap. A sizeof(a) will get you the size of the address of the array.

Read this: http://bytes.com/topic/c/insights/77...rrays-revealed
Feb 5 '12 #2
thanks weaknessforcats :) i am new to c++ but a.length method works in java so i was trying the same with c ++
so there is no way to find length of array in c++ ? because sizeof (a) gives the total size of array it dont give any information about length or we can do like if it is char array then (sizeof(a) /8 )is it good idea?
Feb 6 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
sizeof returns the size of the local variable.

If your array is a local variable (on the stack) sizeof(array) will return hw big the array is in bytes. But if you create your array on the heap using new or malloc then all that's local is the address of the array so sizeof(array) now returns the size of the address of the array and not the array itself. Further, if you pass your array to a function as an argument, then all that's passed is the address of the array. So inside the function sizeof(array) again returns the sizeof the address.

You need to maintain a variable that contains the number of elements and pass it around with the array address. This is the only method that works in all cases.

Further, arrays are consdered very low level. C++ users are supposed to use vectors. The vector container is an array but all of the code you need to write to manage your own personal array is duplicated by the vector code. You should just use a vector and save yurself a lot of time writing code that's alreayd been written and debugged.
Feb 6 '12 #4
I figured this worked pretty well:

in the header define this: const int ARRAYEMPTY = -858993460;

then

int arrayLengt(a[])
{
bool end = false;
int i = 0;
while(!end)
{
if (a[i] == ARRAYEMPTY)
{
end = true;
}
else
{
i++;
}
}
return i;
}
Aug 19 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
You want to be careful of that solution. It prevents -858993460 from being in the array and this is a valid int value.

What is passed to the function is the address of the array. As far as the function knows, this is an int*. That is, a pointer to a single int. Incrementing from that address is an assumption. There is no such thing as a pointer to an array. This is because the name of the array is defined as the address of element 0. There is no way to figure out the number of elements from this address.

If you use sizeof on the array to determine the number of elements in the array, the array must be on the stack. The function's stack in this example does not contain the array so the function can't use sizeof to figure out the number of elements.

The only foolproof method is to use two arguments for the function. One for the address of the array and one for the number of elements. Whoever created the array knew the number of elements so it's important to pass that along.
Aug 19 '14 #6
Finding the length of any non-dynamically allocated array is easy. You simply write:

int Array[ ] = { ... };

unsigned int Array_Length( sizeof( Array ) / sizeof( Array[ 0 ] ) );
Aug 20 '14 #7
weaknessforcats
9,208 Expert Mod 8TB
Just keep in mind that only works on the stack in the function where the array is defined.

Everywhere else you will need the number of elements since all you will have is the address of the array. This is called decay of array.
Aug 20 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
8
by: Fernando Barsoba | last post by:
Hi, I decided to start a new topic about my memory allocation question. One of the answers was that variable length arrays are possible using C99. I found this topic that also mentions it. ...
1
by: moonriver | last post by:
I intend to generate a variable-length array, similar to link lists in plain C. For example, I define the following array int a Initially I assign 5 elements to the array as a = new int...
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
6
by: finerrecliner | last post by:
hey everyone i'm trying to make a function that will return the length and width of a dynamically allocated 2D array. here's what i came up with to find width: int findWidth(int** matrix)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.