当前位置:网站首页>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;
}
边栏推荐
- List of common bugs in software testing
- js面试收藏试题1
- Cubemx DMA notes
- One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
- Markdown编辑语法
- Idea automatic package import and automatic package deletion settings
- Getting started with pytest -- description of fixture parameters
- Learn AI safety monitoring project from zero [attach detailed code]
- Mysql database learning
- fastText文本分类
猜你喜欢

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

Here comes the chicken soup! Keep this quick guide for data analysts

Change deepin to Alibaba image source

Future trend of automated testing ----- self healing technology

How to modify data file path in DM database

Solution of DM database unable to open graphical interface

Online incremental migration of DM database

Cultivate primary and secondary school students' love for educational robots

Virtual machine installation deepin system

About PROFIBUS: communication backbone network of production plant
随机推荐
Realize the function of data uploading
记录一次Unity 2020.3.31f1的bug
Mysql database learning
JS interview collection test question 1
List of common bugs in software testing
农业生态领域智能机器人的应用
Exercise notes 13 (effective letter ectopic words)
VMware installation win10 reports an error: operating system not found
Ruby replaces gem Alibaba image
C# 图片显示占用问题
TypeScript类的使用
win11安装pytorch-gpu遇到的坑
从数组中找出和为目标的下标
Let genuine SMS pressure measurement open source code
Promise all()
Use of typescript classes
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Oracle stored procedure and job task setting
Keil compilation code of CY7C68013A