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

  • Home
  • SEARCH
  • 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 8860247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:13:46+00:00 2026-06-14T15:13:46+00:00

I need to get a specific line in a char * but im doing

  • 0

I need to get a specific line in a char * but im doing something wrong with the arrays… any ideas how can I do it?

The function is something like this:

char * line get_line(char * code, int num_line);

where should i put the frees and mallocs?

thanx!

Im sorry… I had to be more explicit, this is how I solved it (is in spanish)

char* obtenerLinea(char*cont, int numLinea)
{
    int32_t lineaActual=0,comienzoLinea=0,caracterActual=0;        // línea
    char *cadena;

    while(lineaActual<numLinea)
    {
        comienzoLinea=caracterActual;
        while(contenido[caracterActual]!='\n')
            {caracterActual++;}
        caracterActual++;
        lineaActual++;
    }
    cadena = sub_string(contenido, comienzoLinea, caracterActual-comienzoLinea);

    return cadena;
}
  • 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-14T15:13:47+00:00Added an answer on June 14, 2026 at 3:13 pm

    My short answer is: put the malloc inside the function and the free outside.

    But the way you asked your question, it’s almost like saying “I want to throw a tea party… Where should I put the table and chairs?”

    What I mean is your question is open to interpretation:

    1. You don’t want to modify the input string (even though you’ve said it’s allowed by not making it const).
    2. You do want to modify the input string, and want to return a pointer into it.
    3. It doesn’t matter whether you modify the input string or not – you still want to copy a string out of it.

    Well, it turns out that in cases 1 and 3 above, you’ll be making a call to either malloc or strdup inside the function. If malloc, you’ll then copy string data into the new memory. For strdup you’d have to modify the input string to null-terminate the line. In these cases, you’ll return a newly allocated pointer, which the caller is responsible for freeing when they are finished with it. Note there is also the possibility of using strndup without modifying the buffer.

    In the case of number 2, there will be no calls to malloc or free because you’re just modifying the buffer and returning a pointer to some part of it.

    In all cases, I’ve assumed that you already know where to call malloc and free for the input buffer itself!

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

Sidebar

Related Questions

I have a query such that I need to get A specific dog All
I need to get some pictures which have a specific hashtag using PHP ?
I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all
I need to get all the objects whose id matches a specific pattern .
I need to get the value of a field with a specific annotation, So
today i need your help to get an specific sql select query. i have
I am doing this in java, but the answer isn't java specific. Here is
I'm trying to get the following line to compile, but i'm suffering from pointer
I need get the id of an images which is place inside a datatemplate..
Just need get some vals located in application.ini(main ini) in the Controller plugin I

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.