当前位置:网站首页>函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
2022-07-02 04:58:00 【Jimmy_jimi】
问题描述
sizeof()函数是求数组所占的内存空间大小(不是长度)。当在函数中使用该方法求数组长度时,由于数组作为函数参数传入函数的过程中会“退化”为指针,因此将会导致sizeof(arr)求出的是一个指针的内存空间大小,而非数组的内存空间大小–>从而导致计算的长度错误
//输入一个数看看数列里面有没有这个数
#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("请输入一个数:\n");
scanf("%d",&x);
n=fun(x,a,logic);
if(n==-1)
printf("未在数列中找到该数\n");
else{
printf("输入这个数在数列中是第%d位\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;
}
解决方案:
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;
}
边栏推荐
- 2022-003arts: recursive routine of binary tree
- 解析少儿编程中的动手搭建教程
- TypeScript类的使用
- Lay the foundation for children's programming to become a basic discipline
- What are the rules and trading hours of agricultural futures contracts? How much is the handling fee deposit?
- 解决:代理抛出异常错误
- 2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
- DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
- Idea autoguide package and autodelete package Settings
- 農業生態領域智能機器人的應用
猜你喜欢

Cultivate primary and secondary school students' love for educational robots

Leetcode merge sort linked list

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
![Introduction to Luogu 3 [circular structure] problem list solution](/img/fd/c0c5687c7e6e74bd5c911b27c3e19c.png)
Introduction to Luogu 3 [circular structure] problem list solution

Pit encountered in win11 pytorch GPU installation

Detailed process of DC-1 range construction and penetration practice (DC range Series)

Social media search engine optimization and its importance

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

Online incremental migration of DM database
![[common error] the DDR type of FPGA device is selected incorrectly](/img/f3/be66bcfafeed581add6d48654dfe34.jpg)
[common error] the DDR type of FPGA device is selected incorrectly
随机推荐
JS interview collection test question 1
Video cover image setting, put cover images into multiple videos in the simplest way
Application of intelligent robot in agricultural ecology
Getting started with pytest -- description of fixture parameters
Win10 disk management compressed volume cannot be started
idea自動導包和自動删包設置
Unity particle Foundation
[understand one article] FD_ Use of set
洛谷入门3【循环结构】题单题解
Leetcode basic programming: array
Gin framework learning code
Social media search engine optimization and its importance
Embedded-c language-8-character pointer array / large program implementation
Keil compilation code of CY7C68013A
Virtual machine installation deepin system
Knowledge arrangement about steam Education
[common error] the DDR type of FPGA device is selected incorrectly
How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
Simple and practical accounting software, so that accounts can be checked
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers