# Thread: Pointers and Call-by-reference functions

1. Changed "a" to pass by pointer

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void program_intro(void);
void input_read(float *a, float b, float c, float x_initial, float x_final, float x_delta);
int main()
{
float a, b, c, x_initial, x_final, x_delta;
program_intro();
input_read(&a, b, c, x_initial, x_final, x_delta);
printf("a = %2.f\n", a);
printf("b = %2.f\n", b);
printf("c = %2.f\n", c);
printf("x = %2.f\n", x_initial);
printf("x[i] = %2.f\n", x_final);
printf("delta X = %2.f\n", x_delta);
return 0;
}
void program_intro(void) //Explanation of program to the user
{
printf("This program will compute a range of values for\n");
printf("the second degree polynomial f(x) = ax^2 + bx + c,\n");
printf("the derivative f'(x) = 2ax + b, \n");
printf("the integral F(x[i]) = (a/3)x[i]^3 + (b/2)x[i]^2 + cx, and the\n");
printf("area A = F(x) - F(x[i]).\n\n");
return;
}
void input_read(float *a, float b, float c, float x_initial, float x_final, float x_delta)
{
printf("Please input the polynomial coefficients as follows:\n\n");
printf("Coefficient a: ");
scanf("%f", a);
printf("\nCoefficient b: ");
scanf("%f", &b);
printf("\nCoefficient c: ");
scanf("%f", &c);
printf("\n\nPlease input the range and increment to be tested as follows:\n\n");
printf("x: ");
scanf("%f", &x_initial);
printf("\nx[i]: ");
scanf("%f", &x_final);
printf("\ndelta x: ");
scanf("%f", &x_delta);
return;
}```

Tim S.

2. Originally Posted by stahta01
In C, you can only return at most a single variable value.

Therefore this line will not work.
Code:
`return a, b, c, x_initial, x_final, x_delta;`
This can be fixed by using pointers.

Tim S.
Would I modify the prototype and definition to be *a, *b,.. and the declaration and return be &a, &b,...? I'm thinking my printf's need the same &a treatment?

3. Originally Posted by scotdani
Would I modify the prototype and definition to be *a, *b,.. and the declaration and return be &a, &b,...? I'm thinking my printf's need the same &a treatment?
I did the changes for parameter "a" and posted that changed code.

Edit: Link to the post that you seemed to have ignored Pointers and Call-by-reference functions

Tim S.

4. Originally Posted by stahta01
I did the changes for parameter "a" and posted that changed code.

Edit: Link to the post that you seemed to have ignored Pointers and Call-by-reference functions

Tim S.
Look, I'm not interested in being belittled for my novice experience with C, and it was a timing issue that I did not immediately see your edited code.

That being said, thank you for demonstrating what the syntax looks like for pointers in this example. This is how I am learning the ways of C, something reading cannot match.

A professor told me this once: "If you don't understand what's on page two, you're not going to understand page 8."

5. C doesn't have pass by reference, it only has pass by value (a pointer is a value) so the declaration would be as Tim provided and to call it use &var (which is a pointer to var so it will match what the function wants as an argument/s)

6. Originally Posted by Hodor
C doesn't have pass by reference, it only has pass by value (a pointer is a value) so the declaration would be as Tim provided and to call it use &var (which is a pointer to var so it will match what the function wants as an argument/s)
Ok I'm starting to get it now. When using pointers in main, say if I took this function and place it in main, how different or similar would it look?

7. I'm not sure what you mean by "if I took this function and place it in main". C doesn't really support functions inside functions. Did you mean call this function from main?

Perhaps go back and check out post #2. That shows a clear example of the difference between passing in a thing and a pointer-to-a-thing, including how to call it from main.

You're doing something similar, but with several parameters, and they're type double instead of int.

8. Originally Posted by anduril462
I'm not sure what you mean by "if I took this function and place it in main". C doesn't really support functions inside functions. Did you mean call this function from main?

Perhaps go back and check out post #2. That shows a clear example of the difference between passing in a thing and a pointer-to-a-thing, including how to call it from main.

You're doing something similar, but with several parameters, and they're type double instead of int.
What I was thinking was how to pass pointers in main only compared to between main and a function. One of the assignment constraints is to scan for the variables in main then use a function for each calculation using pointers to these variables. I have trouble visualizing it unless I write it myself since I'm not a programmer.

Which brings me to the next step in the assignment. The functions mentioned above are to use an incremented loop from x[I] to x[f] at increment delta[x]. Assuming this is an array, say x_range[N], this needs to be a pointer as well for the functions to run through the stored values; would it be smart to make an array for storing each calculated value too? I'm a little rusty on for loop structure with arrays; below is an outline of what I think it might look like, but I'm very uncertain.

Code:
```#define NMAX 100
double base_poly_func(*x_range[N];)
//
//Previous code with scans
//
double x_range[NMAX];
double base_poly_results[N];

for (i=0; N<NMAX; N+x_delta) //loop for getting all values in data set
{
x_range[N] += x_initial + x_delta;
}

for (i=0; N<NMAX; N++)
{
base_poly_results[N] += base_poly_func(&x_range[N]);
}

//
//more code to follow
//
double base_poly_func(*x_range[N])
{
double base_poly_eq;
base_poly_eq = x;  //actual equation goes here
return;
}```

9. For, While and Do While Loops in C - Cprogramming.com
Functions in C - Cprogramming.com
Quote from Arrays in C - Cprogramming.com
The fact that arrays can act just like pointers can cause a great deal of confusion.
Tim S.

10. Modified code from Arrays in C - Cprogramming.com
to show the normal way to pass an array to an function

Code:
```#include <stdio.h>
#include <stdlib.h>

int CheckForA(char * array, int size);

#define ARRAY_SIZE 10

int main()
{
char astring[ARRAY_SIZE] = {0};

/* Using scanf isn't really the best way to do this; we'll talk about that
in the next tutorial, on strings */
scanf( "%s", astring );

if (CheckForA(astring, ARRAY_SIZE))
{
printf( "You entered an a!\n" );
}
return 0;
}

int CheckForA(char * array, int size)
{
int i = 0;

for ( i = 0; i < size; ++i )
{
if ( array[i] == 'a' )
{
return 1; /* true */
}
}
return 0; /* false */
}```
Tim S.

11. Originally Posted by scotdani
I have trouble visualizing it unless I write it myself since I'm not a programmer.
I suggest you avoid taking classes the requires programming; since, you do *not* seem to want to become a programmer!

Tim S.

12. Originally Posted by stahta01
I suggest you avoid taking classes the requires programming; since, you do *not* seem to want to become a programmer!

Tim S.
I certainly agree if I had the choice in the curriculum. I'm just looking for help to get a good grade and attempt to understand it. I hold no shame in holding someones hand through the programming portions.

13. FYI: Your code in post #23 was filled with errors; I posted links and example code to help you fix your code in the two posts after it.
Pointers and Call-by-reference functions

I have no plans to fix your bad code; it is so bad that it is not likely anyone will fix it for you.