pointers and arrays: my compiler complains

This is a discussion on pointers and arrays: my compiler complains within the C Programming forums, part of the General Programming Boards category; Hi guys...as you can see I have a lot of questions about C... Today I was changing my functions and ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    50

    pointers and arrays: my compiler complains

    Hi guys...as you can see I have a lot of questions about C...
    Today I was changing my functions and I got complains from the compiler. This is the question: I have a function that takes one argument, a pointer, i.e.
    Code:
    void foo(double *wof)
    Now I want to use foo in main
    Code:
    int main()
    {
          double array[10];
          foo(&array);
    }
    my compiler says " warning: passing argument 1 of ‘foo' from incompatible pointer type", but the program is working. I cannot understand why because, as far as I know, passing &array means that I am pointing at the first place of array, i.e. array[0].

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    "array" itself is an array and, with that, a pointer. So array itself is of type "double*" (well, strictly speaking, they are slightly different, but an array can be used as a pointer).

    So you should simply use "array". Or, "&array[0]". While the two are identical, in some cases I find the latter more readable (although this would not be one of them).

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    50
    solved! Usually I compile with this
    gcc -std=gnu99 -shared ...
    but keeping the default standard
    gcc -shared ...
    the compiler does not complain anymore
    Actually I do not know what is the default value

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by EVOEx View Post
    "array" itself is an array and, with that, a pointer. So array itself is of type "double*" (well, strictly speaking, they are slightly different, but an array can be used as a pointer).

    So you should simply use "array". Or, "&array[0]". While the two are identical, in some cases I find the latter more readable (although this would not be one of them).
    mmmhh, actually if I put array the program is working (with std=gnu99)...well I have discovered something new.
    Thanks a lot!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    50
    oh, now is very clear why this "trick" is not working with scalars :-) (but actually they also have a memory address...). Thanks a lot! Now I can save a lot of lines in my codes

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
      foo(&array);
    It's wrong even if it's working.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by Bayint Naung View Post
    Code:
      foo(&array);
    It's wrong even if it's working.
    indeed..I have changed it with foo(array) :-)

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by violatro View Post
    I cannot understand why because, as far as I know, passing &array means that I am pointing at the first place of array, i.e. array[0].
    Perhaps if you print out the locations of array and &array it might become clear

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by itCbitC View Post
    Perhaps if you print out the locations of array and &array it might become clear
    very wise suggestion!
    Code:
    int main()
    {
    
    	int array[2], scalar;
    	printf("%p %p\n",&array,&array[0]); 
    	printf("%p %p\n",array,&array);
    }
    thanks! now it is also clear why with scalars it is not working :-)

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Quote Originally Posted by itCbitC View Post
    Perhaps if you print out the locations of array and &array it might become clear
    They will probably be the same...
    array decays to double*.
    However, &array is of type (double*)[10].
    Different types. Strictly speaking, the only difference is when you use the ++ operator. In the first case, it will step ahead one element. In the second case, it will jump ahead one array (ie 10 elements in this case).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    50
    indeed, they are (at least after running the sample program above).
    This is a very good remark, to keep in mind (at least for me)
    Thanks!
    ai confini della conoscenza

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    They will probably be the same...
    array decays to double*.
    However, &array is of type (double*)[10].
    Different types. Strictly speaking, the only difference is when you use the ++ operator. In the first case, it will step ahead one element. In the second case, it will jump ahead one array (ie 10 elements in this case).
    That would be correct in main(), but since &array is passed to foo() that takes a double* argument, it gets cast to double*.
    Now when the increment / decrement operators are used in foo() the step size == sizeof(double) due to the implicit cast.

    array == pointer to double
    &array == pointer to an array of 10 doubles
    foo(&array) --> foo((double*) &array)

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Quote Originally Posted by itCbitC View Post
    foo(&array) --> foo((double*) &array)[/FONT]
    This may be a warning in C, but as noted, this is an error, because (double*)[10] != double*.
    If you get this kind of warnings, you are doing something wrong. Nevermind the implicit conversion. Don't rely on it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 06:28 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21