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] ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
dave_1234
  • 147
  • 1
  • 3
  • 10
  • 1
    Arrays can't change size. – user253751 Apr 17 '15 at 12:38
  • 2
    If you just want to assign a value to a specific spot in the array, see this: http://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c – Sami Apr 17 '15 at 12:40
  • 1
    just do A[10] = "bond";, supposing the array was made as a string – lecardo Apr 17 '15 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. – csl Apr 17 '15 at 12:42
  • 1
    You may also want to take a look at this. [http://stackoverflow.com/questions/5406935/reading-a-string-with-scanf] – rafaelbattesti Apr 17 '15 at 12:44

5 Answers5

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" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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.

Nick Bailey
  • 3,078
  • 2
  • 11
  • 13
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.

Naliwe
  • 322
  • 1
  • 8
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.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    He specifically said he had a fixed array. – csl Apr 17 '15 at 12:41
  • 1
    @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. – fuz Apr 17 '15 at 12:43
  • 2
    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. – csl Apr 17 '15 at 12:46
  • 2
    Actually, I think, he's got an empty array with fixed size and wants to simply add value on certain position. – tema Apr 17 '15 at 12:47
  • 1
    @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. – fuz Apr 17 '15 at 12:48
  • No, I get you. It's just that he explicitly wants to do this without memory allocation. Whether he's afraid of realloc or not we don't know. I'll remove my downvote and we'll see. – csl Apr 17 '15 at 12:54
  • 1
    You just didn't get the question due to poor wording (that we all used while learning) and now trying to be authoritatively-offensive. Not much professional nor wise. – user3125367 Apr 17 '15 at 14:02
0

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

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