C programming: array declared before main() and initialised in main()

  • Thread starter nobahar
  • Start date
  • #1
493
2
Hello!

I am sure I am again missing something obvious:
I tried to declare the array and assing value within main() as follows:
Code:
#include <stdio.h>

float array_1[10];

int main( void )
{
    array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };
    
   ...other stuff...

    return 0;
}
I can declare and initialise together before main():
Code:
#include <stdio.h>

float array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };

int main( void )
{
    
   ...other stuff...

    return 0;
}
and within main():
Code:
#include <stdio.h>

int main( void )
{
    float array_1[10] = { 1,1,2,3,4,5,6,7,8,9 };
    
   ...other stuff...

    return 0;
}
Is this an actual "restriction" or am I doing something wrong? The textbook I am working through gives me no guidance on this.

Thanks in advance!
 

Answers and Replies

  • #2
12,209
5,903
You could have defined it as a float pointer and then did the one assignment in your main using only the name and without the square bracket index stuff.

Code:
float *xxx = { 1.0, 2.0, 3.0 };

main() {

  xxx = { 4.0,5.0,6.0};

}
Also in your example, you've actually created a new array in the main context overriding the globally defined one.

Here's a tutorial on this stuff that may help:

http://www.cplusplus.com/doc/tutorial/arrays/
 
Last edited:
  • #3
493
2
I was confused because with single variables I can declare and initialise the variable before main() and then change the value in main() using the assign function = . I can also declare the variable outside main() and assign a value in main(); I figured arrays would be the same since they are "collections of variables", but apparently not.
Thanks for the link, I have a textbook but it seems kind-of neglective on some points; does C++ apply to C? I know they are related, but I am new to C and I have never used C++.

Thanks for the help jedishrfu.
 
  • #4
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
16,530
6,890
does C++ apply to C?
C++ is an object oriented language that uses the basic syntax of C, but it would be a huge mistake to think that if you know C then learning C++ is trivial. The OO stuff makes it a whole different ball game.

EDIT: one thing to note is that any C++ compiler will automatically compile C code because C is a subset of C++. This is possible because C++ compilers, unlike Java for example, do not REQUIRE that everything be OO, they just ALLOW for OO programming.
 
  • #5
AlephZero
Science Advisor
Homework Helper
6,994
291
You could have defined it as a float pointer and then did the one assignment in your main using only the name and without the square bracket index stuff.

Code:
float *xxx = { 1.0, 2.0, 3.0 };

main() {

  xxx = { 4.0,5.0,6.0};

}
That is not standard C, and was not standard C++ until the latest version of the standard (C++11).

In C, the { 1.0, 2.0, 3.0 } notation is an initializer list which can only be used when you declare a variable. C++11 has extended the notation to be an "anonymous array", i.e. an array of values that doesn't have a variable name, which you can use "anywhere" in similar ways to scalar constants like 42 or 3.0.
 
  • #6
12,209
5,903
That is not standard C, and was not standard C++ until the latest version of the standard (C++11).

In C, the { 1.0, 2.0, 3.0 } notation is an initializer list which can only be used when you declare a variable. C++11 has extended the notation to be an "anonymous array", i.e. an array of values that doesn't have a variable name, which you can use "anywhere" in similar ways to scalar constants like 42 or 3.0.
Thanks for the clarification, my C/C++ experience is from the 1980s and has been muddled together with my present java knowledge.
 
  • #7
493
2
Thanks for the responses everyone! I was planning on familiarising myself with C then moving onto C++ - the textbook assures me this is a good move.....
 
  • #8
66
24
Thanks for the responses everyone! I was planning on familiarising myself with C then moving onto C++ - the textbook assures me this is a good move.....
If you plan to use C extensively as well as C++, that makes sense. If your goal is C++ and you have been convinced that C is an important stepping stone, note that Bjarne Stroustrup strongly disagrees and has often spoken against this assertion.

http://www.stroustrup.com/bs_faq.html#prerequisite

For another example, check this interview, searching for the answer beginning "No. C isn't":

http://electronicdesign.com/dev-tools/interview-bjarne-stroustrup-discusses-c
 
  • #9
493
2
Thanks for the links Integrand; I'll finish this textbook on C because I am half-way through and I have put in the effort already! I bought another textbook on C++ to start afterwards, one good thing is that C has eased me into this type-of programming.
 
  • #10
6,054
391
one thing to note is that any C++ compiler will automatically compile C code because C is a subset of C++.
No, that is not true. For example, new and virtual are keywords in C++ that are not keywords in C. so any C code that has something named new or virtual won't be accepted by a C++ compiler.

There are some more subtle differences, too. So to convert any non-trivial piece of C code into C++ is by no means automatic.
 

Related Threads on C programming: array declared before main() and initialised in main()

Replies
9
Views
2K
  • Last Post
Replies
4
Views
5K
Replies
14
Views
4K
Replies
2
Views
4K
Replies
4
Views
1K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
3
Views
4K
Replies
2
Views
38K
Top