answersLogoWhite

0


Best Answer

It is possible, if it is a dynamically allocated array:

int n= 0; Elem *p= NULL;

...

AddElem (Elem newval)

{

n += 1; p = (Elem *)realloc (p, n*sizeof (Elem));

p[n-1] = newval;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

To append an element to an array you must reallocate the array with one more element than it currently has. Reallocation may expand the existing memory, or it may allocate fresh memory if there isn't room for expansion. If new memory is allocated, the existing elements will be copied automatically to the new location. Either way, you will have an empty element at the end of the array which will accommodate the new object.

Note that you cannot expand a static array -- the array must be allocated dynamically to begin with.

The example shown below demonstrates allocation and reallocation of a dynamic array. The code includes fail-safes should any allocation fail.

#include <iostream>

using namespace std;

int main()

{int max=25, i;

char *c, *o;

// Allocate memory for max char types.

if((c=(char*)malloc(max*sizeof(char)))==NULL) return(1); // Report error to caller.

// Initialise memory with consecutive letters (a to y).

for(i=0; i<max; ++i ) c[i]='a'+i;

// Print array.

for(i=0; i<max; ++i) cout<<c[i];

cout<<endl;

// Save current pointer in case of error.

o=c;

// Reallocate the memory to accommodate a new char.

if((c=(char*)realloc(c,++max*sizeof(char)))==NULL)

{ free(o); // Release original pointer.

return(1); // Return error to caller. }

// Insert new char at end of array.

c[max-1]='z';

// Print new array.

for(i=0; i<max; ++i) cout<<c[i]; cout<<endl;

// Release array.

free(c);

return(0); // Report success to caller.

}

Output:

abcdefghijklmnopqrstuvwxy

abcdefghijklmnopqrstuvwxyz

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you append an item into an array using c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Appending array elements using function in c plus plus programming?

#include&lt;iostream&gt; void append(std::vector&lt;int&gt;&amp; v, int i){ v.push_back(i); } int main() { std::vector&lt;int&gt; v; append( v, 100 ); // same as calling v.push_back(100); return(0); }


How do you find matrix in c?

using multidimensional array


Find largest value algorithm in c?

** pseudo code ** if array length == 1, return first element else if array length &gt; 1 max = first element for second item to last item if item &gt; max max = item return max else // array length is 0 error


Can you give an example of array using character variable in c program?

the example of array over charcter variables is char ["string"]


How do you make a C plus plus program that arrange the the numbers in ascending order?

Heres something i whipped up in a hurry... This uses the Bubble Sort method found (related links) #include &lt;iostream&gt; using namespace std; int main(int argc, const char* argv) { int arraysize = 5; //Unsorted array size int array [] = { 5, 3, 4, 2, 1 }; //The array of numbers itself //Display the unsorted array cout &lt;&lt; "Before: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; //Acctually sort the array int tmp=0; //Used for swaping values for (int loop=0; loop &lt;= (arraysize - 1); loop++) { for (int c=0; c &lt;= (arraysize - 1); c++) //The sort loop { if (array[c] &gt; array[c + 1]) { //Swaps the two values in the array tmp = array[c]; array[c] = array[c + 1]; array[c + 1] = tmp; //Cleanup tmp = 0; } } } //Display the sorted array cout &lt;&lt; "After: {"; for (int c=0; c &lt;= arraysize; c++) { cout &lt;&lt; array[c]; if (c != arraysize) { cout &lt;&lt; ","; } } cout &lt;&lt; "}" &lt;&lt; endl; return 0; }


Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }


Sorting an array in PHP without using sort function?

plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


What is the array of string in c?

A string in C is stored in a 1 dimension array so an array of strings is simply a two dimension array.


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.