Thread: Passing the variables into array by using functions C

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Passing the variables into array by using functions C

    Hey,

    I'm trying to create a function like:

    Store(20,30,40,50,60);

    the data will store in this form:

    buffer[0]=20
    buffer[1]=30
    buffer[2]=40
    buffer[3]=50
    buffer[4]=60

    Please guide me .. Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you trying to say that Store should take a variable number of arguments?
    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

  3. #3
    Registered User Timon's Avatar
    Join Date
    Jul 2011
    Location
    Bangalore
    Posts
    4
    Question is bit ambiguous. Are you trying to pass the values as arguments to function and that function should store that in array?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Quote Originally Posted by Timon View Post
    Question is bit ambiguous. Are you trying to pass the values as arguments to function and that function should store that in array?
    Yes...Any idea? I prefer using pointer method

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zidanelam View Post
    Hey,

    I'm trying to create a function like:

    Store(20,30,40,50,60);

    the data will store in this form:

    buffer[0]=20
    buffer[1]=30
    buffer[2]=40
    buffer[3]=50
    buffer[4]=60

    Please guide me .. Thank you

    The C Book — Functions

  6. #6
    Registered User Timon's Avatar
    Join Date
    Jul 2011
    Location
    Bangalore
    Posts
    4
    Here is the solution:

    Code:
    #include<stdio.h>
    
    void printdata(int *array, int length)
    {
    	for(int i=0;i<length;i++)
    	{
    		printf("%d\n",array[i]);
    	}
    }
    
    int main() {
    	
    	int array[]= { 20,30,40,50,60};	
    	int len=sizeof(array)/sizeof(int);
    	printdata(array,len);
    	return 0;
    }
    But you may be in need of the length of the array. If the array size is already known pass the value directly or calculate the size of the array as above.
    Hope this solution helped you.

    Cheers,
    Timon

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need the length of the array if you have some value set aside to indicate it is done. Such as \0 for character arrays representing strings. Also, I believe in C99 you can do:
    Code:
    void foo( int[] );
    ...
    foo( { 1, 2, 3, 4, 5, } );
    Though I've never paid much attention to doing it that way.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I would suggest using an expanding array based stack implementation. You can drop the pop function if you don't need it. Simply use a push function with a variable length of integers as arguments. Furthermore, you can access the array like you would any other array.
    Last edited by \007; 07-07-2011 at 05:58 AM.

  9. #9
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    This was quick and dirty, but I hope it makes sense. You could expand on something like this.. I think it works like you described.

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* malloc() */
    
    #define DEFAULT_SIZE 5
    
    typedef struct IntStack IntStack;
    struct IntStack{
      int counter;
      int *container;
      int maxSize;
    };
    
    /***
    * newIntStack - creates and inits a new IntStack 
    **/
    IntStack* newIntStack(void)
    {
      IntStack* stack = malloc(sizeof(IntStack));
      stack->container = malloc(sizeof(int) * DEFAULT_SIZE);
      stack->maxSize = DEFAULT_SIZE;
      stack->counter = 0;
    
      /* NYI: error checks */
      return stack;
    }
    
    /***
    * freeIntStack - frees all memory associated with an IntStack 
    * @stack - the target stack to free
    **/
    void freeIntStack(IntStack* stack)
    {
      free(stack->container);
      free(stack);
    }
    
    /***
    * push - push a value into the stack
    * @value - value to push
    **/
    int push(int value, IntStack* stack)
    {
      /* NYI: check for full container, if it's full expand array and
                don't forget to increase the new maxSize, 2x should do. */
    
      stack->container[stack->counter] = value;
      stack->counter++;
      return 0; 
    }
    
    /***
    * printStack - prints the contents of a stack
    * @stack - the target stack we wish to print
    **/
    void printStack(IntStack* stack)
    {
      int i;
      for(i = 0; i < stack->counter; i++)
        printf("Index %d: %d\n", i, stack->container[i]);
    }
    
    int main(void)
    {
      IntStack* stack = newIntStack();
      
      push(10, stack);
      push(20, stack);
      push(30, stack);
    
      printStack(stack);
    
      freeIntStack(stack);
      return 0;
    }
    Output:

    Code:
    Index 0: 10
    Index 1: 20
    Index 2: 30
    You could also dumb this down and use a basic array, and you don't need to use a struct etc. However, this gives you quite a bit of power.
    Last edited by \007; 07-07-2011 at 06:15 AM.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    And thanks for the replies and efforts.
    007, actually i want to program into microcontroller. your code might be too long for microcontroller to process.

    My application is I would like to send data out to another device in array/string, but the data size and length is not fixed everytime.

    Eg.

    Let's say:

    I write:

    Send(20,30,40,50,60)

    the data is stored in an array size of 5
    buffer[0]=20
    buffer[1]=30
    buffer[2]=40
    buffer[3]=50
    buffer[4]=60

    Then i write:
    Send (20,30,40)

    the data stored in an array size of 3
    buffer[0]=20
    buffer[1]=30
    buffer[2]=40

    Can this possibly done? quzah's idea came close. But i get Build FAILED

    Quote Originally Posted by quzah View Post
    You don't need the length of the array if you have some value set aside to indicate it is done. Such as \0 for character arrays representing strings. Also, I believe in C99 you can do:
    Code:
    void foo( int[] );
    ...
    foo( { 1, 2, 3, 4, 5, } );
    Though I've never paid much attention to doing it that way.


    Quzah.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zidanelam
    Can this possibly done?
    Yes. Read the tutorial on variable length argument lists. That said, I suspect that what you envision now may not actually be what you want because like this the data (or at least the count thereof) will be fixed at compile time.
    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

  12. #12
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I would suggest a combination of ideas.

    I would use the variable length argument list as linked to above.

    I would also use a method similar to how I coded but instead of building a whole stack you can make a kind of... 'disposable stack' ? Each push would result in a different size array.

    For instance ,

    send(1, 2, 3, 4, 5, 6);
    - malloc an array the size of the va_list (sizeof(int) * 6 in this case)
    - sets each argument into the array
    - sends the array to the controller or where ever you are sending.
    - free allocated array (dispose of it)
    - return

    This way each call could be a variable length, the array would be built and sent and gotten rid of. Sounds like what you are aiming for?

    I am not sure how performance critical this program is. The calls to malloc are pretty hefty and if you are sending a lot of data you may notice a performance hit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing variables between functions
    By Martin_T in forum C Programming
    Replies: 9
    Last Post: 11-23-2009, 03:26 AM
  2. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  3. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  4. Passing variables through functions and structures?
    By dizz in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2002, 10:18 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM