如何在没有指针的情况下将数组结构传递给函数? [英] How can I pass a struct of an array to a function without a pointer ?

查看:91
本文介绍了如何在没有指针的情况下将数组结构传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组的结构传递给没有指针的函数。



我正在尝试传递数组[],因此它将打印数组的下一个值。



下面的代码有一个List [5] = {1,2,3,4}的数组,其中第一个是int数组

将在

I am trying to pass a struct of array to a function without a pointer.

I'm trying to pass an array[], so it will print the next value of the array.

The code below has an array of List[5] = {1,2,3,4}, where the first int in the array
will be processed in

struct Count numbers()

我包含了增量i,因此printCode()将打印的整数numbers.intOne [1]& numbers.intTwo [1],numbers.intOne [2]& numbers.intTwo [2] 等...



我希望程序逐个运行数组,例如1将添加4并添加6,将打印57.下一个值将为2,这将导致6和8.但它只会从结构中获取相同的值并保持打印575757.



如何让函数继续下一个数组值(并打印58),这是否可以没有数组?



我尝试过:



I included the increment i, so the printCode() will print integers of numbers.intOne[1] & numbers.intTwo[1], numbers.intOne[2] & numbers.intTwo[2] etc...

I want the program to run the array one by one, for example 1 will be added by 4 and added by 6, which will print 57. And the next value will be 2, which will result in 6 and 8. But it will only take the same value from the struct and keeps printing 575757.

How can I make the function continue to the next array value (and print 58), and is this possible without an array?

What I have tried:

#include <stdio.h>

struct Count {      //My struct
	int intOne;
	int intTwo;
};

struct Count numbers() {
	struct Count numbers;

	int List[5] = { 1, 2, 3, 4 };  //The array

	int i;
	for (i = 0; i < 10; i++) {           
		numbers.intOne = List[i] + 4;
		numbers.intTwo = List[i] + 6;
		return numbers;
	}
};

void printCode(struct Count numbers) {
	int i;
	for (i = 0; i < 2; i++) {
		printf("%i%i", numbers.intOne, numbers.intTwo); //This thing only prints 575757
	}
}

int main() {

	int i = 0;
	for (i = 0; i < 10; i++) {
		numbers();
		printCode(numbers());
		getchar();
	}

}

推荐答案

您的代码中存在许多错误。你有没有想过

There are many mistakes in you code. Did you possibly meant
#include <stdio.h>

struct Count
{      //My struct
  int intOne;
  int intTwo;
};

void printCode(struct Count numbers)
{
  printf("%i%i\n", numbers.intOne, numbers.intTwo);
}

void printNumbers()
{
  struct Count numbers = {0};

  int List[] = { 1, 2, 3, 4 };  //The array

  int i;
  for (i = 0; i < sizeof(List)/sizeof(List[0]); ++i)
  {
    numbers.intOne = List[i] + 4;
    numbers.intTwo = List[i] + 6;
    printCode( numbers );
  }
};

int main()
{
  printNumbers();
  getchar();
  return 0;
}




?


这篇关于如何在没有指针的情况下将数组结构传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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