Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7775087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:32:30+00:00 2026-06-01T17:32:30+00:00

I have an array of strings that has been read from a file. At

  • 0

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);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:32:31+00:00Added an answer on June 1, 2026 at 5:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that has been converted into an 2D Array in js.
I have been given some 'reports' from another piece of software that contains data
I have an array of strings that I want to use for button titles
I have an array of input strings that contains either email addresses or account
I have a variable that contains : Sometimes array of strings and sometimes array
I have array of strings, String[] data and it's 10 elements has value P
I have been writing some code that creates a generic blog. Its features are
I have to read in some data from text files formated like the example
I have a problem which has been torturing for many hours. I need to
Suppose you have a String[] that has been created after a .split() which then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.