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 1078793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:46:34+00:00 2026-05-16T21:46:34+00:00

My problem is very simple, but I really don’t know how to manipulate strings

  • 0

My problem is very simple, but I really don’t know how to manipulate strings in C

The problem is:
I have a struct called person

struct person
{
  char name[25] // use char *name better?
}p;

I also have a function called p *addNewPerson(char *name)

p *addNewPerson(char *name)
{
  p *newPerson = (p *)malloc(sizeof(person));
  //here how can I assign the name to this new person?
  ...
  return newPerson;
}

So, in the main function

void main()
{
   for(; ;)
  {
    char input[25];
    scanf("%s", input); // is this way possible?
    //shoud I do something with this "input", like input[strlen(input)-1] = '\0'
    //call addNewPerson()
    p *newPerson = addNewPerson(&input);
    //store this newPerson in some data structure
    ...
  }
}

Clarification: the question is how can I assign the name to this new person inside p *addNewPerson(char *name)?

  • 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-05-16T21:46:34+00:00Added an answer on May 16, 2026 at 9:46 pm
    p *newPerson = (p *)malloc(sizeof(person));
    //here how can I assign the name to this new person?
    

    You would do this:

    strcpy(p->name,name);
    

    You should also change your use of p to struct person since p is not a type, it is a global veriable of type struct person . Change the code to:

    struct person *addNewPerson(char *name)
    {
       struct person *newPerson = malloc(sizeof *newPerson);
    

    //shoud I do something with this
    “input”, like input[strlen(input)-1] =

    No, scanf will nul terminate the string for you.

    Be aware though, string handling in C needs to be done very, very carefully.
    e.g. when you do scanf("%s", input); , what happens if you enter a name longer than 24 characters ?

    Anything might happen. scanf might overflow your buffer, and you get undefined behavior. You should do atleast this:

      int ret = scanf("%24s",buffer);  //read max 24 chars, to make space for a final '\0'
      if(ret == EOF) { //end of input reached.
         break; 
      if(ret != 1) {
        // for whatever reason, the conversion failed. exit, or alert the user, or whatever
      }
    

    Similarly inside your addNewPerson at the strcpy(p->name,name); , strcpy might happily write past
    the buffer if the name is longer than what p->name can hold. In this particular case, with the above modification to scanf, the length will always be 24 or less so it’s safe. But be very aware of this in general .

    The call to addNewPerson should just pass the name of buffer directly, when used as a value, the name of an array decays to a pointer to the first element of that array;

     struct person *newPerson = addNewPerson(input);
    

    Since newPerson is dynamically allocated with malloc() , remember to free() it when you no longer need it. Otherwise you will leak memory.

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

Sidebar

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.