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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:51:18+00:00 2026-05-18T08:51:18+00:00

I’m reading in a line from a file (char by char, using fgetc()), where

  • 0

I’m reading in a line from a file (char by char, using fgetc()), where all fields(firstname, lastname, …) are seperated by an ;. What I now want to do is create a char**, add all the chars to that and replace the ; by \0 so that I effectively get a list of all the fields.

Is that actually possibly? And when I create a char**, e.g. char ** buf = malloc(80) can I treat it like a one dimensional array? If the memory returned by malloc contiguous?

EDIT

Sry, meant to replace ; by \0, bot \n.

EDIT 2

This code should demonstrate what I intend to do (may clarify things a little):

int length = 80; // initial length char *buf = malloc(length); int num_chars = 0;

// handle malloc returning NULL

// suppose we already have a FILE pointer fp for (int ch = fgetc(fp); ch != EOF && ferror(fp) == 0; ch = fgetc(fp)) {
    if (length == size) { // expand array if necessary
        length += 80;
        buf = realloc(buf, length); 
        // handle realloc failure
    }

    if (ch == ';') {
        buf[num_chars] = '\0'; // replace delimiter by null-terminator
    } else {
        buf[num_chars] = ch; // else put char in buf
    } }

// resize the buffer to chars read buf
= realloc(buf, num_chars);

// now comes the part where I'm quite unsure if it works / is possible

char **list = (char **)buf; // and now I should be able to handle it like a list of strings?
  • 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-18T08:51:19+00:00Added an answer on May 18, 2026 at 8:51 am

    It’s not exactly possible as you describe it, but something similar is possible.
    More specifically, the last line of your example char **list = (char **)buf; won’t do what you believe. char **list means items pointed by *list will be of the type char*, but the content of *buf are chars. Hence it won’t be possible to change one into another.

    You should understand that a char ** is just a pointer, it can hold nothing by itself. But the char ** can be the start address of an array of char *, each char * pointing to a field.

    You will have to allocate space for the char * array using a first malloc (or you also can use a static array of char * instead of a char**. You also will have to find space for every individual field, probably performing a malloc for each field and copying it from the initial buffer. If the initial buffer is untouched after reading, it would also be possible to use it to keep the field names, but if you just replace the initial ; by a \n, you’ll be missing the trailing 0 string terminator, you can’t replace inplace one char by two char.

    Below is a simplified example of what can be done (removed malloc part as it does not add much to the example, and makes it uselessly complex, that’s another story):

    #include <stdio.h>
    #include <string.h>
    
    main(){
        // let's define a buffer with space enough for 100 chars
        // no need to perform dynamic allocation for a small example like this
        char buf[100];
        const char * data = "one;two;three;four;";
    
        // now we want an array of pointer to fields, 
        // here again we use a static buffer for simplicity,
        // instead of a char** with a malloc
        char * fields[10];
        int nbfields = 0;
        int i = 0;
        char * start_of_field;
    
        // let's initialize buf with some data, 
        // a semi-colon terminated list of strings 
        strcpy(buf, data);
    
        start_of_field = buf;
        // seek end of each field and
        // copy addresses of field to fields (array of char*)
        for (i = 0; buf[i] != 0; i++){
            if (buf[i] == ';'){
                buf[i] = 0;
                fields[nbfields] = start_of_field;
                nbfields++;
                start_of_field = buf+i+1;
            }
        }
    
        // Now I can address fields individually
        printf("total number of fields = %d\n"
               "third field is = \"%s\"\n",
               nbfields, fields[2]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.