The method of solving the array length of C language fetters __c language

Source: Internet
Author: User
Tags array length

In C, we often need to know the length of an array, but because of the nature of the language itself, so there is no particularly simple and straightforward way to achieve the effect, so write some methods here for your reference.

Note: The array in this blog is a one-dimensional array, and the complete code will be posted at the bottom of the article accordingly. And this article is designed to solve the problem, but there are a lot of places to be optimized are not written out. For example: When you pass a parameter to a function, you can greatly reduce the amount of code based on the template function. If interested children can try to do it, if anyone wants to see it, I can write a tutorial like that. Thank you.

1, through the strlen () method of the band

header file: #include <string.h>//Don't forget, otherwise it won't work.

Operation Scope: An array of char types that do not work on an int array

Local Example:

Char arr[] = "ASDFGH";
/* If the arr array is an int at this time, it will be an error, because the strlen () itself implementation of the limit/
strlen (arr);


2, through the sizeof () method

The first step we found is that there are many limitations to the use of thestrlrn () method, so what is the effect of the second method? We wait and see.

principle: through the strlen () function we can get the length of a set of elements n, in the same way to get the length of an element p, so the total length of the element m=n/p. This is like our shopping to buy the same kind of goods cost 1000 yuan, know the item price is 100 yuan, then total bought how many items. Obviously, 1000/100 = 10 (a) is the number we buy Ah, this is not the same reason.

Simple code fragment:

/* This is a char array *
/char arr1[] = "ASDFG";
int len1 = sizeof (ARR1)/sizeof (arr1[0));
/* This is an int-type array *
/int arr2[] = {1,2,3,4};
int len2 = sizeof (ARR2)/sizeof (arr2[0)); 

The problem is that it is not feasible to get the length of an array by passing the array name parameter to a child function. Because the incoming a is just a pointer, the result ofsizeof (a) is that the pointer variable A is the size of the memory.

int GetLength (int[] a) {
    int length;
    Length=sizeof (a)/sizeof (a[0]); This is wrong and the result is always 1 return
    length;

3, by traversing the array to find the length of the array

The first two methods are through the function of the system to help us solve the length of the array, then there is a way we can define the function to solve the length of the arrays, of course, can be implemented, and the way to implement is not one. Now let's explore it together.

In the language of C, for an array or string, to determine if the value is equal to "NULL", "0", "/0" At this time, we can use this property to traverse the array (three ways to traverse the array: that is, with counter simulations, pointer subtraction, function recursion) Oh.

3.1 Counter Simulation

Coding 1 This method is used to determine the length of the string, as shown in the complete example Example3.1 (bottom of the article)
 int my_strlen1 (char *str)  
    {  
        int count = 0;  
        while (*str!= ' ")  
        {  
            str++; 
            count++;  
        }  
        return count;  
    } 

Coding 2 This method is used to determine the length of an integer array, complete example reference 3.1 (at the bottom of the article)
int my_strlen2 (int *str)  
    {  
        int count = 0;  
        while (*str!= ' ") 
		{	
			str++;
			count++;
		 } 
        return count;  
    }   

Note: For the function of the array parameters are not familiar with children's shoes can go to the end of the article reference 4 to find OH.

3.2 Pointer reduction pointer

Coding 3 This method is used to compute the length of a char array. For more information, see Example3.2 (bottom of article)
int my_strlen1 (char *str)  
{  
    char *p = str;  
    while (*p!= ' ")  
    {  
        p++  
    }  
    return p-str;  
Coding 4 This method computes the length of an array of int, see Example3.2 (at the bottom of the article)
int my_strlen2 (int *str)  
{  
    int *p = str;  
    while (*p!= ' ")  
    {  
        p++  
    }  
    return p-str;  
}   

3.3 Using recursive return traversal

   Coding 5 for details see Example3.3 (bottom of article)/
* Char array length
    /int my_strlen1 (char *str)  
    {  
        if (*str = NULL)  
            return 0;  
        else return  
        1+my_strlen1 (str+1);  
    }
  Coding 6 Details see Example3.3 (bottom of article)/
* Integer array length * *
   int my_strlen2 (int *str)  
    {  
        if (*str = NULL)  
            Retu RN 0;  
        else return  
        1+my_strlen2 (str+1);  
    }   

4, Summary

Careful children's shoes are not difficult to find, in the third step of the process, we used: "NULL", "0", "/0" to traverse an array, I hope you can find a better way. Come on. The future has come, just not yet popular

------------------------------------------------------

Example1 (Strlrn get length code)

#include "stdio.h"
#include "string.h"
/* This method simply implements the input of char string length
/int main () {
	char arr[] = "ASDFG" ;
	int n = strlen (arr);
	printf ("%d", n);
	return 0;
}

Example2 (sizeof way to get length code)

#include "stdio.h"
#include "string.h"
/* Please note that at this time the sizeof is called in the main function in the main
() {
	char arr1[] = " ASDFG ";
	int len1 = sizeof (ARR1)/sizeof (arr1[0));
	printf ("%d", len1);
	int arr2[] = {1,2,3,4};
	int len2 = sizeof (ARR2)/sizeof (arr2[0));
	printf ("%d", len2); 
	return 0;
}

Example3.1 (counter simulation get length code)

    #include <stdio.h>  
	/* Char array length *
    /int my_strlen1 (char *str)  
    {  
        int count = 0;  
        while (*str!= ' ")  
        {  
            str++; 
            count++;  
        }  
        return count;  
    } 
    /* int array length * 
	/int my_strlen2 (int *str)  
    {  
        int count = 0;  
        while (*str!= ' ") 
	{	
		str++;
		count++;
	} 
        return count;  
    }   
    int main ()  
    {  
        int len1,len2; 
    	Char arr1[] = "ABCDEFGH"; 
		int  arr2[] = {1,2,3,4,5};   
        Len1 = My_strlen1 (arr1);  
        Len2 = My_strlen2 (arr2);
        printf ("%d\n", len1); 
		printf ("%d\n", len2);  
        return 0;  
}  
  

Example3.2 (pointer subtraction pointer gain length code)

#include <stdio.h> 
/* Char array length * 
/int my_strlen1 (char *str)  
{  
    char *p = str;  
    while (*p!= ' ")  
    {  
        p++  
    }  
    return p-str;  
} 
/* int-type array length * 
/int my_strlen2 (int *str)  
{  
    int *p = str;  
    while (*p!= ' ")  
    {  
        p++  
    }  
    return p-str;  
}   
int main ()  
{  
    int len1 = 0,len2 = 0;  
    Char arr1[10]= "ABCDEFGH";
	int arr2[10]={1,2,3,4,5};   
    Len1 = My_strlen1 (arr1);  
    Len2 = My_strlen2 (arr2);  
    printf ("%d\n", len1); 
	printf ("%d\n", len2);   
    return 0;  

Example3.3 (Recursive get length code)

    #include <stdio.h>  
    /* Char array length */
    int my_strlen1 (char *str)  
    {  
        if (*str = NULL) return  
            0;  
        else return  
        1+my_strlen1 (str+1);  
    }
    /* Integer array length */
	int my_strlen2 (int *str)  
    {  
        if (*str = NULL) return  
            0;  
        else return  
        1+my_strlen2 (str+1);  
    }   
    int main ()  
    {  
        int len1 = 0,len2 = 0;  
        Char arr1[10]= "ABCDEFGH"; 
		int  arr2[10]={1,2,3,4,5};  
        Len1 = My_strlen1 (arr1);
		Len2 = My_strlen2 (arr2);    
        printf ("%d\n", len1); 
		printf ("%d\n", len2);  
        return 0;  
    }  


Reference: 1, C language to find the length of the array

2, C language How to get the length of the array

3, three methods of simulating strlen function

           4,c language fetters int array and function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.