0

I want after I do some calculation create an array in C would u please help me?

int len_arr = (sizeof inputs)/(sizeof inputs[0]);
int half_arr = len_arr / 2;
if(len_arr%2 != 0)
    half_arr++;

int m[half_arr][half_arr];
0

3 Answers 3

4

I'm assuming you wish to create a matrix with row/column size of half_arr.

int i;
int len_arr = (sizeof inputs)/(sizeof inputs[0]); 
int half_arr = len_arr / 2; 
if(len_arr%2 != 0)     
    half_arr++;  

int **m = malloc(half_arr * sizeof(int));

for (i = 0; i < half_arr; i++)
    *m = malloc(half_arr * sizeof(int));

You should then be able to access m using m[row][column] or m[column][row].

Sign up to request clarification or add additional context in comments.

7 Comments

then he has to do another loop like that freeing all that memory
@BlackBear: Yes, he does. If he wishes to use m[][] syntax though it is needed.
int m** that a new type? :)
Compiling... main.c C:\OBA_VTS\SVD_Enhancement\main.c(25) : error C2143: syntax error : missing ';' before 'type' C:\OBA_VTS\SVD_Enhancement\main.c(28) : error C2065: 'm' : undeclared identifier C:\OBA_VTS\SVD_Enhancement\main.c(28) : error C2100: illegal indirection C:\OBA_VTS\SVD_Enhancement\main.c(28) : warning C4013: 'malloc' undefined; assuming extern returning int C:\OBA_VTS\SVD_Enhancement\main.c(46) : error C2109: subscript requires array or pointer type
@James: no, it's not - the C language supports variable-length arrays; if rima uses MSVC as a compiler (which basically only supports C90), I strongly suggest using C++ instead or switching to MinGW
|
2

You need to use malloc to allocate your 2-dimensional array.

Look at this LINK for a good explanation.


EDIT:

Starting from the code at the given link, I've written a piece of code that should work.

Some differences from James' code:

  • declaration of m at the top of the code block
  • conversion from the return type of malloc (void *) to the correct one
  • memory deallocation at the end
  • it's a complete code, that must work :P

#include <stdlib.h>

int main ()
{

    // just an example of inputs...
    int inputs[3][7];

    // your code
    int i;
    int **m;
    int len_arr = (sizeof inputs)/(sizeof inputs[0]); 
    int half_arr = len_arr / 2; 
    if(len_arr%2 != 0)     
        half_arr++;  

    // allocation of the array
    m = (int **)malloc(half_arr * sizeof(int *));
    for(i = 0; i < half_arr; i++)
        m[i] = (int *)malloc(half_arr * sizeof(int));

    // use the array as you like...
    // ...

    // let's release the allocated memory properly
    for(i = 0; i < half_arr; i++)
        free(m[i]);
    free(m);

}

2 Comments

Oh, you accepted my answer before my edit :P Anyway, have a look at it if you want ;)
yes, because your link was so interesting for me & it was help me in a minute
-1

Your code is valid C99. You might want to use a compiler which supports newer revisions of the C language.

On Windows, I'd go with the MinGW edition of gcc. If you're stuck with MSVC, I suggest using C++ instead: the standard container types will make your life less painful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.