Thread: Function w/ pointer errors

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    Function w/ pointer errors

    When I call the following function:
    Code:
    void avg_sum (float a [10], int n=9 , float *avg, float *sum) {
    
    
    	int i;
    	float s, v:
    	sum = &s;
    	avg = &v;
    	for (i = 0; i < n; i++) {
    
    		s += a [i];
    	}
    	avg = sum/n;
    }
    I get errors. I know something is wrong, but I cant put my finger on it.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    what are the errors and how are you calling the function?? I think you mean astericks as well when you assign those floats. Also, you assign the value of n to nine in the paramater list, this is a c++ property, not c. Also, look at how you are passing that array, use pointers instead.
    Last edited by chrismiceli; 03-18-2004 at 09:24 PM.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A pointer is a variable that contains an address (of some other variable). You have to use the asterisk (*) operator to read/write to the data at that address.

    Code:
    void avg_sum (
     float a [10], // don't need the size here, it's in 'n' 
     int n=9 , // C doesn't allow default parameters
    float *avg, 
    float *sum) {
    
    	int i;
    	float s, v: // don't need these, use the pointers
    	sum = &s;
    	avg = &v;
    	for (i = 0; i < n; i++) {
    
    		s += a [i];
    	}
    	avg = sum/n; // use * appropriately
    }

    This is all you have to do:


    Code:
    void avg_sum (float a[], int n, float *avg, float *sum) {
     
     (*sum) = 0;
    
    	int i;
    
    	for (i = 0; i < n; i++) {
    
    		(*sum) += a [i];
    	}
    	(*avg) = (*sum)/n; 
    }
    
    
    
     int main()
    {
     const int size = 6;
     float data[size] = {1.4, 3.0, 7.32, 2.5, 3.14, 14.1};
     float sum, average;
    
     avg_sum(data, size, &sum, &average);
    
     printf(
     "# elements: %d\n"
     "sum total: %g\n"
     "avgerage: %g\n", 
      size, sum, average);
    
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    So when do you not use the * when using a pointer. I think this whole pointer concept is going over my head.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You use the * when you want to get at the contents of the variable that the pointer is pointing to.

    for example

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
      int i = 7;
      int *p;
    
      p = &i; /* make p point to i */
                 /* p holds the address of the variable i */
    
      printf("The value of i is %d\n", *p);
      return 0;
    }
    You see how that works? p points to i, but if you want to see the contents of i, you use the * ( to dereference). I know you asked when not to use the *, but if you know when to use it, I suppose you know when not to use it also.

    A good tutorial on pointers and arrays can be found here
    Last edited by kermit; 03-18-2004 at 10:00 PM.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    in your pointer declaration, you didnt use the *, but above, the * is used. I guess that is where I am confused.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I am not following you.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    you wrote:

    p = &i;

    above, it is a function. in the previous post to yours.

    So the reason he put all the * in there, like for the *sum things, is because the fucntion is later called thats where you use the &?

    I dunno. I feel lost too.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    * is a dereferencing operator - think of it as 'the contents of' operator - when you use it, it is not the memory address, but the contents of the variable that the pointer is pointing to that you are dealing with. If you don't use it, you are dealing with the address of the variable that the pointer points to.

    Have a read of the tutorial I posted above. Look carefully at Sebastiani's code, and try and see what he has going on there.

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    I didn't even see the link. Ill take a look. thanks for the help.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Okay, these principles work for all variables (even pointers, yes, you can have pointers to pointers!). First of all, when you see a declaration:

    float * p;

    The asterisk isn't doing anything there except telling the compiler you want a pointer to a float.

    Now when you want the address of a variable, you use the amperstand, of course:

    float * p1 = NULL;
    float * p2 = NULL;
    float num = 10;

    p1 = &num;
    p2 = p1;
    *p2 = 30; // 'num' now contains the value of 30

    Notice that you don't use the dereference operator to assign a pointer to a pointer - they are the same type of variable, that's why.

    OK, pretend we could see both the address (A) and the contents (C) of the variables. The above might look like:

    float * p1 = NULL; // A: 1234 - C: 0
    float * p2 = NULL; // A: 9658 - C: 0
    float num = 10; // A: 5454 - C: 10

    p1 = &num; // p1 = // A: 1234 - C: 5454
    p2 = p1; // p2 = // A: 9658 - C: 5454

    *p2 = 30;

    The last line loads the contents of p2 (the address of num), and then assigns content 30 to it (num).

    Sorry if I don't explain very well, but that's basically it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM