LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2005, 01:16 PM   #16
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15

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.

James
 
Old 11-23-2005, 01:20 PM   #17
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 11-23-2005 at 01:22 PM.
 
Old 11-24-2005, 02:50 AM   #18
kalebris
LQ Newbie
 
Registered: Nov 2005
Posts: 15

Rep: Reputation: 0
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
Code:
{
    int array[3][3]={{ , , ,},{ , , , },....};
    ...
    ...
}
is working fine at the beginning of the block, but not in the middle and unfortunately i and j have to be calculable in compile time.
 
Old 11-24-2005, 03:15 AM   #19
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
May be like this
Code:
int m[][3]={{1,2,3},{5,6,7},{8,9,10}};
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
 
Old 11-24-2005, 03:53 AM   #20
kalebris
LQ Newbie
 
Registered: Nov 2005
Posts: 15

Rep: Reputation: 0
sorry my bad. Could you explain shortly why is it working? This pointer stuff is not totaly clear for me.

Last edited by kalebris; 11-24-2005 at 04:02 AM.
 
Old 11-24-2005, 05:37 AM   #21
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
you can only use this syntax for initialisation.
I.e. when you declare the variable.
Not at runtime.
 
Old 03-06-2011, 12:49 PM   #22
sylmarsh
LQ Newbie
 
Registered: Mar 2011
Posts: 1

Rep: Reputation: 0
2-D Array Initializing in C Programming

One can initialize a 2-D integer Array in C as below :

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

Pls. note that in all there are 9 elements (3 rows and 3 cols).

I know this post is quite old but just in case somebody still needs it.
 
Old 03-06-2011, 06:45 PM   #23
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You do realize the original poster could have completed both a bachelor's and a master's in the time that's passed since 2005...
Kevin Barry
 
Old 03-16-2011, 11:43 AM   #24
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with c++ code/pointers/arrays drisay Programming 1 01-28-2005 08:24 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM
Looking for a safe and efficient way to use two dimensional arrays. George2 Programming 6 03-22-2004 02:19 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 05:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 03:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration