Reputation: 510
I have an array of strings that has been read from a file. At some point i need to take one element out, add * before and * after and put it back to the same array.
So far I've managed to add one asterisk at the end with strcat
. And it is printing it out correctly.
Now, how do I add the one at the beginning?
//malloc for the array has been done when read from file
char **array;
int arraySize;
for (i=0;i<arraySize;i++){
if (some_condition){
//Add * chars
array[i]=strcat(array[i],"*");
printf("Element %s was marked",array[i]);
}
//prints for example *foo*
}
Sorry if the question is completely stupid and the answer might be obvious. Thanks for any possible answers in advance!
UPD: array malloc function
void readd(FILE *file){
size=0; /*local size */
char line[BUFSIZ]; /* Local array for a single word read */
while ((fgets(line,sizeof(line),file))!=NULL){
/* trim newline char */
if (line[strlen(line)-1]=='\n')
line[strlen(line)-1] = '\0';
array=(char**)realloc(array,(size+1)*sizeof(char *));
array[size++]=strdup(line);
}
}
Upvotes: 0
Views: 407
Reputation: 48310
If the original array[i]
elements have enough room for the additional characters:
int len = strlen(array[i]);
memmove(array[i] + 1, array[i], len); // memmove() allows overlap btwn src & dest
array[i][0] = '*';
array[i][len+2] = '\0';
array[i][len+1] = '*'
Edit: Since you've updated your question, I'll update my answer. :-)
First, be advised that it's dangerous to use realloc()
like this:
array=(char**)realloc(array,(size+1)*sizeof(char *));
If the realloc()
fails, then array
is reassigned to NULL
and the original pointer is lost, orphaning the memory that had been allocated to it. This is safer:
char **tmp = realloc(array,(size+1)*sizeof(char *));
if (tmp == NULL) {
// Out of memory error
}
array = tmp;
Of course, if you'll be doing this often, you might as well keep track of the current size and allocate room for another hundred or thousand elements at a time.
Since the individual strings are allocated with strdup()
you have two options:
Allocate an extra 2 characters for each, in case you need to add the asterisks. This isn't unreasonable unless there are millions of strings, or the system is memory-constrained.
Reallocate each string before adding the asterisks, using code like the snippets above.
Upvotes: 2
Reputation: 28772
You could copy every character in array[i]
one position to the right (to a higher index) starting from the back and going towards the beginning, then replace the first character with the '*'.
This assumes that you have enough space to hold the two extra stars.
If not, you have two choices
malloc
a big enough area then copy the string starting from index 1 and place the stars at the appropriate position (index 0 and index strlen(array[i])+1
. Don't forget to re-assign the pointer in array[i]
to the malloc'd area afterwards.Upvotes: 1
Reputation: 3596
The easiest method would be to go with doing a double strcat.
However, have you thought about memory management for this kind of thing? How are your string saved in memory?
On this line, you lose the pointer to the original string:
array[i]=strcat(array[i],"*");
Do you have another reference to the original pointer? Maybe you want to save it first, then reallocate with the same length + 2 characters, then put the first *, copy the string, put the last * and delete the original pointer.
Upvotes: 1