[Webinar] Learn how to a build a cloud-first strategyRegister Now


Multi dimensional array of structs...

Posted on 2009-12-16
Medium Priority
Last Modified: 2012-05-08
I have a struct :

struct Person {
   int empNo;
   unsigned char fname;
   unsigned char lname;

I can declare this :

struct Person *personList;
personList = (struct Person *) malloc( 10 * sizeof(struct Person)));

and then do something like this:
personList[0].empNo = 12;

and so on.

How do I declare/initialize this struct so I can get 100 persons? in other words personList[10][10] ?
Question by:Maarvaadi
LVL 32

Accepted Solution

phoffric earned 2000 total points
ID: 26068340
Note: Your malloc has an imbalance of ().

I'll take you literally and just have a single char for fname and lname. (If you want a string, then use a char* or a char [].)

Well, you are saying slightly different things:
1. Explicit Multidimensional array:

struct Person personList[10][10] =
 { {101, 'f', 'l'},{102,'a','b'},{},...,{110, 'x', 'y'} }, // first 10 records
 { {111, 'w', 's'},{112,'a','b'},{},...,{} }, // 2nd 10

2. Using malloc's as in your example.
See code below:
#include <stdio.h>
#include <stdlib.h>

struct Person {
   int empNo;
   unsigned char fname;
   unsigned char lname;

void main() {
	struct Person *personList[10]; // define an array of 10 pointers to Person
	int i,j;
	char ln = 'a', fn='a';
	// malloc 10 records per pointer
	for( i=0; i<10; i++ ) {
		fn = 'a';
		personList[i] = (struct Person *) malloc( 10 * sizeof(struct Person));

		for( j=0; j<10; j++ )
		{	struct Person tmp = {10*i+j, fn, ln};
			personList[i][j] = tmp; // substitute 'a' and 'b' with proper values

	for( i=0; i<10; i++ ) {
		for( j=0; j<10; j++ ) {
			struct Person tmp = personList[i][j];
			printf("%3d  %c  %c\n", tmp.empNo, tmp.fname, tmp.lname);

Open in new window


Author Closing Comment

ID: 31667102

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question