Initialize an "eye" (identity) matrix array in C -


int eye[3][3] = {   { 1,0,0 },   { 0,1,0 },   { 0,0,1 } }; 

is there shorter way initialize it? it's regular there must smarter way initialize it, if it's more 3x3, 10x10 or more.

in can write:

int eye[][3] = { [0][0] = 1, [1][1] = 1, [2][2] = 1 }; 

all other elements zeroed, compiler figures out size of array you. don't skip second size (3).

btw. in code don't have use double braces, fine too:

int eye[3][3] = {   1,0,0,   0,1,0,   1,0,1, }; 

in can leave trailing comma, symmetry , future refactorings

other solutions require write code, may indeed save time/space in file. note way you're splitting declaration , "initialization", in case of e.g. globals can make difference.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -