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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:04:40+00:00 2026-05-20T01:04:40+00:00

I am fairly new to C and am getting stuck with arrays and pointers

  • 0

I am fairly new to C and am getting stuck with arrays and pointers when they refer to strings. I can ask for input of 2 numbers (ints) and then return the one I want (first number or second number) without any issues. But when I request names and try to return them, the program crashes after I enter the first name and not sure why.

In theory I am looking to reserve memory for the first name, and then expand it to include a second name. Can anyone explain why this breaks?

Thanks!

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



void main ()
{
    int NumItems = 0;

    NumItems += 1;
    char* NameList = malloc(sizeof(char[10])*NumItems);
    printf("Please enter name #1: \n");
    scanf("%9s", NameList[0]);
    fpurge(stdin);

    NumItems += 1;
    NameList = realloc(NameList,sizeof(char[10])*NumItems);
    printf("Please enter name #2: \n");
    scanf("%9s", NameList[1]);
    fpurge(stdin);

    printf("The first name is: %s",NameList[0]);
    printf("The second name is: %s",NameList[1]);

    return 0;

}
  • 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-20T01:04:40+00:00Added an answer on May 20, 2026 at 1:04 am

    I think that your problem is in this code:

    scanf("%9s", NameList[0]);
    

    The problem here is that scanf requires that the argument you provide as a location to store the result must be a pointer. If you provide something that isn’t a pointer, scanf will treat it as though it is and essentially write memory to a random location, crashing the program.

    Fixing this requires two steps. First, you’ll want to change your declaration of NameList so that it’s no longer a char *. The reason is that a char * is a single string, whereas you want an array of strings. This would be defined as a char **, a pointer to an array of char *s. This might look like this:

    char** NameList;
    

    Next, you’ll need to allocate storage space for the strings. This is tricky and a bit subtle because you have to do two allocations. First, you need to allocate space for the array itself, which you could do like this:

    NameList = malloc (sizeof(char*) * NumItems);
    

    This allocates an array of pointers to characters, but it doesn’t actually set up the pointers in those arrays to point to valid memory locations. To fix this, you’ll want to then iterate across this array and set all of its elements to be pointers to buffers large enough to hold your strings – in this case, buffers of length 10:

    int i;
    for (i = 0; i < NumItems; ++i)
        NameList[i] = malloc (10); // Space for 10 characters
    

    Now, you can call

    scanf("%9s", NameList[0]);
    

    Because NameList[0] is a char * pointing to the buffer into which the characters should be written.

    One more comment on your code – rather than allocating an array of one element and then reallocating it to an array of two elements later on, consider just allocating all the space up-front. It’s a bit clearer. Also, since you’re now dealing with a buffer of char *s, each of which needs to be initialized to point to its own buffer, if you do incremental allocations you’ll need to be sure to initialize all of the new char *s you allocate to point to a buffer somewhere. If you do this one step at a time there’s a good chance you’ll forget to set up the pointers and cause a crash, whereas if you do it up front there’s no such risk.

    When it comes time to free the dynamically-allocated memory, you’ll need to run the allocation process in reverse, first freeing the dynamically-allocated buffers for the strings, then freeing the top-level buffer. For example:

    for (i = 0; i < NumItems; ++i)
        free (NameList[i]);
    free (NameList);
    

    This is necessary because free doesn’t work recursively. You need to explicitly deallocate all the memory that you allocate.

    Note that you do not write the code like this:

    free (NameList);
    for (i = 0; i < NumItems; ++i)
        free (NameList[i]);
    

    This will cause all sorts of Bad Things at runtime because if you free the top-level array first, then as you try iterating over its contents freeing the pointers, you’ll be reading memory that you no longer own.

    Hope this helps!

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

Sidebar

Related Questions

I am fairly new to WPF and am having trouble getting the DataTemplateKey to
I'm fairly new to WPF and I have some problems getting databinding to work
First off, fairly new to JS but getting better :-) This question is similar
Im fairly new to andorid and getting to grips with the UI elements. I
I'm fairly new to C++ programming and I've been getting an error that I
I am fairly new to Android programming, but I am getting pretty good at
I am fairly new to Core Text but have been getting on well, however
I'm fairly new to Git, and still getting the hang of it. I just
I'm fairly new to this, and don't have anyone else to ask. I'm attempting
I am fairly new with Objective C and getting my feet wet! I have

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.