Int arr1[4]= {1,2,3,4} or int arr[3]={1,2,3,4}

  • Context: C/C++ 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the correct syntax for array initialization in C, specifically comparing "int arr1[4]={1,2,3,4};" and "int arr[3]={1,2,3,4};". Both syntaxes are valid, but the second example is incorrect as it specifies an insufficient size for the initialized elements, leading to a compiler error. The correct approach is to either define the array size to match or exceed the number of initial elements or to leave the size undefined, as in "int arr2[]={1,2,3,4};", which automatically sets the size based on the initializer.

PREREQUISITES
  • Understanding of C programming syntax
  • Familiarity with array initialization rules in C
  • Knowledge of compiler error messages related to array sizes
  • Basic concepts of pseudocode and its differences from C
NEXT STEPS
  • Research C array initialization rules and best practices
  • Learn about compiler error handling in C programming
  • Study the differences between pseudocode and C syntax
  • Explore debugging techniques for array-related errors in C
USEFUL FOR

Students learning C programming, software developers working with C, and anyone interested in understanding array initialization and compiler behavior in C.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
declaring an array in C
I'm wondering which one is the correct syntax? Of course both are correct syntax, but which one is meaningful? my confusion comes from this algorithms:
1675608095901.png

Source: classic data structures by debasis samanta
 
Last edited:
Technology news on Phys.org
The dimension number should be at least as large as the number of initial elements specified. It can be larger. Your second example where it is too small should give a compiler error message. You can leave it undefined and the compiler will set it at the number of initial elements in the list: "int arr2[]={1,2,3,4};" is the same as "int arr1[4]={1,2,3,4};"
 
FactChecker said:
The dimension number should be at least as large as the number of initial elements specified. It can be larger. Your second example where it is too small should give a compiler error message. You can leave it undefined and the compiler will set it at the number of initial elements in the list: "int arr2[]={1,2,3,4}" is the same as "int arr1[4]={1,2,3,4}"
can you take a look at updated question? is the algorithm wrong? where it says U=U1+U2-L2+1?
 
shivajikobardan said:
can you take a look at updated question? is the algorithm wrong? where it says U=U1+U2-L2+1?
First, the algorithm is expressed in pseudocode, which should not be confused with C.
I think the numbers work out.
I get that the total number of elements in the appended array are ##(U_1-L_1+1)+(U_2-L_2+1) = (U_1+U_2-L_2+1)-L_1+1 = U-L+1##.
I think that it is confusing to use the element numbers as array indices, but that is language specific and this is pseudocode. Be careful with the array indices if you convert this to C.
 
You could have run both snippets of code through the compiler and seen which one caused an error. Why do you need us to do this for you?

It has got to be slower to wait for us than to do it yourself.
 
Last edited:

Similar threads

  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
9
Views
3K