Thread: passing pointer argument

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    Question passing pointer argument

    hey, it is me again. I have another question in my mind, ok, this is the code:
    Code:
    #include<stdio.h>
    #define max 15
    void average5(double a[]);
    int main(void)
    {
        
        static double rain[3][5]={
        {1,1,1,1,1},
        {2,2,2,2,2},
        {3,3,3,3,3}};
        average5(rain);
        
        system("pause");
        return 0;
    }
    void average5(double a[])
    {
         int a1;
         double subtot;
         for(a1=0;a1<15;a1++)
          {
           if(a1%5==0)
            subtot=0;
           subtot+=*(a+a1);
           if(a1%5==4)
            printf("the 5 average is %.1lf ",subtot/5);
          
          }
    }
    I pass the array name "rain" to average5() which takes a double pointer argument. The compiler only told me this is an imcompatible pointer type, but the program itself still can run and showing the correct result. any idea about this?

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What exactly does the compiler say ?

    Also, I don't think your program does exactly what you expect it to. But then again, I don't quite know what you expect it to do either...

    Oh, one last thing, you can change that system("PAUSE") for a getchar() and maintain portability.
    Last edited by Happy_Reaper; 01-08-2007 at 07:20 AM.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    rain has a type double[][5]
    and it is different from the double[]

    if you want to read two-dimentional array as one-dimentional pass pointer to the first element:
    &rain[0][0]

    and make a function prototype as:
    Code:
    void average5(double* a)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,533
    You will want to pass in a size for the second subscript. The compiler can't guess at what size the second (or subsequent arrays, if you had more than two) array is. As it is, you have only passed a pointer to the first element of the array a, but each element of a also contains a vector of 5 elements.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    Exactly the same as your previous question.
    http://cboard.cprogramming.com/showthread.php?t=87215
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    21
    I compiled this code, and I only got an warning saying passed the imcompatible pointer type, but the program works!

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    Quote Originally Posted by timhxf
    I compiled this code, and I only got an warning saying passed the imcompatible pointer type, but the program works!
    So what's the question? Why your wrong code works on your compiler? Just luck... Don't you expect that you'll be always so lucky with your errors?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    21
    thanks all. I know that my code is wrong. I just wondered why it worked at my machine. I am a new comer for C, do not know how the compiler works. So, just a bit curious

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I just wanted to weigh in on this issue here.
    Quote Originally Posted by timhxf
    thanks all. I know that my code is wrong. I just wondered why it worked at my machine. I am a new comer for C, do not know how the compiler works. So, just a bit curious
    Dense arrays can be allocated in a number of ways, but what the compiler should do in this case

    unsigned int x = 2, y = 3;
    double rain[MAXROWS][MAXCOLS];

    is simply allocate a huge block of memory of MAXROWS * MAXCOLS doubles.

    What changes most is the way that the array is indexed. One sane array implementation may ensure that if you laid out the array visually--maybe on a graph--you would have to move x rows and y columns to get to the right space. So, when the parser reaches a statement like

    rain[x][y] = 42;

    The address which is dereferenced gets computed like so x * MAXROWS + y. Pointer implementations must be able to do this sort of arithematic, given the size of the object in bytes to which they point and that all objects in the array are stored immediately after one another, if you step enough bytes forward or backward, you can reach an address which stores something valid.

    What is remarkable about your code is that you pass a simple double pointer and "walk" through the array, but, pedantically
    Code:
    int average5( double *a1 );
    ...
    average5(rain);  /* this points to the whole array; much bigger in bytes than a double. */
    Which is why a lot of people yelled at you about changing your prototype. While passing a pointer to such a huge space definitely ensures that you could access the whole array, you may not dereference it in the right places. Undefined behavior is the result.
    Last edited by whiteflags; 01-08-2007 at 10:56 AM. Reason: some rewording...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Server Help
    By lautarox in forum C Programming
    Replies: 146
    Last Post: 09-24-2008, 06:32 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM