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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:07:16+00:00 2026-06-17T18:07:16+00:00

I’m at a loss as far as how to parse the line in getline().

  • 0

I’m at a loss as far as how to parse the line in getline(). I would like look at each char that is in the line.

So if someone were to type in: “Hello” into stdin, I’d like to be able to able to access the char array in this manner:

line[0] = 'H' 
line[1] = 'e' 
line[2] = 'l'
line[3] = 'l'
line[4] = 'o'
line[5] = '/0';

I’ve looked at getchar(), but I want to try and use getline() as I feel like it is more convenient. I’ve also looked at scanf(), but it skips whitespaces and doesn’t let me parse the input as nicely as getchar() or getline().

Here is simple code that attempts at getting the first char of a line via stdin, but results in a seg fault:

#include <stdio.h>                                                                                                
#include <stdlib.h>                                                                                               

int main()                                                                                                        
{                                                                                                                 
  int len;                                                                                                        
  int nbytes = 100;                                                                                               
  char *line = (char *) malloc (nbytes + 1);                                                                      

  while(getline(&line, &nbytes, stdin) > 0){                                                                      
    printf("first char: %s", line[0]);  //try and get the first char from input

    /**
     * other code that would traverse line, and look at other chars 
     */                                                                         
  };                                                                                                              

  return 0;                                                                                                       
} 

Thanks.

  • 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-17T18:07:17+00:00Added an answer on June 17, 2026 at 6:07 pm

    Use the %c format specifier to print out a single character.

    The code

    printf("first char: %s", line[0])
    

    will try to treat line[0] as the address of a char array. If you just want to print out the first character, change it to

    printf("first char: %c", line[0])
    //                   ^
    

    There are a couple of other small changes you could consider in other parts of your code:

    • you don’t need to cast the return from malloc
    • you should free(line) after your while loop
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.