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

Click For Summary

Discussion Overview

The discussion revolves around the declaration and initialization of arrays in C programming, particularly focusing on the differences between declaring arrays outside of the main function versus within it. Participants explore the implications of these approaches and their understanding of array initialization syntax.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant expresses confusion about the inability to initialize an array within the main function using the syntax typically used for variable assignment.
  • Another participant suggests defining the array as a float pointer and initializing it differently, indicating that the original approach overrides the globally defined array.
  • There is a discussion about the relationship between C and C++, with some participants noting that while C++ is based on C, it introduces object-oriented concepts that significantly change the programming paradigm.
  • Clarifications are made regarding the use of initializer lists in C and their limitations compared to C++11, where such lists can be used in more flexible contexts.
  • Participants share their experiences and intentions regarding learning C and C++, with some expressing a desire to transition from C to C++ after gaining familiarity with C.
  • One participant corrects a claim about C code being automatically compilable by C++ compilers, pointing out that there are specific keywords in C++ that do not exist in C, which complicates direct compatibility.

Areas of Agreement / Disagreement

Participants generally agree on the basic principles of array declaration and initialization but exhibit disagreement regarding the nuances of C and C++ compatibility and the implications of different initialization methods. The discussion remains unresolved on some technical points.

Contextual Notes

Limitations include potential misunderstandings of array initialization syntax and the differences between C and C++ that may not be fully addressed in the participants' resources.

Who May Find This Useful

Individuals learning C programming, those transitioning from C to C++, and programmers interested in understanding the differences in array handling between the two languages may find this discussion beneficial.

nobahar
Messages
482
Reaction score
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!
 
Technology news on Phys.org
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:
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.
 
nobahar said:
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.
 
jedishrfu said:
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.
 
AlephZero said:
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.
 
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...
 
nobahar said:
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
 
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
phinds said:
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
10K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
14
Views
4K
Replies
20
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K