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

The Archive Base Latest Questions

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

A newbie question: I was practice assignment to a char pointer, but found there

  • 0

A newbie question:

I was practice assignment to a char pointer, but found there was nothing printed out. Here is the code:

#include <stdio.h>

int main (void)
{
    char * option_string = NULL;
    option_string = (char*)malloc(sizeof(5));
    memset(option_string, 0, sizeof(char) * 5);

    int j;
    for ( j = 0; j < 5; j++)
    {
        *option_string++ = 'a';
    }

    printf("print options_string: %s\n", option_string); //!nothing was printed out!
    free(option_string);
    return 0; 
}

Thanks in advance!

  • 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:31:57+00:00Added an answer on May 18, 2026 at 8:31 am

    Your problem is that within the loop, you write *option_string++. This means that once the loop is done, you’re going to be pointing past the end of the string:

    option_string at start
       |
       V
    +----+----+----+----+----+
    |    |    |    |    |    |
    |    |    |    |    |    |
    +----+----+----+----+----+
                                 ^
                                 |
                         option_string at end
    

    Note that this reveals a second problem with your code: strings in C are null-terminated, but this string will eventually contain “aaaaa” and then… who knows? Garbage, most likely, but you can’t tell. You need a six-length string. Fixing the first problem means using simple indexing instead: option_string[j] = 'a'. If you really want the *option_string++ method, you’ll have to save and restore option_string (char * real_option_string = option_string; ... option_string = real_option_string;), but I wouldn’t recommend it. Fixing both of these bugs, and a couple of style things, gives you:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main (void)
    {
        char * option_string = calloc(6, sizeof(char));
    
        int j;
        for ( j = 0; j < 5; j++)
        {
            option_string[j] = 'a';
        }
    
        printf("print options_string: %s\n", option_string);
        free(option_string);
        return 0; 
    }
    

    The other thing I changed was your malloc usage. I feel like calloc is a better stylistic choice here; calloc(count, size) allocates count objects of size size, and zeros them. It’s like malloc(count*size) plus a memset, but feels cleaner to me. You also shouldn’t have a cast on malloc/calloc/etc., generally speaking (it can obscure useful warnings), and you need to allocate six slots, like I said (so you can have the null-terminator, which is the zero-valued character, so we don’t need to set it explicitly). Combine that with the option_string[j] indexing mode, include the missing stdlib.h for calloc (you should have had it for malloc, too), and we’re good to go!

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

Sidebar

Related Questions

This is sort of SQL newbie question, I think, but here goes. I have
Total newbie question but this is driving me mad! I'm trying this: myInt =
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
Pardon the dumb newbie question here; web programming isn't my forte... (blush) I have
I'm sure this is a newbie question, but every time I've compiled/dl'ed a new
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Sorry for the second newbie question, I'm a developer not a sysadmin so this
This is a sql newbie question. Basically, I want an extra column to return
This is a total newbie question, so thanks in advance. I'm trying to get
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my

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.