LinuxQuestions.org
Register a domain and help support LQ
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, 12:50 PM   #1
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Rep: Reputation: 15
Double pointers and two dimensional arrays in C


If a function takes a double pointer like this:

find(int **array){.....}

to call this function I have
Code:
int height = 3;
int width = 3;
int **b;
  int total,i,j;
  b=(int **)malloc(height*(sizeof(int *)));
  for (i=0; i<height; i++){
     b[i]=(int *)malloc(width*sizeof(int));
    b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; // I GET AN ERROR here
    find(b);
  };
when I compile this i get an error at this line "b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; "
How can I fix this?

I'm little confused about the two dimenstional array and double pointers.

Thank you,
James
 
Old 11-23-2005, 12:57 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
try making your array like
Code:
int **array=new (int *)[width]
for(int i=0;i<width;i++)array[i]=new int[height];
 
Old 11-23-2005, 01:03 PM   #3
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
thanks but i get an error at this line

int **array=new (int *)[width];

Last edited by kponenation; 11-23-2005 at 01:10 PM.
 
Old 11-23-2005, 01:19 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
try making width and height const. What the errors are is more important than just the line number. Post them!
 
Old 11-23-2005, 01:24 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
tuxdev please look at the title for this thread. C doesn't have the new operator, this was introduced in C++.
 
Old 11-23-2005, 01:27 PM   #6
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
so i also tried int **array=(int *)[width]; but it still doesn't work.
 
Old 11-23-2005, 01:29 PM   #7
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
and the error i get is " syntax error before '[' token"
 
Old 11-23-2005, 01:35 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by kponenation
so i also tried int **array=(int *)[width]; but it still doesn't work.
if you want to dynamicly create it C you need to use malloc,calloc etc.
I havn't post the answer yet,because im trying to figure it myself, its a long time since i used C.
 
Old 11-23-2005, 01:37 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I haven't tried to compile this, but please correct me if I'm wrong. Your assignment is not doing what you think.
Code:
b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
First of all, index '3' is out of bounds. For a height and width of 3, the row and column indices range in value from 0 to 2.

Second, "b[3][3]" is a reference to a single int. "{{1,1,0},{0,0,1},{1,0,1}}" is a literal 3x3 integer array. You can't store an array in an int.

You'll need to reexamine how you're approaching your data. There looks like some confusion exists on how to deal with pointers, arrays, or both.

Last edited by Dark_Helmet; 11-23-2005 at 02:14 PM.
 
Old 11-23-2005, 01:55 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
that quite true Dark_Helmet
@kponenation there is nothing wrong with your creation of the array
Code:
	int height = 3;
	int width = 3;
	int i;
	int**b;
	b=(int **)malloc( height * ( sizeof(int*) ) );

	for (i=0; i<height; i++)
		{ b[i] = (int*)malloc( width * sizeof(int) ); }
if you hadn't of already created the array, you could create and fill on the fly like so:
Code:
int b[3][3] = { {1,1,0}, {0,0,1}, {1,0,1} };
 
Old 11-23-2005, 01:57 PM   #11
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
i'm pretty sure b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
is correct for

1 1 0
0 0 1
1 0 1
 
Old 11-23-2005, 02:04 PM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by kponenation
i'm pretty sure b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
is correct for

1 1 0
0 0 1
1 0 1
Im afraid its not. you have already created the array like i said, if you hadn't then you could do it by declaring and initialising in the same line like the above post.
what you are trying to do here Dark_Helmet has pointed(haha) out is incorrect.

Last edited by dmail; 11-23-2005 at 02:06 PM.
 
Old 11-23-2005, 02:05 PM   #13
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
then how do i assign values to the 2 dimensional array?

and thanks for your help

Last edited by kponenation; 11-23-2005 at 02:06 PM.
 
Old 11-23-2005, 02:12 PM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
it depends on where the values come from, but in my experience:
b[0][0] = 1;
b[0][1] = 1;
b[0][2] = 0;
if anybody know of another way(not including loops etc) i would like to hear it.

Last edited by dmail; 11-23-2005 at 02:14 PM.
 
Old 11-23-2005, 02:13 PM   #15
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Code:
b[0][0]=1;
b[0][1]=1;
b[0][2]=0;
b[1][0]=0;
...
I don't know of any elegant way of doing it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:24 AM
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 11:00 PM
Looking for a safe and efficient way to use two dimensional arrays. George2 Programming 6 03-22-2004 03:19 AM
help w/ pointers vs arrays PTBmilo Programming 3 04-10-2003 06:13 PM
C Question: arrays and pointers tundra Programming 7 07-19-2002 04:38 AM


All times are GMT -5. The time now is 03:17 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration