当前位置:网站首页>The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
2022-07-02 05:00:00 【Jimmy_ jimi】
Problem description
sizeof() The function is to find the memory space occupied by the array ( It's not the length ). When using this method to find the length of an array in a function , As the array is passed into the function as a function parameter, it will “ degeneration ” For the pointer , Therefore, it will lead to sizeof(arr) What we get is the memory space size of a pointer , Not the memory space of the array –> Thus, the calculated length is wrong
// Enter a number to see if there is this number in the series
#include <stdio.h>
int main ()
{
int a[]={
1,2,3,4,34,455,23,4,7,1,9,6,8,};
int i=0,x,logic=0,n;
int fun(int x,int a[],int logic);
printf(" Please enter a number :\n");
scanf("%d",&x);
n=fun(x,a,logic);
if(n==-1)
printf(" The number was not found in the sequence \n");
else{
printf(" Enter this number, which is the number %d position \n",n);
}
printf("%d\n",n);
return 0;
}
int fun(int x,int a[],int logic)
{
logic=-1;
int i;
for (i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
if(x==a[i])
{
logic=i;
break;
}
}
return logic;
}
Solution :
int main() {
int a[] = {
1,2,3,4,34,455,4,7,1,98,6 };
int len = sizeof(a) / sizeof(a[0]);
int i = 0, x, logic = 0, n;
int fun(int x, int len, int a[], int logic);
printf("please input a num: \n");
scanf("%d", &x);
n = fun(x, len, a, logic);
if (n == -1)
printf("not find");
else {
printf("at %d ", n);
}
return 0;
}
int fun(int x, int len,int a[], int logic) {
logic = -1;
for (int i = 0; i < len; i++) {
if (x == a[i]) {
logic = i;
break;
}
}
printf("%d\n", logic);
return logic;
}
边栏推荐
- Knowledge arrangement about steam Education
- 正大美欧4的主账户关注什么数据?
- Markdown编辑语法
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- Introduction to Luogu 3 [circular structure] problem list solution
- 函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
- Application of intelligent robot in agricultural ecology
- About PROFIBUS: communication backbone network of production plant
- List of common bugs in software testing
- Summary of database problems
猜你喜欢
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b

Federal learning: dividing non IID samples according to Dirichlet distribution

Markdown编辑语法

2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas

数学知识(欧拉函数)

Common errors of dmrman offline backup

Solution of DM database unable to open graphical interface

How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer

Cultivate primary and secondary school students' love for educational robots

Research on the security of ognl and El expressions and memory horse
随机推荐
GeoTrust ov multi domain SSL certificate is 2100 yuan a year. How many domain names does it contain?
Research on the security of ognl and El expressions and memory horse
Domestic all Chinese automatic test software apifox
How to configure PostgreSQL 12.9 to allow remote connections
Markdown edit syntax
06 decorator mode
Save the CDA from the disc to the computer
Let genuine SMS pressure measurement open source code
geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
National all Chinese Automatic Test Software apifox
C case of communication between server and client based on mqttnet
Rhcsa --- work on the fourth day
Markdown编辑语法
Realize the function of data uploading
go实现leetcode旋转数组
leetcode两数相加go实现
CubeMx DMA笔记
初学爬虫-笔趣阁爬虫
正大美欧4的主账户关注什么数据?
数据库问题汇总