-
arrays and pointers
[code]
i was jsut wondering how to pass in an array to a funciton using a pointer. so far i have declared 3 parallel arrays and now i want to pass the elements in the array into a function. would i use a poitner or do i change the varibles in the funciton so thye match the array?
-
The name of the array is useful for this. It points to the base of the array, but unlike a normal pointer, it's address can't be changed or it's type, cast.
Code:
yourFunction(arrayname1, arrayname2, arrayname3);
Remember that, to the function being called, the arrayname is just a pointer, so:
Code:
void yourFunction(double *arrayname1, double *arrayname2, double *arrayname3)
{
etc....
or
void yourFunction(double arrayname1[], double arrayname2[], double arrayname3[])
{
etc...
Wouldn't it be easier if you kept this all in one thread?
-
void
can u explain to me what the void does and why it is used?
ive read up on it but it still doent make much sense to me
-
In place of another type when declaring functions, it simply means that function doesn't return anything.
Quzah.
-
Code:
double function(double, double, double);
int
main (int argc, char **argv){
setvbuf(stdout, NULL, _IONBF, 0);
double a,b,c;
int n=0;
while(scanf("%lf %lf %lf", &a, &b, &c)==3){
printf("%.1f , %.1f ,%.2f \n", a,b,c);
function(a,b,c);
n++;
}
printf("%.1f \n", 3*(function(a,b,c)));
printf("Number of loops: %d",n);
return 0;
}
double
function(double a, double b, double c){
float d, e, f, g;
d=sqrt((a*a)+(b*b));
e=(2+c)*PI*x*;
f=pow(3,(e/3));
g+=f;
return g;
}
so here is my function. my problem is that whne i run it, it doesnt actually add up all the f to the g-as in g+=f has no effect at all. it only takes the lsat input to acccount. i dont know how to change this
also, can u please start me off with how to add the arrays and also so that reads in data...
sorry for not putting this in on post. i thought i would name it by wat my problem was.
-
Why are d, e, f, g declared as float, when the rest of the function uses double?
g is never initialized, and then you add f to it. That's going to result in a fairly unpredictable result, completely unrelated to your actual calculation.
And of coruse, if you want to accumulate the result of the function, you may want to do that in main, or pass a pointer to a variable to the function. Your variable g disappears after the function exits, so it won't be able to hold a value beyond the end of the function[1]
[1] There is a way to have variables that keep their value using the static keyword, but that is not what you SHOULD be using for this type of function.
--
Mats
-
thanks! it worked...i took the g+=f out of the function and into the while loop...now its wokring perfectly...
can anyone now tell me how i can arrays in this program?the number of souces is of no more than 25
so i was thinking of putting
double array[3];
and then i know i have to have some kind of for loop
for (n=0;n<=25;n++){
im not sure how the rest works
and how can i pass an array into a function?
this is all so confusing
-
It depends on what you're planning on using your array for. If you want to simply lump three values together, then an array of 3 elements is fine. If you are just passing three elements, and smashing them into a single value, and doing that twenty-five times, then maybe you want an array of 25. If you want three values, twenty-five times, then maybe an array that's 25x3 is what you want.
Code:
void foo( float array[3] )
{
...do stuff...
}
float array[3]; /* make an array */
...
foo( myarray ); /* call function */
Now, arrays actually arrive at their function as a pointer to an element of that type. So it doesn't actually keep track of the array's size in the function itself. You're going to have to pay attention to that on your own.
Quzah.
-
ok so before i pass it into the loop, how would i declare the 3 arrays i need and how can i add pointers ? can i please get an example..
-
Code:
double aa[N];
double ab[N];
double ac[N];
int
main (int argc, char **argv){
setvbuf(stdout, NULL, _IONBF, 0);
double a,b,c;
int n=0;
while(scanf("%lf %lf %lf", &a, &b, &c)==3){
for (i=0;i<3;i++){
printf("%.1f , %.1f ,%.2f \n", aa[i], ab[i] , ac[i]);
}
SPL+=SPL_tot(x,y,W_i);
n++;
}
so now when i run this and input
2 6 7
3 4 6
it will print out each line twice but with the value 0. i tried to change the loop to i<40 and all it does is print out each line 40 times with the value 0
how can i fix this ..wat am i doing wrong here
-
Where in the code are you initializing globals aa[], ab[], ac[]?
Uninitialized globals are set to zero by default.
-
ok so i set aa[N]=a;
ab[N]=b;
ac[N]=c;
is that correct?
but then im not sure how to make it read in the input and point at the variable a, b and c...
-
You probably want n, not N, since n is the current item number, and N is the number of elements in the array. ab[N] is actually the (N+1)th element in the array, so it's outside the valid range.
--
Mats
-
ok but since its a global variable, its telling me that a b and c arent declared now
-
I think you need to post your updated code.
--
Mats