Thread: arrays and pointers

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    23

    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?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    23

    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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In place of another type when declaring functions, it simply means that function doesn't return anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    23
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    23
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 05-21-2009 at 05:53 AM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    23
    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..

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    23
    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

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Where in the code are you initializing globals aa[], ab[], ac[]?
    Uninitialized globals are set to zero by default.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    23
    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...

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    23
    ok but since its a global variable, its telling me that a b and c arent declared now

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you need to post your updated code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM