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
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
Are you trying to say that Store should take a variable number of arguments?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Question is bit ambiguous. Are you trying to pass the values as arguments to function and that function should store that in array?
Here is the solution:
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.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; }
Hope this solution helped you.
Cheers,
Timon
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:Though I've never paid much attention to doing it that way.Code:void foo( int[] ); ... foo( { 1, 2, 3, 4, 5, } );
Quzah.
Hope is the first step on the road to disappointment.
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.
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.
Output: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; }
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.Code:Index 0: 10 Index 1: 20 Index 2: 30
Last edited by \007; 07-07-2011 at 06:15 AM.
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
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.Originally Posted by zidanelam
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.