当前位置:网站首页>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;
}
边栏推荐
- How to recover deleted data in disk
- Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
- Mouse events in JS
- C # picture display occupancy problem
- Exercise notes 13 (effective letter ectopic words)
- Use of Baidu map
- 农业生态领域智能机器人的应用
- Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
- Analyzing the hands-on building tutorial in children's programming
- Promise all()
猜你喜欢

06 decorator mode

農業生態領域智能機器人的應用

Knowledge arrangement about steam Education

正大留4的主账户信息汇总

Steam教育的实际问题解决能力

C# 基于MQTTNet的服务端与客户端通信案例

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

Express logistics quick query method, set the unsigned doc No. to refresh and query automatically

Change deepin to Alibaba image source
![[high speed bus] Introduction to jesd204b](/img/78/1a0a3672e63058da6d98da95aa3cf2.jpg)
[high speed bus] Introduction to jesd204b
随机推荐
C - derived classes and constructors
About PROFIBUS: communication backbone network of production plant
How to write a client-side technical solution
Summary of database problems
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
The underlying principle of go map (storage and capacity expansion)
TypeScript函数详解
How to recover deleted data in disk
Embedded-c language-9-makefile/ structure / Consortium
奠定少儿编程成为基础学科的原理
Design and implementation of general interface open platform - (44) log processing of API services
LM09丨费雪逆变换反转网格策略
Case sharing | intelligent Western Airport
Analyze the space occupied by the table according to segments, clusters and pages
Learn AI safety monitoring project from zero [attach detailed code]
Summary of MySQL key challenges (2)
正大留4的主账户信息汇总
从数组中找出和为目标的下标
Mapping settings in elk (8) es
解决:代理抛出异常错误