Click here to Skip to main content
15,881,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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();
	}

}
Posted
Updated 19-Nov-18 11:46am
v3
Comments
Aydin Homay 19-Nov-18 17:10pm    
AS far as I know regarding the code that you have provided above you are passing the struct Count to the printCode by value not by reference, could you please make your question more clear what are you trying to achieve?
Member 14060076 19-Nov-18 17:17pm    
@Aydin Homay yes i am trying to printCode by value. 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 57... And i want it to be 5768etc. I hope it is clear enough

1 solution

There are many mistakes in you code. Did you possibly meant
C
#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;
}

?
 
Share this answer
 
Comments
Member 14060076 19-Nov-18 17:55pm    
That is what i am looking for! Thank you! Can i ask what this i < sizeof(List)/sizeof(List[0]) is going on? I am a rough beginner in C, sorry for the mistakes as i'll learn from them.
raddevus 19-Nov-18 18:14pm    
sizeof(List) returns the number of bytes the entire list takes in memory.
sizeof(List[0]) returns the number of bytes one element take in memory.
If Int is Int32 then each one takes 4 bytes.
So if you have 4 elements in your list then sizeof(List) returns 16 bytes.
And, of course, sizeof(List[0]) returns 4.
16 / 4 = 4
so while i < 4 (0 - 3) the for loop will execute.
CPallini 20-Nov-18 9:15am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900