Thread: help with confusing declarations please

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    help with confusing declarations please

    in k&r c the following declaration is given to be pointer to array[13] of int
    Code:
    int (*daytab)[13]
    on similar lines i initialize a pointer to array[2]of int in my program as follows
    Code:
    int c[2];
    int (*d)[2];
    d=c;
    when compiling this code i get a warning as follows
    Code:
    sizeof.c: In function ‘main’:
    sizeof.c:7: warning: assignment from incompatible pointer type
    what is generating this warning and how to get rid of it? also are there any good resources for understanding complicated declarations in c?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually you could just use
    Code:
    int c[20];
    int *d;
    d = c;
    There's no need to make it any more complex than it has to be.

    Also note that depending which edition of K&R you have and the compiler you're working with, some things may not be entirely compatible between the two.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    re

    thank you for your reply. what you sad can be done. but i am trying to understand what that declaration means.

    another important observation. when i do the following initialisation the the warning disappears

    Code:
    int c[2];
    int (*h)[2];
    h=&c;
    please help in understanding this declaration.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    int (*daytab)[13];
    int days[2][13] ={  {0 , 1,2,3,4,5,6,7,8,9,10,11,12 },
                                {12,11,10,9,8,7,6,5,4,3,2,1,0 } };
    daytab = days;   // points to first element &days[0] { 0,1,2,....}
    daytab + 1        // points to next element( array {12,11,10...})
    daytab is pointer to array[of 13 int].

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by csepraveenkumar View Post
    thank you for your reply. what you sad can be done. but i am trying to understand what that declaration means.

    another important observation. when i do the following initialisation the the warning disappears

    Code:
    int c[2];
    int (*h)[2];
    h=&c;
    please help in understanding this declaration.
    int c[2] has two elements, and is an array of int's. (it helps to read them from right to left, instead of left to right).

    int (*d)[2] has two elements, but is an array of two pointers to int. So they can't be set equal to each other - they're different beasties. Edit: See the correction below, this one I got wrong.

    But you can set the pointer to the address of the base of the int array.
    Last edited by Adak; 03-19-2011 at 07:30 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > int (*d)[2] has two elements, but is an array of two pointers to int.
    Pointer to an array of 2 ints.

    int *d[2] is an array of 2 pointers to int.

    The () are important here.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thank you, Salem. The (*d)[2] declaration is one I've never used.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You've never passed a 2D array to a function before?

    As a function parameter, you can write
    Code:
    void foo ( int arr[2][2] );
    void foo ( int arr[][2] );
    void foo ( int (*arr)[2] );
    But you can only use the last form to declare a variable with the same type.
    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.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    1
    Quote Originally Posted by csepraveenkumar View Post
    thank you for your reply. what you sad can be done. but i am trying to understand what that declaration means.

    another important observation. when i do the following initialisation the the warning disappears

    Code:
    int c[2];
    int (*h)[2];
    h=&c;
    please help in understanding this declaration.

    Hello Praveen

    Code:
     int (*daytab)[4];
    This is a pointer to an array of 4 integers.

    Now if you have the following :

    Code:
    int c[2];
    int (*d)[2];
    d=c;

    Now, the name of an array always gives us the address of the first element of the array.
    So the 'c' here is the Integer Pointer Ito the first int in c. On the other hand 'd' is a Pointer to an Array of 2 Integers. It is because of this reason that you get a Warning.

    On Incrementing an Integer pointer, you get a pointer to the next int, however on incrementing the pointer to array, you jump over the entire array and point to the next array of the same dimension.

    Now coming to this code:

    Code:
    int c[2];
    int (*h)[2];
    h=&c;
    As we all know that the name of the array represents the pointer to the first element of the Array. However this rule has some exceptions:

    1. When you use sizeof(arr) , then it does not return the size of the pointer, instead it returns the size of the array. In your example, the answer will be 8 (because c is an array of 2 integers) and not 4(which is the size of an integer pointer)

    2. Similarly when the name of the array is used with the '&' Operator, it converts the pointer to the entire array.

    Hence when you use
    Code:
    h=&c
    Here, &c is now the pointer to the array of 2 ints and is of the same type as h and thus you do not get the warning.

    The code can also be written as
    Code:
    h= (int (*)[2])c;
    Here we have typecasted c into a pointer to an array of 2 ints.

    Hope this was helpful.
    Last edited by Abhinav Arora; 07-07-2011 at 09:39 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Abhinav Arora, that is a good explanation. However, the discussion in this thread has since died out, and there was no errors that your contribution corrected.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In .h files, should function declarations be 'extern' ?
    By Richardcavell in forum C Programming
    Replies: 2
    Last Post: 02-28-2011, 07:56 AM
  2. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 01:29 AM
  3. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  4. mixing code with declarations
    By robwhit in forum C Programming
    Replies: 12
    Last Post: 01-31-2008, 12:55 AM
  5. help on declarations
    By robasc in forum C Programming
    Replies: 9
    Last Post: 03-05-2005, 01:50 PM