Arrays in Function Arguments

This is a discussion on Arrays in Function Arguments within the C Programming forums, part of the General Programming Boards category; Hello, I'm wondering if I can list contents in an array within a function argument? For instance: Code: RetreiveArrayInput({2, 1, ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    9

    Arrays in Function Arguments

    Hello,

    I'm wondering if I can list contents in an array within a function argument?

    For instance:

    Code:
    RetreiveArrayInput({2, 1, 3, 0xFF});
    I have devised the following code to experiment with, but unfortunately I don't exactly know what I'm doing.

    Code:
    #include <stdio.h>
    
    //** Defines ************************************************************
    #define		TERMINATOR		0xFF
    
    //** Globals ************************************************************
    unsigned int ArrayOfMyLife[] = {0x01, 0x02, 0x04, 0x08};
    
    void RetreiveArrayInput(unsigned int *OrderOfValues)
    {
         unsigned int i;
         unsigned int index;
         
         while((index = *OrderOfValues++) != TERMINATOR)
         {
    			for(i = ArrayOfMyLife[index]; i > 0; i--)
    			{
    				printf("Selection: %s\n", i);
    			}
          }
    }
    
    int main()
    {
    	unsigned int someArray[5];
    	RetreiveArrayInput({2, 1, 3, 0xFF});
    	
    	system("PAUSE");
    }
    Just in case, I'm using Dev-C++.

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Compound Literal
    In C99, you can use compound literal.
    Code:
    RetreiveArrayInput(( unsigned int[]) { 2,1,3, TERMINATOR } ) ;
    Which is the same as in C89
    Code:
    unsigned int __some_anonymous_array[] = { 2,1,3,TERMINATOR };
    RetreiveArrayInput( __some_anonymous_array );

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Ok, that makes sense. I have another question as well. If I have the following code:
    Code:
    char a[] = {“Hello”};
    char *p;
    
    p = a;		// Does p = “Hello” also?
    In other words, it points to the entire quantity of the array? Can I do this?
    Code:
    printf("%s", p[0]);      // Should this equal 'H' ?
    Last edited by ElectroNerd; 04-26-2011 at 11:12 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Easy way to find out....

    Code:
    char a[] = {“Hello”};   // These need to be char not int.
    char *p;
    
    p = a;		// Does p = “Hello” also?
    
    puts(p);           // well, lets have a look...

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Aha, it does. But doing a p[0] didn't work. I expect I would have to do this:

    Code:
    p = &a[0];     // This is 'H'

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,634
    Code:
    printf("%s", p[0]);      // Should this equal 'H' ?
    You want %c for an individual character. At this point, p points the the start of a, and refers to "Hello". You can index a pointer just like an array, so p[0] == a[0] == 'H', an individual character.

    Code:
    p = &a[0]
    This is the same as p = a, since the name of an array serves as a pointer to it's first element.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ElectroNerd View Post
    Aha, it does. But doing a p[0] didn't work. I expect I would have to do this:

    Code:
    p = &a[0];     // This is 'H'
    Well, one way to find out.... TRY IT!

    Really, it harms nothing to try little experiments like this...

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    9
    Really? I imagined my computer blowing up!

    I like the Alt-F4 Windows Aptitude Test; what is crazy is that I learned that function today at my work (some engineer did it, so I inquired how).

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The OS will not let a simple program error blow it up. You may get seg-faults or access violation errors, or your program may crash in some very inglorious way... but except in extreme situations (most often as the result of deliberacy) one blown-out program will not crash a system.

    IOW... what do you think error messages are for? That's your system protecting itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-02-2007, 04:22 PM
  2. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  3. Arrays as function arguments!
    By strokebow in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 02:32 PM
  4. dynamic multidimensional arrays as function arguments
    By magda_k in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 04:00 PM
  5. Arrays..and arguments
    By fanaonc in forum C Programming
    Replies: 2
    Last Post: 10-16-2001, 05:58 PM

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