define a varaible in C with flexible length

• Question

• Hi.

I want to define a variable in C with length flexible depending on the user choice. Suppose N is known, the question is :

1.  How can I write :
if N==3  ----->  double c[3]
else if N=4 ------> double c[5]
end

2. If N is defined by user through a function A=func(N,...) could we dfine ateh varaible dynamicaly at each function call ?

double c[N]

thanks

Thursday, May 28, 2009 11:31 AM

• You will need to dynamically allocate memory.

double *p = (double*)malloc(sizeof(double) * 3);

Here 3 can be a variable.

«_Superman_»
• Marked as answer by Wednesday, June 3, 2009 9:13 AM
Thursday, May 28, 2009 11:59 AM
2. If N is defined by user through a function A=func(N,...) could we dfine ateh varaible dynamicaly at each function call ?

1. Unfortunately C language needs a constant expression while defining arrays.
2. So array[n] won't work for C.
3. But you can still allocate dynamic array by using malloc().
4. Have a look at sample code snippet,

#include <malloc.h>

void Function( int nSize )
{
// allocate using malloc().
double* array = (double*) malloc( sizeof( double) * nSize );

// use it like this
array[0] = 10.0f;

// Free after use.
free( array );
}

Regards,
Jijo.

http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
• Marked as answer by Wednesday, June 3, 2009 9:13 AM
Thursday, May 28, 2009 12:05 PM
• Quote>Unfortunately C language needs a constant expression while defining arrays.

For C89/C90 implementations this is true, but C99 implementations support
Variable Length Arrays (VLAs). However, VC++ does not support this at present.

- Wayne

• Marked as answer by Wednesday, June 3, 2009 9:14 AM
Thursday, May 28, 2009 3:15 PM
• Right, better make that _alloca().

Hans Passant.
• Marked as answer by Wednesday, June 3, 2009 11:12 AM
Thursday, May 28, 2009 8:32 PM

All replies

• You will need to dynamically allocate memory.

double *p = (double*)malloc(sizeof(double) * 3);

Here 3 can be a variable.

«_Superman_»
• Marked as answer by Wednesday, June 3, 2009 9:13 AM
Thursday, May 28, 2009 11:59 AM
2. If N is defined by user through a function A=func(N,...) could we dfine ateh varaible dynamicaly at each function call ?

1. Unfortunately C language needs a constant expression while defining arrays.
2. So array[n] won't work for C.
3. But you can still allocate dynamic array by using malloc().
4. Have a look at sample code snippet,

#include <malloc.h>

void Function( int nSize )
{
// allocate using malloc().
double* array = (double*) malloc( sizeof( double) * nSize );

// use it like this
array[0] = 10.0f;

// Free after use.
free( array );
}

Regards,
Jijo.

http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
• Marked as answer by Wednesday, June 3, 2009 9:13 AM
Thursday, May 28, 2009 12:05 PM
• Quote>Unfortunately C language needs a constant expression while defining arrays.

For C89/C90 implementations this is true, but C99 implementations support
Variable Length Arrays (VLAs). However, VC++ does not support this at present.

- Wayne

• Marked as answer by Wednesday, June 3, 2009 9:14 AM
Thursday, May 28, 2009 3:15 PM
• Quote>Unfortunately C language needs a constant expression while defining arrays.

For C89/C90 implementations this is true, but C99 implementations support
Variable Length Arrays (VLAs). However, VC++ does not support this at present.

- Wayne

oh! I didn't know that. Thanks Wayne!

Regards,
Jijo.

http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
Thursday, May 28, 2009 3:45 PM
• Use _malloca() for the exact same effect.  Memory is allocated on the stack and doesn't have to be released.

Hans Passant.
Thursday, May 28, 2009 6:15 PM
• Use _malloca() for the exact same effect.  Memory is allocated on the stack and doesn't have to be released.

Hans Passant.

Not what my documentation on _malloca() says.

It says it might allocate on the heap, and you must use _freea() to release the memory.

David Wilkinson | Visual C++ MVP
• Marked as answer by Wednesday, June 3, 2009 9:14 AM
• Unmarked as answer by Wednesday, June 3, 2009 11:12 AM
Thursday, May 28, 2009 8:13 PM
• Right, better make that _alloca().

Hans Passant.
• Marked as answer by Wednesday, June 3, 2009 11:12 AM
Thursday, May 28, 2009 8:32 PM