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

  • C/C++
  • Thread starter shivajikobardan
  • Start date
In summary, the algorithm is confusing and the compiler might give an error if you use the element numbers as array indices.
  • #1
shivajikobardan
674
54
TL;DR Summary
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
  • #2
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};"
 
  • #3
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?
 
  • #4
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.
 
  • #5
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:

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

1. What is the difference between int arr1[4]= {1,2,3,4} and int arr[3]={1,2,3,4}?

The difference is in the size of the array. In the first declaration, arr1 has a size of 4 elements, while in the second declaration, arr has a size of 3 elements. However, in the second declaration, the array is being initialized with 4 elements, which can lead to unexpected behavior or errors.

2. Can I access the element arr[3] in int arr[3]={1,2,3,4}?

No, you cannot access arr[3] in this case because the array was declared with a size of 3 elements. Accessing arr[3] would result in going out of bounds of the array, which can lead to undefined behavior or errors.

3. What happens if I try to access arr1[4] in int arr1[4]= {1,2,3,4}?

Accessing arr1[4] in this case would also result in going out of bounds of the array. The last valid index for arr1 would be arr1[3], as the array was declared with a size of 4 elements. Trying to access arr1[4] can lead to undefined behavior or errors.

4. Can I change the size of the array after it has been declared?

No, the size of an array in C or C++ is fixed at compile time and cannot be changed dynamically after declaration. If you need a dynamic size array, you should consider using pointers and dynamic memory allocation.

5. What is the purpose of initializing an array with fewer elements than its declared size?

Initializing an array with fewer elements than its declared size can lead to unexpected behavior or errors. It is always recommended to initialize an array with the exact number of elements it was declared with to avoid any issues related to out of bounds access.

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
17
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
Replies
9
Views
2K
Back
Top