Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <stdio.h>
void main()
{
int arr[]= {1,4,6,9,2};
int n = 5;
 for(int i=0;i<n;i++)
   printf ("befoe adding");
   printf ("arr[%d]=%d\n",i,arr[i]);
 arr[n]=7;
 n=n+1;
 for(int i=0;i<n;i++)
    printf("after adding");
    printf ("arr[%d]=%d\n",i,arr[i]);
}


What I have tried:

#include <stdio.h>
void main()
{
int arr[]= {1,4,6,9,2};
int n = 5;
 for(int i=0;i<n;i++)
   printf ("befoe adding");
   printf ("arr[%d]=%d\n",i,arr[i]);
 arr[n]=7;
 n=n+1;
 for(int i=0;i<n;i++)
    printf("after adding");
    printf ("arr[%d]=%d\n",i,arr[i]);
}
Posted
Comments
Richard Deeming 13-May-24 3:41am    
Two copies of the same unexplained code-block do not make a question.

You need to explain precisely what you are trying to do, what you have tried, and where you are stuck.

At a guess, based on the title, are you trying to increase the size of an array, which is a fixed-size construct? That would typically involve creating a new array and copying the values from the existing array to the new array.

There are probably better collection types you could use if you need to do this.
Rick York 15-May-24 3:51am    
I have written code that does insertion into an array before and I found there two key things. First is pre-allocate more memory than will ever be actually needed for the array. Second is the function memmove is very handy for moving the array elements down to make room for the new item(s). In my case it was an array of characters but the principles are the same.

There are multiple issues with this code.

It would seem that you intended the first for loop to print the array before adding a new element. However, the printf statement is not inside the loop because there are no braces {} after the for statement. This means that only the statement printf ("befoe adding"); is executed n times, and the statement printf ("arr[%d]=%d\n",i,arr[i]); is executed only once after the loop, with i being equal to n (which is out of bounds for the array since there are 5 elements and the index is zero based).

arr[n]=7; I am guessing that you wanted to add a new element to the array at index n. C arrays are of fixed size, you can't do it like this.

The second for loop has the same problem as the first, no curly braces.

1. Modify the for loops with curly braces
2. Write code to create a new array with space for one or more extra elements and copy the existing array elements into the new array
3. Modify the second for loop to print the contents of the new array rather than the original array

M
 
Share this answer
 
To add to what M-Badger has said, you can't "insert" a value into an array at any point, unless you either throw away an element, or allocate a new, bigger array to put it in - which isn't a trivial operation, and which needs to be planned out pretty carefully or it can get very inefficient and wasteful (to the point of crashing your app).

When you allocate an array inside a function in C, the memory for the arry comes from the stack which is a small area of memory that gets allocated when the function execution starts and deallocated automatically when it exits - you cannot expand that space at all because the area allocated and released is always a specific size which is determined at compile time and which can;t be changed.

If you want to allocate a bigger array, you need to allocate both the original and the new array on the heap (using malloc) and then copy'n'insert the data. If you do want to do that have a look here: List<T> - Is it really as efficient as you probably think?[^] - it's not showing you C code to do it, but it explains what the system does for C# code under the same circumstances, and why that means it can be spectacularly inefficient if you aren't careful!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900