Thread: confused with declrartions...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    confused with declrartions...

    I have following code please see the declarations of array

    Code:
    int main()
    {
    
    //How is this different from the below declarations
     int *x[10];
    
    //Another declaration
     int ( *x)[10];
    
    
     return 0;
    }
    What would be size of each declarations?

    Does this declaration int ( *x)[10] and int *x[10] mean array containing pointers.
    Shouldn't the size will be sizeof(int)*10 = 40.

    Thanks in advance

  2. #2
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    These are two kinds of declaration for an array of integers:
    Code:
    int x[10];    /* allocated at compile time, each position has the size of an int */
    
    int *x;        /* it's the same with the difference that the memory allocated at compile time is just for a pointer,
                        you will need to allocate it dinamically with malloc */
    while these are a bit different
    Code:
    int *x[10]   /* at compile time, sizeof(int*)*10 is allocated, thus is an array of pointers to integers */
    
    int **x;      /* as above with tthe difference that the memory allocated at compile time is just for a pointer
                       you will need to allocate it dinamically */
    the advantage of allocate arrays dinamically is that you can manage their size at run time.

    hope that help.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks for ur time..

    Quote Originally Posted by flexo87 View Post
    These are two kinds of declaration for an array of integers:
    Code:
    int **x;      /* as above with tthe difference that the memory allocated at compile time is just for a pointer
                       you will need to allocate it dinamically */
    So your mean

    int **x is same as int (*x)[10].........Can u explain this?


    What would be the size that will be allocated?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sanddune008
    What would be size of each declarations?
    You can easily find that out yourself by writing a program.

    Quote Originally Posted by sanddune008
    Does this declaration int ( *x)[10] and int *x[10] mean array containing pointers.
    Shouldn't the size will be sizeof(int)*10 = 40.
    This declares an array, named x, of 10 pointers to int:
    Code:
    int *x[10];
    Consequently, its size would be sizeof(int*) * 10.

    This declares a pointer, named x, to an array of 10 pointers to int:
    Code:
    int (*x)[10];
    Consequently, its size would be sizeof(int (*)[10]).

    Quote Originally Posted by flexo87
    These are two kinds of declaration for an array of integers:
    That is inaccurate since the latter declares a pointer, not an array.
    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

  5. #5
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    It is not the same, with the first (int** x) you are telling the compiler to not allocate memory for the array (you will do it in the program).
    with the second (int *x[10], i thnk brackets are unnecessary) the compiler will allocate the space in order to contain 10 pointers to an integer, so the memory you will need to allocate is to contain the integers, while in the first case you allocate memory to contain pointers, and then integers.
    Code:
    int **x;   /* allocate sizeof(int**), 4 bytes */
    
    int *x[10];   /* allocate sizeof(int*)*10, 40 bytes */

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks...

    Quote Originally Posted by laserlight View Post
    This declares a pointer, named x, to an array of 10 pointers to int:
    Code:
    int (*x)[10];
    How different is it from declaring

    so
    Code:
    int *x and int (*x)[10]
    is one and the same?

    What would be the advantage with the declaration
    Code:
    int (*x)[10]
    ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sanddune008
    How different is it from declaring
    (...)
    is one and the same?
    The former declares a pointer to an int, the latter declares a pointer to an array of 10 ints. It is like asking how is the declaration of a pointer to an int different from the declaration of a pointer to a double.

    Quote Originally Posted by sanddune008
    What would be the advantage with the declaration
    Well, I do not know if you can call this an "advantage", but you would be able to point to an array, as opposed to pointing to an element in the array.

    Quote Originally Posted by flexo87
    with the second (int *x[10], i thnk brackets are unnecessary)
    The parentheses are necessary. Without them, you would be declaring an array of 10 pointers to int.
    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

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    I have following code please see the declarations of array

    Code:
    int main()
    {
    
    //How is this different from the below declarations
     int *x[10];
    
    //Another declaration
     int ( *x)[10];
    
    
     return 0;
    }
    What would be size of each declarations?

    Does this declaration int ( *x)[10] and int *x[10] mean array containing pointers.
    Shouldn't the size will be sizeof(int)*10 = 40.

    Thanks in advance
    int *x[10]
    It's an array of pointers, the pointers are pointing to int type. This means all the elements of the array are nothing but addresses.

    int (*x)[10]
    Here x is a poniter to an array having 10 integers.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 05:41 PM
  5. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM