passing pointers

Discussion in 'C Programming' started by PengYu.UT@gmail.com, Jun 21, 2005.

  1. Guest

    Hi,

    I have the following two functions. However, the function printa gives
    me a warning. If I delete "const" from its definition, I will not get
    the warning. I'm wondering if there is anything wrong with compiler,
    because the function printb works fine.

    Best wishes,
    Peng

    #include <stdio.h>

    void printa(const double (*a)[2]){//warning
    //void printa(double (*a)[2]){//no warning
    printf("Hello!\n");
    }

    void printb(const double *b){
    printf("Hello!\n");
    }

    int main(int argc, char *argv[]){
    double a[10][2];
    double b[10];
    printa(a);
    printb(b);

    return 0;
    }
     
    , Jun 21, 2005
    #1
    1. Advertising

  2. On Tue, 21 Jun 2005 14:05:44 -0700, wrote:

    > Hi,
    >
    > I have the following two functions. However, the function printa gives
    > me a warning. If I delete "const" from its definition, I will not get
    > the warning. I'm wondering if there is anything wrong with compiler,
    > because the function printb works fine.


    The compiler is correct, this is a wart in C's type system. An array can't
    be specified as const, only its elements.

    > Best wishes,
    > Peng
    >
    > #include <stdio.h>
    >
    > void printa(const double (*a)[2]){//warning //void printa(double
    > (*a)[2]){//no warning
    > printf("Hello!\n");
    > }
    > }
    > void printb(const double *b){
    > printf("Hello!\n");
    > }
    > }
    > int main(int argc, char *argv[]){
    > double a[10][2];
    > double b[10];
    > printa(a);


    Here you are trying to pass a pointer to an array of 2 doubles to a
    function requiring a pointer to an array of 2 const doubles. C allows
    conversion from a pointer to TYPE to a pointer to const TYPE. In this case
    that would have to be to a pointer to a const array of double, which is
    not possible in C. Conversion to a pointer to an array of const double
    doesn't fit the rule so is invalid. This is a case where you just have to
    forget about const or put a cast in.

    > printb(b);


    This is fine because you are converting from a pointer to double to a
    pointer to const double which fits the rule.

    Lawrence
     
    Lawrence Kirby, Jun 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    669
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    783
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    478
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    708
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
Loading...

Share This Page