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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:14:03+00:00 2026-05-12T17:14:03+00:00

I am still learning C and I’m having some trouble figuring out how to

  • 0

I am still learning C and I’m having some trouble figuring out how to handle this. Well, I have two structs:

struct myStruct {
    ...
    struct myString *text[5];
    ...
} allStructs;

struct myString {
    char part[100];
};

The objective is to have allStruct[n] point to 5 different parts of a text divided into lines of 100 chars each. So I allocate the space:

allStructs = calloc(n, sizeof(allStructs));

Then, assume that I have a filled char text[500] that I want to divide into 5 parts, and have allStructs[n].text[n].part point at a given part of the text. Can anyone help me with how I proceed?

  • 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-12T17:14:03+00:00Added an answer on May 12, 2026 at 5:14 pm

    Short answer: you can not with this because you have not consider the ‘\0’ character to terminate each string.

    Longer answer:
    Change structs like that to have more flexibility:

    struct myStruct {
       struct myString *text;
    }
    struct myString {
       char *part;
    }
    

    The allocation should be:

    struct myStruct *allStruct = calloc(n, sizeof(struct myStruct));
    

    So you have a pointer/array on n struct myStruct.

    Then initialize all members of allStruct;

    for( i=0; i<n; ++i )
    {
       allStruct[i].text = calloc(5, sizeof(myString));
       // Following for only needed if you want new strings by using the strncpy (see above)
       for( y=0; y<5; ++y )
       {
          allSTruct[i].text[y].part = calloc(101, sizeof(char));
       }
    }
    

    Now you have all vars initialized.

    To copy your 500-chars long string into allStruct[n]:

    for( i=0; i<5; i++ )
    {
       allStructs[n].text[i].part = &text[i*100]; // If you want to point on the existing string
       // OR
       strncpy(allStructs[n].text[i].part, &text[i*100], 100); // If you want to have new strings
    
       // In all case, terminate the string with '\0'
       allStructs[n].text[i].part[100] = '\0';
    }
    

    This should work.

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

Sidebar

Related Questions

Still learning JSP Web Applications here. I have been doing this for a while
I'm still learning LINQ. I can do some simple queries, but this one is
I am still learning to use MVVM and Prism and have some general questions:
I'm still learning about DDD and I have these two (probably simple) questions: If
I'm still learning Grails and seem to have hit a stumbling block. Here are
I'm still learning ASP.NET and I often see code like this throughout parts of
I'm still learning about REST and, in my test, came up with this scenario
This is going to sound like a silly question, but I'm still learning C,
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Still learning this jQuery stuff.... I like this ajax page loader; it works for

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.