You are here:

C/array declaration

Advertisement


Question
how to declare a two dimesional array dynamicaly

Answer
You can use double pointer and then allocate memory.
After this use it as you use a 2-dim array.

char **arr;
arr = (char **) malloc(3);
arr[0] = (char *) malloc(100)
arr[1] = (char *) malloc(100)
arr[2] = (char *) malloc(100)

So, this can be used as a 3X100 array, which is created during runtime.

But, be careful while using this.
Because, array and pointers are not same.
You will have to understand the difference and then use it.
Otherwise, it will not work as you expect it to.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.