| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hellow,
I am new to the C enviroment. I need to create a row matrix (array) on the fly. That is when I pass on the number of columns n into the program through argv[], I should be able to obtain the row matrix consists of n columns like [x1 x2 x3 x4 ------xn] as an output file. I would like to keep these as variables in terms of x, instead of actual numbers. So these x's are in fact integers. I would manipulate this matrix later on by finding its transpose and multiplying with another matrix to get equation in terms of x1, x2 etc. Can any of you throw some idea!!! |
|
#2
|
|||
|
|||
|
Once you convert the argv value into an integer you can use it to create a dynamic array using dynamic memory allocation functions such as malloc and calloc.
For example if you have the number of items in x - int *array = calloc (x, sizeof (int)); -KM- |
|
#3
|
|||
|
|||
|
Hello,
actually a more correct way of doing it is like this: int *array = NULL; if ( (array = calloc (x, sizeof (int))) == NULL) { /* allocation failed! */ exit(1); } and in c++ its done like this: if ( (array = new int[x]) == NULL) { /* allocation failed! */ exit(1); } Arash Partow __________________________________________________ Be one who knows what they don't know, Instead of being one who knows not what they don't know, Thinking they know everything about all things. http://www.partow.net |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Creating an Array on the fly |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|