473,327 Members | 1,936 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,327 software developers and data experts.

Find length of an array in C

31
Can we use array.length when we dont know lengt of array.I use C language
Jul 2 '07 #1
17 3993
Meetee
931 Expert Mod 512MB
Can we use array.length when we dont know lengt of array.I use C language
You can apply something like following to find the length of an array
Expand|Select|Wrap|Line Numbers
  1. if array has three values like this:
  2. int var[10]={1,2,3};
  3. for(int i=0;i>=0;i++)
  4. {
  5. if(var[i]!=NULL)
  6. {
  7.   len++; this will be the length of array
  8. }
  9. else break;
  10. }
  11.  
Regards
Jul 2 '07 #2
r035198x
13,262 8TB
Erm, you can't find the length of an array unless the last element of the array is some sentinel value.
Jul 2 '07 #3
mihiri
31
You can apply something like following to find the length of an array
Expand|Select|Wrap|Line Numbers
  1. if array has three values like this:
  2. int var[10]={1,2,3};
  3. for(int i=0;i>=0;i++)
  4. {
  5. if(var[i]!=NULL)
  6. {
  7.   len++; this will be the length of array
  8. }
  9. else break;
  10. }
  11.  
Regards
thanks your information.But i wanna use a array in for loop.But I dont know the length of array.So can I use that kind of code for that.
for(int i;i<array.length;i++)
{
...
}
thanks
mihiri
Jul 2 '07 #4
Meetee
931 Expert Mod 512MB
Erm, you can't find the length of an array unless the last element of the array is some sentinel value.
Yes, number of elements can be found but not length "unless the last element of the array is some sentinel value". :))

To use array length you can use some dyanamic array and give last value to @ or some other such value and count until that value arrives.
Jul 2 '07 #5
mihiri
31
Yes, number of elements can be found but not length "unless the last element of the array is some sentinel value". :))

To use array length you can use some dyanamic array and give last value to @ or some other such value and count until that value arrives.
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;

int array [k];

for(k=0;k<array.length;k++)
..................
can I make a for loop like this
Jul 2 '07 #6
r035198x
13,262 8TB
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;

int array [k];

for(k=0;k<array.length;k++)
..................
can I make a for loop like this
That wouldn't be correct. Use another variable for the index like i and replace array.length with k
Jul 2 '07 #7
Meetee
931 Expert Mod 512MB
thanks your reply .I ve another problem.Can you pls help me.can I increment array using same parameter in array.I mean,
#include <stdio.h>
int main()
{
int k;

int array [k];

for(k=0;k<array.length;k++)
..................
can I make a for loop like this
First of all, array.length is not possible. You can increment array using dynamic array like this

int *array = new int[];

and then delete it after use.

delete [] array;

What you have wriiten is fixed size array :))
Jul 2 '07 #8
mihiri
31
First of all, array.length is not possible. You can increment array using dynamic array like this

int *array = new int[];

and then delete it after use.

delete [] array;

What you have wriiten is fixed size array :))
this is not a fixed array.So how can I go to end of the array and I use C language
Jul 2 '07 #9
Meetee
931 Expert Mod 512MB
this is not a fixed array.So how can I go to end of the array and I use C language
For that r035198x has given the solution. If you do not understand here is the code.
(referred frm ur example)

int k,

int array[k];

for(int i=0; i<k;i++)
{
{Do whatever}
}

Cheers!! :))
Jul 2 '07 #10
mihiri
31
For that r035198x has given the solution. If you do not understand here is the code.
(referred frm ur example)

int k,

int array[k];

for(int i=0; i<k;i++)
{
{Do whatever}
Cheers!! :))
}
thank you very much ur help .but I wanna to increment array.ie acording to ur code I wanna increment k.that mean I wanna travel the array.and also i dont know array size.thats my prob(I dont wanna increment other variable as i)
Jul 2 '07 #11
Meetee
931 Expert Mod 512MB
}
thank you very much ur help .but I wanna to increment array.ie acording to ur code I wanna increment k.that mean I wanna travel the array.and also i dont know array size.thats my prob(I dont wanna increment other variable as i)
You can not increament fixed size array. One you have given the value 10 for that variable than you cannot programmatically increment it to 15. Only dynamic array can do that. They will give u facility to add whatever number of values u need.

