ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a 20x20 array that i need to assign values (can't be random values)
that's why i want to know if i could do a[] []={{........}}
but i guess I need to type in 400 values by typing...
Thank you dmail and tuxdev.
If you have any suggestion please reply me.
well as an alternative create and intailise the array in a header file and make it global, you then dont need to pass the address of the array from func to func and neither do you have to create the array using memory funcs.
As far as I know the array[i][j] means *(*array+i)+j correct me if i'm wrong and this is a simple int value. As they told you cannot store a 2 dimensional array in an int. But the
And I think to you Mr.kpone... I gave a long string comparison program. You can very well refer to it on how to dynamically allocate two dimensional arrays
The key to this problem is that C does not have multidimensional arrays, only one dimensional ones. The declaration
int **b;
creates a pointer to an array, each element of which is a pointer to a one dimensional array. This structure could be set up by
b = (int **)malloc(3*sizeof(int *));
for (i=0;i<3;i++) b[i] = (int *)malloc(3*sizeof(int));
Now
b[3][3]=1;
sets the last element of the array. Note that
b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
was wrong on two counts: b[3][3] would be an integer variable if it had existed, which it did not; in any case it would be out of range, since arrays are zero-indexed.
Gnu C has an extension: a declaration like
int b[3][3];
sets up a local 3 by 3 array, which may be initialised by
int b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
but observe that this is an initialisation, performed at compile time, not an assignment. Notice also that both dimensions must be given explicitly. I suspect that b in this case is really an array of 9 integers which is treated as two dimensional, but I cannot be sure. It may be possible to assign (in either method) something like
b[1]={0,0,1};
I cannot say without trying it, not possible on a borrowed MAC without Linux.
There is a fundamental difference between the original C version and the Gnu extension. Malloc creates space on the heap, which is global, whereas the extension allocates local space, scoped to the function in which b is declared.
Last edited by Peverel; 03-16-2011 at 11:54 AM.
Reason: inserting forgotten casts
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.