Home > Back-end >  How to calculate the length of a pointer array C language
How to calculate the length of a pointer array C language

Time:09-18

As shown in the following code, I want to Count function of calculating the length of the array arr in the main function, but is not correct, should be how to calculate???
 
#include
#include
Int Count (int * arr)
{
int n=0;
//the code below calculation is not correct
While (+ n (arr)!=NULL) {
n++;
}
return n;
}
Int main ()
{
Int arr [6]=,4,5,8,12,20 {1};

Printf (" count=% d \ n ", the count (arr));

Retunrn 0;
}

CodePudding user response:

Array arr is not the end of the logo, the count function to calculate out the length of it,

CodePudding user response:

reference 1st floor qiu_shaofeng response:
array arr is not the end of the logo, the count function could not calculate the length of it,

That I must finish such a function is how to achieve?

CodePudding user response:

For example: the known nanjing road first house number is 100, nanjing road for a total of how much a house number? (can make this topic, can solve your problem)
An array name is the serial number of the first of the array 0.


CodePudding user response:

The elements in the array is not null-terminated, and no NULL elements,

Array length: in the main function, perform n=sizeof (arr)/sizeof (arr [0]).

CodePudding user response:

Unless there is a string, child function can't work out int the length of the array

CodePudding user response:

You can at the end of the int array with a special number as the closing tag, as long as meet the number, will exit the loop and thereby obtain the int the length of the array, which is a character array method used to determine the length of the string,
Otherwise, due to the C does not provide array bounds checking, you are unable to detect an int array how long,

CodePudding user response:

references 4 building self-confidence boy reply:
element in the array is not a null-terminated, and no NULL element,

Array length: in the main function, perform n=sizeof (arr)/sizeof (arr [0]).

My purpose is to find out the length of a pointer array in a child function, not in the main

CodePudding user response:

references 4 building self-confidence boy reply:
element in the array is not a null-terminated, and no NULL element,

Array length: in the main function, perform n=sizeof (arr)/sizeof (arr [0]).

My purpose is to find out the length of a pointer array in a child function, not in the main

CodePudding user response:

refer to the eighth floor @ __Chao -l response:
Quote: refer to 4th floor confident boy reply:
element in the array is not a null-terminated, and no NULL element,

Array length: in the main function, perform n=sizeof (arr)/sizeof (arr [0]).

My purpose is to find out the length of a pointer array in a child function, not in the main in

Good is bad, unless you define the value of the last element in the array, or how to go to pray?

In addition, special use a subroutine to put array length instead of complication,

An array is passed to the function, it is not an array, but the pointer, this you need to pay attention to

CodePudding user response:

Count (arr, 6), the length of multiple parameters

CodePudding user response:

Or change the calling function to Count (& amp; Arr), function as inr Count ((* arr) [6])

CodePudding user response:

Sizeof (arr)/sizeof (arr) [0] why can express every for many times, also waste of running time

CodePudding user response:

refer to 12 floor thou farmland exposure of reply:
sizeof (arr)/sizeof (arr) [0] why can express every for many times, also waste run time



refer to 7th floor @ __Chao -l response:
Quote: refer to 4th floor confident boy reply:
element in the array is not a null-terminated, and no NULL element,

Array length: in the main function, perform n=sizeof (arr)/sizeof (arr [0]).

My purpose is to find out the length of a pointer array in a child function, not in the main in


Sizeof (arr)/sizeof (arr [0]) is not good, want to write "diao" look a bit, you use a macro, such as
# define _count_array (arr) (sizeof (arr)/sizeof (arr) [0])
After use it _count_array time (arr), is the size of the array
  • Related