Thread: Compound literals

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Compound literals

    Code:
     #include <stdio.h>
    int sum_array(int * , int );
    
    int main(void)
    {	
    	
    int total = sum_array( (int []) { 1 , 2 , 3 , 4 } , 4 );
    
    printf(" Total is: %d" , total);
    
    return 0;
    }
    int sum_array( int arr[] , int n)
    {
    	int i , sum=0 ;
    	
    	for(i=0; i<n; i++)
    	sum += arr[i];
    	
    	return sum;
    }
    We create an unnamed array "on the fly" that it means array has no permament storage during the program execution?

    Thank you in advance

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    As far as your program is concerned, the array does not exist in any statement of main() after the call of sum_array(), if that's what you mean.

    As to how (or if) that array will exist in memory for as long as your program is running, that is compiler-dependent. That array is essentially a compile-time constant, and it (or the population of it) has to be represented somehow in executable code.

    It is not a technique you should use if your goal is to minimise memory usage - as measured, for example, by a host operating system - of your program at some point after calling sum_array().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by grumpy View Post
    As far as your program is concerned, the array does not exist in any statement of main() after the call of sum_array(), if that's what you mean.

    As to how (or if) that array will exist in memory for as long as your program is running, that is compiler-dependent. That array is essentially a compile-time constant, and it (or the population of it) has to be represented somehow in executable code.

    It is not a technique you should use if your goal is to minimise memory usage - as measured, for example, by a host operating system - of your program at some point after calling sum_array().
    Ok. I was wondering what is the meaning of creation an array "on the fly" . That was my basic doubt.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Sorry but I am little confused because i read again the definition of a compound literal :

    In general a compound literal consists of a type name within parentheses , followed by a set of values enclosed by braces.

    In the case of arrays what is the type name?

    Code:
     int total = sum_array( (int []) { 1 , 2 , 3 , 4 } , 4 );
    int is a data type and [] is an operator.... type name is the keyword? the array is unnamed.

    How the compiler evaluate this statement? from right to left? because it writes tha the set of values follow the type name within parentheses.

    Thank you in advance
    Last edited by Mr.Lnx; 05-23-2013 at 03:43 PM.

  5. #5
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    It's an array of integers, it's pretty clear regarding the type (perhaps you are asking something else).

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by migf1 View Post
    It's an array of integers, it's pretty clear regarding the type (perhaps you are asking something else).

    I don't know maybe is a silly question... but I can't understand what we mean when we say "type name" in a compound literal using structures is more clear to me.... (struct tag_name) yes that is a type name.... but here ? I don't see any name in (int [])

    sorry if the question is silly.

  7. #7
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    I didn't imply your question was silly, only that perhaps I didn't understand it.

    So to make it clear, your question is "why int[] specifies a type"? If so, what would you think as a better alternative for describing the type of an array of integers?

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by migf1 View Post
    I didn't imply your question was silly, only that perhaps I didn't understand it.

    So to make it clear, your question is "why int[] specifies a type"? If so, what would you think as a better alternative for describing the type of an array of integers?
    No. I am just wondering what does it means with "type name" I know that the type is int [] the name of type where is? maybe my question is bad or weird.... I don't want to tire you.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Arrays are defined using
    Code:
       int x[] = {initialiser list};
       int y[2];
    but every type has a name. It just so happens that the name of the type is int [dimension]. Since, in many cases the dimension doesn't matter to the compiler, the shorthand for the type is int[].

    Depending on your viewpoint, I suppose that can be viewed as a syntactic peculiarity of the language. It is not the only one.....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Mr.Lnx View Post
    No. I am just wondering what does it means with "type name"
    "type name" is defined in the C standard (section 6.7.6 in C99, 6.7.7 in C11).

    Quote Originally Posted by Mr.Lnx View Post
    I know that the type is int [] the name of type where is?
    The type name of the type int [] is int [] :-).

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by C11 standard
    In several contexts, it is necessary to specify a type. This is accomplished using a type
    name, which is syntactically a declaration for a function or an object of that type that
    omits the identifier.
    So
    Code:
    int i; // type name "int"
    int arr[5]; // type name "int[5]"
    void func(int i, int j); //type name "void (int,int)"
    So yes, the int[] between the brackets is the type name. It's not a stupid question -- you might have expected it to be "array" or something. But nope, it's easier than that.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Thank you very much guys. Your explanations is what I wanted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compound Statement
    By vicky2 in forum C Programming
    Replies: 6
    Last Post: 12-06-2012, 06:05 AM
  2. Compound Interest?
    By jbone0881 in forum C Programming
    Replies: 6
    Last Post: 02-25-2011, 11:07 PM
  3. need help with compound interest
    By JoelearningC in forum C Programming
    Replies: 4
    Last Post: 03-09-2008, 10:32 AM
  4. compound interest, need help
    By shaitan_superma in forum C Programming
    Replies: 8
    Last Post: 09-01-2007, 11:50 PM
  5. compound interest
    By bliznags in forum C Programming
    Replies: 11
    Last Post: 03-20-2005, 01:46 AM