Array own data type?

This is a discussion on Array own data type? within the C Programming forums, part of the General Programming Boards category; When you declare an array Code: int myarray[100]; what type does myarray get? "Array of hundred ints"? Is that why ...

  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,006
    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
    ZuK
    ZuK is offline
    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, 06:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM

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