0

I want to make a function to pass and return values from the same arguments. For example prm3=0 after function's execution must become prm3=3, and next prm3=6, and next prm3=9 ...

I wrote the following but it doesn't work. p1,p2,p3 are not passed to prm1, prm2, prm3. What is wrong? Any example returning more than one values?

#include <stdio.h>

void increments (int prm1, int prm2, int prm3);
int	prm1=0, prm2=0, prm3=0;

int main(void)
{
	while (1){
		increments (prm1, prm2, prm3);
	}
	return (0);
}

void increments (int p1, int p2, int p3){
	p1 += 1;
	p2 += 2;
	p3 += 3;
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by kzdev
0

I want to make a function to pass and return values from the same arguments. For example prm3=0 after function's execution must become prm3=3, and next prm3=6, and next prm3=9 ...

You are passing parameters by value. Pass pointers instead.

#include <stdio.h>

void increments (int* p1, int* p2, int* p3);
int prm1 = 0, prm2 = 0, prm3 = 0;

int main(void) {
  increments (&prm1, &prm2, &prm3);
  return (0);
}

void increments (int* p1, int* p2, int* p3){
	*p1 += 1;
	*p2 += 2;
	*p3 += 3;
}

Also it is strongly recommended for you to read K&R or any other referencial manual about C language.

0

ok, thanx for your response, I will follow your suggestions.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.