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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:51:21+00:00 2026-05-13T12:51:21+00:00

in my phase of (re)learning C, I’m often facing problems with arrays and structures

  • 0

in my phase of (re)learning C, I’m often facing problems with arrays and structures (and pointers). This is a little test I wrote.

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

typedef struct {
    char name[10];
} man;

int main (int argc, const char * argv[]) {
    int i;
    man john;
    strcpy(john.name, "john");

    // array of structures
    man men[10];
    strcpy(men[3].name, "john");
    printf("*** %s ***\n", men[3].name);

    // dynamic array of structures
    man *men2;
    men2 = malloc(10 * sizeof(man));
    strcpy(men2[3].name, "john");
    printf("*** %s ***\n", men2[3].name);

    // multidimensional array of structures
    man men3[10][10];
    strcpy(men3[3][3].name, "john");
    printf("*** %s ***\n", men3[3][3].name);

    // dynamic multidimensional array of structures
    man **men4;
    men4 = malloc(10 * sizeof(man*));
    for (i = 0; i < 10; i++)
        men4[i] = malloc(10 * sizeof(man*));
    strcpy(men4[3][3].name, "john");
    printf("*** %s ***\n", men4[3][3].name);

    // array of pointer to structure
    man *men5[10];
    men5[3] = &john;
    printf("*** %s ***\n", men5[3]->name);

    // dynamic array of pointers to structure
    man **men6;
    men6 = malloc(10 * sizeof(*men6));
    men6[3] = &john;
    printf("*** %s ***\n", men6[3]->name);  

    // dynamic multidimensional array of pointers to structures
    /* ? */

    return 0;
}

Can you:

  1. tell me if all is correct
  2. explain why in men4 i use sizeof(man*) but in men6 it is sizeof(*men6) (asterisk position)
  3. explain how to make a dynamic multidimensional array of pointers to structures (last point)
  4. explain me men4? it works, but is seems to me more of an array of pointers to structure than a multidimensional array of structures!

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-13T12:51:21+00:00Added an answer on May 13, 2026 at 12:51 pm

    First, your these two cases

    // dynamic multidimensional array of structures
    man **men4;
    men4 = malloc(10 * sizeof(man*));
    for (i = 0; i < 10; i++)
        men4[i] = malloc(10 * sizeof(man*));
    

    and

    // dynamic array of pointers to structure
    man **men6;
    men6 = malloc(10 * sizeof(*men6));
    men6[3] = &john;
    printf("*** %s ***\n", men6[3]->name);
    

    are conceptually the same. In both cases, you have a pointer to a pointer to man. You are using those pointers differently. Also, the first one has an error. Instead of:

        men4[i] = malloc(10 * sizeof(man*));
    

    you should have written:

        men4[i] = malloc(10 * sizeof(man));
    

    because each men4[i] is of type “pointer to man“. That error is why I usually prefer my malloc() calls to be of the form:

    a = malloc(N * sizeof *a);
    

    So, your call would have become:

    men4[i] = malloc(10 * sizeof *men4[i]);
    

    This is less error-prone.

    So, the answer to your question (2) is, you should have use sizeof(man *) only when allocating for men4, but not inside the loop. (Or, you now know a better way to call malloc(), so the call outsize the loop becomes men4 = malloc(10 * sizeof *men4);.)

    1. It is all correct, except for the malloc() call as mentioned above.
    2. See above.
    3. You want an array of pointers to struct. To get an array, you need [], since it is a pointer to a pointer that you want (multidimensional), you want two **. So you get: man **(men7[10]);, but since [] binds more tightly than * anyway, you can write it as man **men7[10];.
    4. men4 is not a multidimensional array nor an array of pointers. It is a pointer to a pointer to man. When you allocate memory for men4, you allocate space for 10 pointers to man. Then, each pointer is allocated space for 10 man. The fact that you can index men4 as a multidimensional array is a result of such allocation. In your loop, each men4[i] could have been allocated space for different number of man values if you would have liked, something which is impossible to do with multidimensional arrays.

    Pointers are not arrays. Arrays are not pointers. Please read all the questions and answers in section 6 of the C FAQ for details.

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

Sidebar

Related Questions

I am new to Jquery and in learning phase. I have written a test
I am learning to develop mobile applications, quite in the initial phase. This query
What Tools do you people use to work with Scala? For my learning phase,
I'm in the phase of learning OOP in PHP and i wanna know how
I am trying to learn programming and I am on the phase of learning
I am in the phase of learning Zend Framework for PHP development, I have
I am new in iPhone and in learning phase now a days. Actually i
I'm developing on a pic 18Fxxx (18F452) in assembly. I'm in a learning phase
I am writing my first android app. So am still in the learning phase.
Its my first project with PHP, I am still learning phase. Designer is working

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.