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

Discussion Overview

The discussion revolves around the correct syntax for declaring arrays in C, specifically comparing two examples: "int arr1[4]={1,2,3,4};" and "int arr[3]={1,2,3,4};". Participants also explore implications of these declarations in the context of an algorithm mentioned in a source text.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants assert that both array declarations are syntactically correct, but question the meaningfulness of the second declaration due to its size being smaller than the number of initialized elements.
  • One participant emphasizes that the dimension number of an array must be at least as large as the number of initial elements, suggesting that the second example should produce a compiler error.
  • Another participant mentions that leaving the dimension undefined allows the compiler to infer the size based on the number of initial elements, providing an example of a declaration that works without specifying the size.
  • There is a request for clarification on an algorithm that uses array indices, with concerns about potential confusion due to the pseudocode format and its translation to C.
  • A participant points out that running the code snippets through a compiler could clarify which declaration causes an error, questioning the need for external validation.

Areas of Agreement / Disagreement

Participants generally agree on the syntactical correctness of the first array declaration and the potential issues with the second. However, there is no consensus on the implications of the algorithm mentioned, and the discussion remains unresolved regarding its correctness.

Contextual Notes

The discussion includes references to pseudocode and its differences from C syntax, highlighting the need for careful handling of array indices when converting between the two. There are also mentions of compiler behavior that may vary based on specific implementations.

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
4K
  • · 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 5 ·
Replies
5
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K