当前位置:网站首页>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;
}
边栏推荐
- 培养中小学生对教育机器人的热爱之心
- Oracle stored procedure and job task setting
- Change deepin to Alibaba image source
- Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
- Here comes the chicken soup! Keep this quick guide for data analysts
- Getting started with pytest -- description of fixture parameters
- Pit encountered in win11 pytorch GPU installation
- Markdown edit syntax
- Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
- 農業生態領域智能機器人的應用
猜你喜欢

LM09丨费雪逆变换反转网格策略

Common errors of dmrman offline backup

Cannot activate CONDA virtual environment in vscode

奠定少儿编程成为基础学科的原理

Video cover image setting, put cover images into multiple videos in the simplest way

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

将光盘中的cda保存到电脑中

idea自动导包和自动删包设置

Let genuine SMS pressure measurement open source code

win11安装pytorch-gpu遇到的坑
随机推荐
How to write a client-side technical solution
What data does the main account of Zhengda Meiou 4 pay attention to?
C # picture display occupancy problem
Leetcode merge sort linked list
DJB Hash
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Cannot activate CONDA virtual environment in vscode
Mouse events in JS
6.30年终小结,学生时代结束
Knowledge arrangement about steam Education
正大美欧4的主账户关注什么数据?
Preparation for writing SAP ui5 applications using typescript
Leetcode- insert and sort the linked list
Mysql database learning
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
Save the CDA from the disc to the computer
國產全中文-自動化測試軟件Apifox
Embedded-c language-8-character pointer array / large program implementation
Realize the function of data uploading
Basic differences between Oracle and MySQL (entry level)