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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:02:27+00:00 2026-06-14T10:02:27+00:00

Is it possible to create string variables using pointers? So that I don’t have

  • 0

Is it possible to create string variables using pointers? So that I don’t have to pass its size everytime, like char x[4] = “aaa”?

How can I get the size of such string?

And can I initialize an empty string with a pointer?

  • 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-14T10:02:29+00:00Added an answer on June 14, 2026 at 10:02 am

    Remember that strings in C are ended by the null terminator character, written as \0. If you have a well-formed string stored in your pointer variable, you can therefore determine the length by searching for this character:

    char *x = "hello"; // automatically appends a null terminator
    int len = 0;
    while (x[len] != '\0') {
        len++;
    }
    

    If your variables are uninitialized or otherwise not well-formed (e.g., by being NULL) you obviously cannot take this approach; however, most functions are generally written under the assumption that the strings are well-formed, because this leads to faster code.

    If you wish to initialize a pointer, you have three options: NULL, a valid address (e.g., char *x = &someCharVar), or a string constant (e.g., char *x = "hello"). Note that if you use a string constant, it is illegal for you to write into that pointer unless you re-assign to it with the address of a non-constant string.

    // Get enough space for 24 characters plus null terminator
    char *myString = (char*) malloc(25 * sizeof(char));
    strcpy(myString, "some text"); // fill the new memory
    fgets(myString, 25, stdin); // fill with keyboard input
    

    Note that sizeof(char) is unnecessary here, since a char is always defined to be exactly 1 byte. However, it’s a good habit to get into for when you’re using other data types, and it helps make your code self-documenting by making your intentions very clear.

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

Sidebar

Related Questions

Does anyone know if it is possible to create a single connection string that
Is it possible to create types like e.g. String(20) in scala? The aim would
Using C#, is it possible to create an IPrincipal User from a string username?
Possible Duplicate: Splitting a string in C++ Im trying to create a function that
I am wondering, if it is possible to create variables and name the using
Is it possible to create a SQL Server function which returns a nullable string?
is it possible to create an anonymous method in c# from a string? e.g.
Possible Duplicate: How do I create a random alpha-numeric string in C++? I need
I have to create a C# program that deals well with reading in huge
I have a string variable that contains the entire HTML of a web page.

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.