Thread: Array own data type?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Array own data type?

    When you declare an array
    Code:
    int myarray[100];
    what type does myarray get? "Array of hundred ints"?
    Is that why you get sizeof(myarray) == 400 while you would get sizeof(myarrayhead) = 4 if you declare
    Code:
    int *myarrayhead = malloc(100 * sizeof(int));
    ?
    So if you send myarray as an argument, the whole array will be copied and sent to the function, while if you send myarrayhead as an argument to another function, just the pointer will be copied and sent?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by TriKri
    When you declare an array
    Code:
    int myarray[100];
    what type does myarray get? "Array of hundred ints"?
    Yup.
    Quote Originally Posted by TriKri
    Is that why you get sizeof(myarray) == 400 while you would get sizeof(myarrayhead) = 4 if you declare
    Code:
    int *myarrayhead = malloc(100 * sizeof(int));
    ?
    The array is an array, the pointer is a pointer. Taking the sizeof an array gives you the size of the array; taking the sizeof a pointer gives you the size of the pointer.
    Quote Originally Posted by TriKri
    So if you send myarray as an argument, the whole array will be copied and sent to the function, while if you send myarrayhead as an argument to another function, just the pointer will be copied and sent?
    No, the whole array does not get copied. A pointer to the array is passed.
    http://c-faq.com/aryptr/index.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by TriKri
    So if you send myarray as an argument, the whole array will be copied and sent to the function, while if you send myarrayhead as an argument to another function, just the pointer will be copied and sent?
    No. Arrays are kindof passed by reference. Only a pointer to the first element is passed to functions.
    That means inside the function there is no difference if you pass myarray or myarrayhead.
    Kurt
    EDIT: too late again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM