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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:26:03+00:00 2026-06-18T10:26:03+00:00

I am working on a project for my CS1 class and I have run

  • 0

I am working on a project for my CS1 class and I have run into something I have never thought of before. I know at all arrays in C are essentially pointer to the first element of an array, and a string is really just a character array. However, for my assignment we have to read in a file, and part of the file is the following:

Brad Tim Rick (more man names separated by spaces)
Lucy Angela Tina (more women names separated by spaces)

This is a short example, but what I have to do is extract the names and store them into two separate arrays, one for men and one for females.

I have never worked with something like this, so of course I am confused. This is what I am trying to do, and of course its not working… oh yeah, and I’m trying to store them in dynamic allocation. The only spec says that the names will never exceed 19 characters (should I say twenty to allow the ‘/0’ at the end of the string to still be there no matter what?) How can I tell the compiler, “hey I want an array of strings, and each string can hold 19 characters + 1 for the “string trailer ‘/0’ “? And then how do I access those through pointers?

char **mens_names, **womens_names;

mens_names = malloc(number_of_couples * sizeof(char[19]));
womens_names = malloc(number_of_couples * sizeof(char[19]));

if(mens_names == NULL){
printf("Malloc failed! Memory could not be allocated to variable mens_names.");
return -1;
}

int i;
for(i = 0; i < number_of_couples; i++){
    fscanf(input_file, "%s", &mens_names[i]);
}


if(womens_names == NULL){
    printf("Malloc failed! Memory could not be allocated to variable womens_names.");
    return -1;
}

for(i = 0; i < number_of_couples; i++){
    fscanf(input_file, "%s", &womens_names[i]);
}

for(i = 0; i < number_of_couples; i++){
    printf("Man: %s ", mens_names[i]);
    printf("Woman: %s\n", womens_names[i]);
}
  • 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-18T10:26:05+00:00Added an answer on June 18, 2026 at 10:26 am

    I know at all arrays in C are essentially pointer to the first element of an array

    Not quite. Arrays and pointers are two different things entirely. Except when it is the operand of the sizeof, _Alignof, or unary & operator, or is a string literal being used to initialize an array in a declaration, an expression of type “N-element array of T” will be converted to an expression of type “pointer to T” and its value will be the address of the first element in the array.

    Given the declaration

    int a[10];
    

    the object that a designates is always and forever a 10-element array of int; however, the expression a may be treated as a pointer to the first element.

    If you know your strings will never be more than 19 characters long (20 elements including the terminator), but don’t know the number of strings ahead of time, you can do something like this:

    char (*mens_names)[20];
    char (*womens_names)[20];
    ...
    mens_names = malloc(number_of_couples * sizeof *mens_names);
    womens_names = malloc(number_of_couples * sizeof *womens_names);
    ...
    fscanf(input_file, "%s", mens_names[i]);
    ...
    free(mens_names);
    free(womens_names);
    

    In this case, we’ve declared mens_names and womens_names as pointers to 20-element arrays of char (the parentheses matter). Thus, sizeof *mens_names is equivalent to sizeof (char [20]).

    You would access each individual character as you would with a regular 2-d array:

    char x = mens_names[i][j];
    

    mens_names[i] implicitly dereferences the mens_names pointer (remember that the expression a[i] is interpreted as *(a + i)).

    This method has a couple of advantages over KBart’s method. First, all the memory is allocated contiguously as a single chunk, which may matter if caching becomes an issue. Secondly, you only need one malloc and one free for each array. Of course, this assumes that the maximum size of each name array is a) fixed and b) known at compile time.

    If you won’t know the size of the name until runtime, and you’re using a C99 compiler or a C2011 compiler that supports variable-length arrays, you can do something like this:

    size_t name_len, number_of_couples;
    // get name_len from the user or input file
    // get number_of_couples
    char (*mens_names)[name_len+1] = malloc(number_of_couples * sizeof *mens_names);
    ...
    

    If you won’t know the size of the name until runtime, and you’re using a compiler that doesn’t support VLAs, then you’ll need to use KBart’s method.

    If you wanted to get really fancy, you could use a single 3-dimensional array instead of two 2-dimensional arrays:

    #define MENS_NAMES 0
    #define WOMENS_NAMES 1
    ...
    char (*all_names)[2][20] = malloc(number_of_couples * sizeof *all_names);
    ...
    fscanf(input_file, "%s", all_names[i][MENS_NAMES]);
    ...
    free(all_names);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a project where I have a C# class library which
I have a working project that Im amending, it crashes after trying to use
I working on project and have problem with threading and update of UI. I
I have a working C2DM project, atm. I have me and my colleagues phones
I have a project working fine under MSVS 2010 SP1. I'm trying to convert
I am working on project and ran into an issue. Now I want to
I am working on project where I have the checkInternet method in one of
I compiled an fine working project (before compiled with 3.1.3) now with 3.2.1 Now
I'm currently editing a working project with some experience on PHP. I know a
I have a working project with Symfony2. One of my bundles works well by

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.