9

I have an array of strings with a given size, without using any memory allocation, how do I append something into it?

Say I run the code, its waiting for something you want to enter, you enter "bond", how do I append this into an array ? A[10] ?

5
  • 1
    Arrays can't change size. Commented Apr 17, 2015 at 12:38
  • 2
    If you just want to assign a value to a specific spot in the array, see this: stackoverflow.com/questions/1088622/… Commented Apr 17, 2015 at 12:40
  • 1
    just do A[10] = "bond";, supposing the array was made as a string Commented Apr 17, 2015 at 12:41
  • 1
    As others said, if the array is fixed size, but has room for more items, you need to keep track of the last item, either with a counter or a sentry (e.g. a '\0' for strings, NULL etc.), then update the counter/sentry and insert the item. Commented Apr 17, 2015 at 12:42
  • 1
    You may also want to take a look at this. [stackoverflow.com/questions/5406935/… Commented Apr 17, 2015 at 12:44

5 Answers 5

9

If the array declared like

char A[10];

then you can assign string "bond" to it the following way

#include <string.h>

//...

strcpy( A, "bond" );

If you want to append the array with some other string then you can write

#include <string.h>

//...

strcpy( A, "bond" );
strcat( A, " john" );
Sign up to request clarification or add additional context in comments.

Comments

3

You can't append to an array. When you define the array variable, C asks the is for enough contiguous memory. That's all the memory you ever get. You can modify the elements of the array (A[10]=5) but not the size.

However, you CAN create data structures that allow appending. The two most common are linked lists and dynamic arrays. Note, these are no built into the language. You have to implement them yourself or use a library. The lists and arrays of Python, Ruby and JavaScript are implemented as dynamic arrays.

LearnCThHardWay has a pretty good tutorial on linked lists, though the one on dynamic arrays is a little rough.

Comments

2

Hi,

It really depends on what you mean by append.

...
int tab[5]; // Your tab, with given size
// Fill the tab, however suits you.
// You then realize at some point you needed more room in the array
tab[6] = 5; // You CAN'T do that, obviously. Memory is not allocated.

The problem here can be two things :

  • Did you misjudge the size you need ? In that case, just make sure this given size you mentioned is correctly 'given', however that might be.
  • Or don't you know how much room you want at the beginning ? In that case, you''ll have to allocate the memory yourself ! There is no other way you can resize a memory chunk on the fly, if I might say.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define STR_MAX_SIZE 255                                // Maximum size for a string. Completely arbitray.
char *new_string(char *str) { char *ret; // The future new string;
ret = (char *) malloc(sizeof(char) * 255); // Allocate the string strcpy(ret, str); // Function from the C string.h standard library return (ret); }
int main() { char *strings[STR_MAX_SIZE]; // Your array char in[255]; // The current buffer int i = 0, j = 0; // iterators
while (in[0] != 'q') { printf("Hi ! Enter smth :\n"); scanf("%s", in); strings[i] = new_string(in); // Creation of the new string, with call to malloc i++; } for ( ; j < i ; j++) { printf("Tab[ %d ] :\t%s\n", j, strings[j]); // Display free(strings[j]); // Memory released. Important, your program // should free every bit it malloc's before exiting }
return (0); }


This is the easiest solution I could think of. It's probably not the best, but I just wanted to show you the whole process. I could have used the C standard library strdup(char *str) function to create a new string, and could have implemented my own quick list or array.

Comments

0

The size of an array variable cannot change. The only way to append to an array is to use memory allocation. You are looking for the realloc() function.

7 Comments

He specifically said he had a fixed array.
@csl To which I say that it can't be done (observe my wording “array variable”). I then proceed to explain how it can be done is by altering the prerequisites. Please read my answer before dismissing it as wrong.
He said "without using any memory allocation", so I guess that means no realloc. My only guess is that he's got a preallocated one and is supposed to use spare room in that.
Actually, I think, he's got an empty array with fixed size and wants to simply add value on certain position.
@csl Again, it seems like you didn't read my comment either: In the first sentence of my answer I explain that what OP wants cannot be done. Then I explain how the question can be answered by altering the question's constraints. Yes, OP doesn't want to use memory allocation but the reason for that is probably that OP is afraid of using it, not that it's the wrong thing to use.
|
0

If you want to append a character or string to it;

strcpy(a, "james")
strcpy(a, "bond")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.