Overclock.net banner

1 - 12 of 12 Posts

·
Registered
Joined
·
304 Posts
Discussion Starter · #1 ·
I thought my professor said that you cannot declare the size of an array with a variable. ex.
int elements = 5;
unsigned a[elements];
I just tried that in my program and it worked. I am curious if I misunderstood my professor or if it is allowing it because I'm using visual c++ as a compiler and that method is legal in c++. Thanks!

Kyle
 

·
Premium Member
Joined
·
10,773 Posts
Older compilers don't support it, but it is valid syntax (they were added to the C language in C99).

This snippet is completely acceptable:

Code:

Code:
float ReadAndDoSomething( int n )
{
    float vals[ n ];

    for ( int i = 0; i < n; i++ )
    {
        vals[ i ] = Read();
    }

    return DoSomething( vals, n );
}
http://www.cplusplus.com/forum/beginner/1601/

Visual C++ does. You're good to go.

C++ has dynamic arrays anyway (like you'll see in the link above).

Although I don't recommend using variables unless you need to.... use a macro instead.
 

·
Banned
Joined
·
12,758 Posts
Quote:
Originally Posted by Kyleinator;15205902
I thought my professor said that you cannot declare the size of an array with a variable. ex.
int elements = 5;
unsigned a[elements];
I just tried that in my program and it worked. I am curious if I misunderstood my professor or if it is allowing it because I'm using visual c++ as a compiler and that method is legal in c++. Thanks!

Kyle
I believe it's legit if you make that int 'const'

const int elements = 5;

However what kind of array is that? Unsigned what? Surely that didn't compile.
 

·
Premium Member
Joined
·
10,773 Posts
Quote:
Originally Posted by lordikon;15207634
I believe it's legit if you make that int 'const'

const int elements = 5;

However what kind of array is that? Unsigned what? Surely that didn't compile.
Doesn't need to be const.

It is an unsigned int. It will expand to "unsigned int" if you just write "unsigned". See http://en.cppreference.com/w/cpp/language/types

Remember he is using Visual C++ compiler, so the rules are different, but C99 will happily accept a variable length array declaration.
 

·
Banned
Joined
·
12,758 Posts
Quote:
Originally Posted by tompsonn;15207642
Doesn't need to be const.

It is an unsigned int. It will expand to "unsigned int" if you just write "unsigned". See http://en.cppreference.com/w/cpp/language/types

Remember he is using Visual C++ compiler, so the rules are different, but C99 will happily accept a variable length array declaration.
Ick, I've never seen someone omit the 'int', seems like poor form. Good to know though, thanks for the link.
 

·
Premium Member
Joined
·
10,773 Posts
Quote:
Originally Posted by lordikon;15207684
Ick, I've never seen someone omit the 'int', seems like poor form. Good to know though, thanks for the link.
Definitely agree! I've never done it either, it'll easily become confusing!
cool.gif
 

·
Premium Member
Joined
·
11,272 Posts
Strictly speaking, the compiler needs to know on compile time what the array size is. In other words, constants are fine.

int size = 100;

unsigned int array[size]; <- OK

int size = getSizeFromUser(); <- not ok

Most people program in ANSI C and not C99, in which case variable-based arrays won't work - the compiler won't do your dirty work for you.
 

·
Registered
Joined
·
304 Posts
Discussion Starter · #8 ·
I am just a student taking a C course but my professor always declares as just "unsigned" not "unsigned int." Would it be better if I included the int for unsigneds? Also thanks for answering my question.
 

·
Banned
Joined
·
12,758 Posts
Quote:
Originally Posted by Kyleinator;15216857
I am just a student taking a C course but my professor always declares as just "unsigned" not "unsigned int." Would it be better if I included the int for unsigneds? Also thanks for answering my question.
Definitely, in my years I've never seen someone just use 'unsigned', especially since there are 'unsigned __int64' and 'unsigned char' as well. I'm almost always explicit with my code, when things are implicit I find it harder to read sometimes, and more prone to error.
 

·
Premium Member
Joined
·
10,773 Posts
Quote:


Originally Posted by lordikon
View Post

Definitely, in my years I've never seen someone just use 'unsigned', especially since there are 'unsigned __int64' and 'unsigned char' as well. I'm almost always explicit with my code, when things are implicit I find it harder to read sometimes, and more prone to error.

Then you're not going to like C#'s implicit casting !
 

·
Premium Member
Joined
·
11,272 Posts
C++ has implicit casting too, but you can make sure your type doesn't use by declaring explicit constructors. I'm sure there's something like that in C# too.

It's a matter of preference, but I also agree that explicit is better than implicit, anywhere.
 

·
Premium Member
Joined
·
10,773 Posts
Quote:


Originally Posted by Coma
View Post

C++ has implicit casting too, but you can make sure your type doesn't use by declaring explicit constructors. I'm sure there's something like that in C# too.

It's a matter of preference, but I also agree that explicit is better than implicit, anywhere.

Indeed.

In C# casting isn't implicit on user defined types unless you define an implicit operator for that type.
 
1 - 12 of 12 Posts
Top