4 Replies - 617 Views - Last Post: 25 March 2012 - 09:31 PM Rate Topic: -----

#1 masala12x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-March 12

2-d array and function

Posted 24 March 2012 - 02:27 AM

Hi!
I'm new to c language. I'm writing a program to insert the number in 2-d array using function and displaying them. my code is following

code:
#include<stdio.h>
         void insert(int *a);
        void display( int *B)/>;

       int main()
      {
       int b [3][3];
       insert(B)/>;
       display(B)/>;
       return 0;
       }
void insert (int *a)
{
  int i,j;
  for(i=0; i<3; i++)
      {
       for(j=0;j<3; j++)
      {
        a[i][j]=5;
       }
      }
}
void display (int *a)
{
  int i,j;
  for(i=0; i<3; i++)
      {
       for(j=0;j<3; j++)
      {
       printf("%d", a[i][j]);
       }
       }
}



when i compiled it i got following error:
In function ‘main’:
warning: passing argument 1 of ‘insert’ from incompatible pointer type
note: expected ‘int *’ but argument is of type ‘int (*)[3]’
warning: passing argument 1 of ‘insert’ from incompatible pointer type
note: expected ‘int *’ but argument is of type ‘int (*)[3]’
In function ‘insert’:
error: subscripted value is neither array nor pointer
In function ‘display’:
error: subscripted value is neither array nor pointer


after that i changed the proto type of my insert and display function like

void insert(int **a);
void display(int ** a)

and compile it again than i got following error:
warning: passing argument 1 of ‘insert’ from incompatible pointer type
note: expected ‘int **’ but argument is of type ‘int (*)[3]’
warning: passing argument 1 of ‘insert’ from incompatible pointer type
note: expected ‘int **’ but argument is of type ‘int (*)[3]’

I'm very new to c language can you please answer the following question

1: what are the error suggesting or why I'm getting these error ?
2:what is the proper way to do it ?
3: how can we use single pointer to pass the array to function ?
4 why the double pointer is showing warning ?
5 how we can return array from the function ?

please explain me so that I can get this concept bcoz its a very important concept.

This post has been edited by JackOfAllTrades: 24 March 2012 - 03:05 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: 2-d array and function

#2 krizjaz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 99
  • Joined: 07-October 07

Re: 2-d array and function

Posted 24 March 2012 - 03:02 AM

Please use code tags when adding codes in your post. And please arrange your code, add proper indentions so that we can read it properly.

This post has been edited by krizjaz: 24 March 2012 - 03:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: 2-d array and function

Posted 24 March 2012 - 06:13 AM

In the following snippet:
         void insert(int *a);
        void display( int *b );

       int main()
      {
       int b [3][3];
       insert(b );
       display(b );
       return 0;
       }
void insert (int *a)
{


To pass an array into a function you must tell the compiler the size of the array you will be passing into the function. For a multidimensional array you only have to pass in the size of the last element. So for your array the following are legal:

void insert(int a[3][3])
void insert(int a[][3])
void insert(int (*a)[3]) // note the ().


You may want to read the links provided in my tutorial for a more thorough explanation on how to pass variables into functions.

Also please use code tags when posting code and you should find an indentation style you like and use it consistently. That will make your code easier to read.


Jim

This post has been edited by jimblumberg: 24 March 2012 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: 2-d array and function

Posted 24 March 2012 - 06:59 AM

You're thinking that since int a[3] degrades to int *a when passed, then int a[3][3] degrades to int **a. Unfortunately, it just doesn't work that way. Your compiler is telling you: int (*)[3]. Yes, it's messy. Multi dimension arrays are.

However, your functions are expecting exactly 3x3. Magic numbers are bad. Define them up all front and make your life easier. Also, indent consistently!

e.g.
#include<stdio.h>

/* magic numbers are bad.  Name them, then use them. */
#define ROWS 3
#define COLS 3

/* typedef is your friend; make a type */
typedef int Array[ROWS][COLS];

/* now your code */
void insert(Array);
void display(Array);

int main() {
	Array a;
	
	insert(a);
	display(a);
	return 0;
}

void insert(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			a[i][j] = 5;
		}
	}
}

void display(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			printf("%d ", a[i][j]);
		}
		printf("\n"); /* it looks better with this; trust me */
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 masala12x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-March 12

Re: 2-d array and function

Posted 25 March 2012 - 09:31 PM

View Postbaavgai, on 24 March 2012 - 06:59 AM, said:

You're thinking that since int a[3] degrades to int *a when passed, then int a[3][3] degrades to int **a. Unfortunately, it just doesn't work that way. Your compiler is telling you: int (*)[3]. Yes, it's messy. Multi dimension arrays are.

However, your functions are expecting exactly 3x3. Magic numbers are bad. Define them up all front and make your life easier. Also, indent consistently!

e.g.
#include<stdio.h>

/* magic numbers are bad.  Name them, then use them. */
#define ROWS 3
#define COLS 3

/* typedef is your friend; make a type */
typedef int Array[ROWS][COLS];

/* now your code */
void insert(Array);
void display(Array);

int main() {
	Array a;
	
	insert(a);
	display(a);
	return 0;
}

void insert(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			a[i][j] = 5;
		}
	}
}

void display(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			printf("%d ", a[i][j]);
		}
		printf("\n"); /* it looks better with this; trust me */
	}
}



Hope this helps.


View Postmasala12x, on 25 March 2012 - 09:30 PM, said:

View Postbaavgai, on 24 March 2012 - 06:59 AM, said:

You're thinking that since int a[3] degrades to int *a when passed, then int a[3][3] degrades to int **a. Unfortunately, it just doesn't work that way. Your compiler is telling you: int (*)[3]. Yes, it's messy. Multi dimension arrays are.

However, your functions are expecting exactly 3x3. Magic numbers are bad. Define them up all front and make your life easier. Also, indent consistently!

e.g.
#include<stdio.h>

/* magic numbers are bad.  Name them, then use them. */
#define ROWS 3
#define COLS 3

/* typedef is your friend; make a type */
typedef int Array[ROWS][COLS];

/* now your code */
void insert(Array);
void display(Array);

int main() {
	Array a;
	
	insert(a);
	display(a);
	return 0;
}

void insert(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			a[i][j] = 5;
		}
	}
}

void display(Array a) {
	int i, j;
	for(i=0; i<ROWS; i++) {
		for(j=0;j<COLS; j++) {
			printf("%d ", a[i][j]);
		}
		printf("\n"); /* it looks better with this; trust me */
	}
}



Hope this helps.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1