当前位置:网站首页>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;
}
边栏推荐
- win10 磁盘管理 压缩卷 无法启动问题
- Change deepin to Alibaba image source
- Vmware安装win10报错:operating system not found
- Hcip day 17
- 数学知识(欧拉函数)
- Pit encountered in win11 pytorch GPU installation
- 数据库问题汇总
- Super detailed pycharm tutorial
- Online incremental migration of DM database
- Flag bits in assembly language: CF, PF, AF, ZF, SF, TF, if, DF, of
猜你喜欢
解决:代理抛出异常错误
C case of communication between server and client based on mqttnet
ansible安装与使用
Gin framework learning code
About PROFIBUS: communication backbone network of production plant
Markdown edit syntax
VMware installation win10 reports an error: operating system not found
Introduction to Luogu 3 [circular structure] problem list solution
C# 基于MQTTNet的服务端与客户端通信案例
Embedded-c language-8-character pointer array / large program implementation
随机推荐
MMAP zero copy knowledge point notes
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
Knowledge arrangement about steam Education
Comp 250 parsing
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Application d'un robot intelligent dans le domaine de l'agroécologie
Hcip day 17
Cultivate primary and secondary school students' love for educational robots
Leetcode basic programming: array
Application of intelligent robot in agricultural ecology
正大留4的主账户信息汇总
Feign realizes file uploading and downloading
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
Summary of common string processing functions in C language
TypeScript函数详解
How to modify data file path in DM database
06 decorator mode
数学知识(欧拉函数)
MySQL table insert Chinese change? Solution to the problem of No
Mouse events in JS