If possible give ur problem in detail, what your program want to do.

Regards
Jul 2 '07 #12
mihiri
31
You can not increament fixed size array. One you have given the value 10 for that variable than you cannot programmatically increment it to 15. Only dynamic array can do that. They will give u facility to add whatever number of values u need.

If possible give ur problem in detail, what your program want to do.

Regards
there is a fnction and its return values i stored to a array.So this array is not fixed.I have ti travel this array element by element.So I use for loop for travel the array.So i define array like int k;int array[K];and use for(k;k<end of array;k++)
*This is not a fixed array
*I wanna to travrel the array until last element
*I dont kno array size
this is my problem
Jul 2 '07 #13
Meetee
931 Expert Mod 512MB
there is a fnction and its return values i stored to a array.So this array is not fixed.I have ti travel this array element by element.So I use for loop for travel the array.So i define array like int k;int array[K];and use for(k;k<end of array;k++)
*This is not a fixed array
*I wanna to travrel the array until last element
*I dont kno array size
this is my problem
OK.

So you can define a dynamic array like
Expand|Select|Wrap|Line Numbers
  1. int *array = new int[];
  2. int count = 0;
  3.  
  4. for (int i = 0; i <whatever time you want to insert the new element; i++)
  5. {
  6. cin>>value; //scanf incase of C
  7. array[count] = value; //This will add to array after last element
  8. count++;
  9. }
  10.  
Finally u will get total number of elements in array in count. But each time automatically last element will be added dynamically by this method.

If you still have some doubt then search dynamic arrays in C in google :))

Regards
Jul 2 '07 #14
OK.

So you can define a dynamic array like
Expand|Select|Wrap|Line Numbers
  1. int *array = new int[];
  2. int count = 0;
  3.  
  4. for (int i = 0; i <whatever time you want to insert the new element; i++)
  5. {
  6. cin>>value; //scanf incase of C
  7. array[count] = value; //This will add to array after last element
  8. count++;
  9. }
  10.  
Finally u will get total number of elements in array in count. But each time automatically last element will be added dynamically by this method.

If you still have some doubt then search dynamic arrays in C in google :))

Regards
Is there any alternative to this in C++ ?

Regards,
Girish.
Aug 20 '07 #15
Banfa
9,065 Expert Mod 8TB
Is there any alternative to this in C++ ?
Use a vector
Aug 20 '07 #16
JosAH
11,448 Expert 8TB
You can only find the length of an array when you can refer to that array *not* in
a value context, i.e. the array name is *not* passed as a parameter, nor is it
dynamically allocated; only *then* you can do this:

Expand|Select|Wrap|Line Numbers
  1. #define LENGTH(a) (sizeof(a)/sizeof(*a))
  2. ...
  3.  
  4. int a[42];
  5. int l= LENGTH(a); // l == 42;
  6.  
kind regards,

Jos
Aug 20 '07 #17
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. int *array = new int[];
  2. int count = 0;
  3.  
  4. for (int i = 0; i <whatever time you want to insert the new element; i++)
  5. {
  6. cin>>value; //scanf incase of C
  7. array[count] = value; //This will add to array after last element
  8. count++;
  9. }
  10.  
This results in corrupted memory on my computer (using MSVC 6.0)

C99 supports dynamic arrays, but this isn't C it's C++ and C++ doesn't since it has the vector to deal with that problem.
Aug 20 '07 #18

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

Similar topics

4
by: chennakeshava_ramesh | last post by:
hi, I have a problem, I am not able to find out which day of the week it is using the calendar class. I am using set() function to set the date and want to find out which day i.e mon,tue etc of...
4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
1
by: Bob | last post by:
Can anyone make this legible (format?) and tell me if there's any error or something that calls a pop-up "message dialog box" ? Cheers... q18=0;q19=new Array();q61...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
1
by: vmoreau | last post by:
I have a text and I need to find a Word that are not enclosed in paranthesis. Can it be done with a regex? Is someone could help me? I am not familar with regex... Example looking for WORD:...
30
by: =?Utf-8?B?VGhvbWFzVDIy?= | last post by:
Hello, I have an array that holds 4 values ( they contain random numbers). I would like to find the lowest value (if there is a tie i would like to find one of the tie.) then remove that value....
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.