如何在main函数中获取数组元素? [英] How to get elements of array in main function?

查看:108
本文介绍了如何在main函数中获取数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我知道错误是在其他函数中定义的[] anf ef数组与main函数中声明的数组不同虽然我没有在main函数中声明但是

假设它声明但是它也不会起作用所以我怎样才能在main中获得[]和ef []的元素?



我尝试过:



here i know the error is that the array of[] anf ef defined in other functions are not same as that declared in main function although i have not declared in main function but
assume that it is declared but then also it will not work so how could i get elements of of[] and ef[] in main?

What I have tried:

#include<stdio.h>
int n,j,k,l;
int main()
{
int i,m,q;
printf("enter value of size of array =n\n");
scanf("%d",&n);
int ea[n];
printf("enter elements\n");
for(i=0;i<n;i++)
scanf("%d",&ea[i]);

for(j=0;j<n;j++)
{
    if(ea[j]%2==0)
    eea(&ea[j]);
    else
    oea(&ea[j]);
}
for(m=0;m<=k;m++)
printf("%d ",of[m]);
printf("\n");
for(q=0;q<=l;q++)
printf("%d ",ef[q]);

}
void oea(int *x)
{   static int p=0;
int of[n];
    of[p]=*x;
    p++;
    k=p;
}
void eea(int *x)
{
static int f=0;
int ef[n];
    ef[f]=*x;
    f++;
    l=f;
    
}

推荐答案

闻起来像我的作业...



Smells like homework to me...

void oea(int *x)



将它从方法更改为返回值/对象的函数。



如果你问Google Gods:如何创建c ++功能 [ ^ ],他们将回答 C ++函数 [ ^ ]以及更多...


Change it from a method to a function that returns a value/object.

If you ask the Google Gods: how to create a c++ function[^], they will answer C++ Functions[^] and much much more...


scanf("%d",&n);
int ea[n];



您不能以这种方式从变量声明数组。如果这是C ++,则需要使用 new 运算符;如果是C代码,则需要使用 malloc


You cannot declare arrays from variables in this way. You need to use the new operator if this is C++, or malloc if this is C code.


首先,学会缩进代码,使其易于阅读。

First of all, learn to indent your code, it ease its reading.
#include<stdio.h>
int n,j,k,l;
int *oea(int*);
int *eea(int*);
int main()
{
	int i,m,q,*y,*z,a,b,arry[k],arrz[l];
	printf("enter value of size of array =n\n");
	scanf("%d",&n);
	int ea[n];
	printf("enter elements\n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&ea[i]);
	}
	printf("hi");
	for(j=0;j<n;j++)
	{
		if(ea[j]%2==0)
		{
			y=eea(&ea[j]);
			for(a=0;a<=k;y++)
				arry[a]=*y;
		}
		else
		{
			z=oea(&ea[j]);
			for(b=0;b<=l;y++)
				arrz[b]=*z;
		}

	}
	for(m=0;m<=k;m++)
		printf("%d ",arry[m]);
	printf("\n");
	for(q=0;q<=l;q++)
		printf("%d ",arrz[q]);

}
int *oea(int *x)
{   static int p=0;
	int of[n];
	of[p]=*x;
	p++;
	k=p;
	return &of[p];
}
int *eea(int *x)
{
	static int f=0;
	int ef[n];
	ef[f]=*x;
	f++;
	l=f;
	return &ef[f];
}



第二:避免使用单字母变量:一个聪明的名字可以让您更轻松地理解您的代码。

第三:发表评论在你的代码中,它有助于理解你的意图。

-----

当你不明白你的代码在做什么或为什么它做它的作用时,答案是 debugger

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Second: avoid single letter variables: a clever name can ease the understanding of your code.
Third: put comments in your code, it help to understand what is your intend.
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何在main函数中获取数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