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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:18:42+00:00 2026-06-06T21:18:42+00:00

I have a char that is given from fgets , and I would like

  • 0

I have a char that is given from fgets, and I would like to know how I can convert it into a char*.

I am sure this has been posted before, but I couldn’t find one that was doing quite what I wanted to do. Any answer is appreciated.

EDIT:
Here is the code.

char *filename = "file.txt";
FILE *file = fopen(filename, "r");
if(file != NULL) {
  char line[260];
  char *fl;
  while(fgets(line, sizeof line, file) != NULL) {
    // here I combine some strings with the 'line' variable.
    str_replace(line, "\"", "\"\""); // A custom function, but it only takes char*'s.
  }
  printf(fl);
  printf("\n");
} else {
  printf(" -- *ERROR* Couldn't open file.\n");
}
  • 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-06T21:18:44+00:00Added an answer on June 6, 2026 at 9:18 pm

    Well, first of all, line is an array of chars and so can be manipulated in much the same way as a char * (See comp.lang.c FAQs for important differences), so you don’t need to worry about it.

    However, in case you want an answer to the general question…

    The & operator is what you need:

    char c;
    char *pChar = &c;
    

    However, bear in mind that pChar is a pointer to the char and will only be valid while c is in scope. That means that you can’t return pChar from a function and expect it to work; it will be pointing to some part of the heap and you can’t expect that to stay valid.

    If you want to pass it as a return value, you will need to malloc some memory and then use the pointer to write the value of c:

    char c;
    char *pChar = malloc(sizeof(char));
    
    /* check pChar is not null */
    
    *pChar = c;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class User that looks like this: class User { private: char*
I have a C-array that looks like this: char hexc[] = { 0x41, 0x80,
I have an array that is like this: unsigned char array[] = {'\xc0', '\x3f',
In phpMyAdmin I have a char(32) column that has no default value(ie. Default: None).
I have extracted a MAC address into a char* array such that each section
I have developed a small view that contains special char like 'é' and when
I have an object that I'm freeing with delete, and it has a char*
I have code that looks something like, #include<stdlib.h> #include<string.h> char** someArray = NULL; size_t
I would like to be able to generate all possible strings from a given
I have an application that prompt to user an character from user: char letter;

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.