C - Array in an array

I want to have a static array with arrays in it. I know you can make a normal array like this:

int test[] = {1,2,3,4};

But I want to do something like this (Xcode gives me a bunch of warnings and stuff):

int test[] = {{1,2}, {3,4}};

In python it would be:

arr = [[1,2], [3,4]];

What's the proper way to do this?

Answers


To have a multidimensional array, you'd need two levels of arrays:

int test[][] = {{1,2}, {3,4}};

However, that will not work, as you need to declare the size of the inner-most arrays except the last one:

int test[2][] = {{1,2}, {3,4}};

Or if you need an even stricter type safety:

int test[2][2] = {{1,2}, {3,4}};

You can use typedef like this

typedef int data[2];
data arr[] = {{1,2},{3,4}};

This approach may be clearer if you use a "good" name for the type definition


You need array of arrays or 2 dimensional arrays :

int test[][] = {{1,2}, {3,4}};

EDIT : I am out of touch with C. This is almost correct but not entirely as shown by LiraNuna.


Need Your Help

g++ -static causes memory leak (reported by mtrace)

c++ memory-leaks g++ static-libraries mtrace

So I'm having a strange problem that I'm hoping someone can shed some light on... I have the following code:

NSWindow Titlebar with close button

objective-c interface-builder nswindow nswindowcontroller

I have created a window using Interface builder.By default window having title bar with close,minimise and maximise buttons.I can hide minimise and maximise button using IB attributes inspector to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.