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-10-2005, 03:21 AM   #1
erikash
LQ Newbie
 
Registered: Sep 2005
Location: Israel
Posts: 22

Rep: Reputation: 15
a question regarding typedef


hi, i came across this code:
Code:
#include <stdio.h>

#define N 3
typedef int matrix[N+2][N+2];

void initial_matrix(matrix m) // initiate the matrix
{
	int i,j;
	for (i=0;i<=N+1;i++) 
	{
		for(j=0;j<=N+1;j++) 
		{
		m[i][j] = -1;  //*(&m[0][0]+i*(N+1)+j)
		}
	}
}



void print_matrix(matrix m) // prints the matrix
{
	int i,j;
	for (i=0;i<=N+1;i++) 
		{
			for(j=0;j<=N+1;j++)
				printf("%d ",m[i][j]);
			printf("\n");
		}
}



void main(void) {

matrix mat;
initial_matrix(mat);
print_matrix(mat);
system("pause");

}
guys, could you explain me how exacly the function "initial_matrix(mat)" works? (this code is written in C), why is it affecting the original variable mat?
(and isn't making a copy), what is sent to the function, and how does it receives it?

thanks in advance,
erik
 
Old 11-10-2005, 04:37 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
In C, an array defininition is equivalent to the adress of its first element.
So when you pass an array in parameter, you pass the adress of the first element.

void initial_matrix(int *Matrix)
is the same as your function, you just loose the information of the length.

So you manipulate directly the real matrix, not a copy
 
  


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
typedef defines which type? kpachopoulos Programming 1 11-10-2005 02:46 AM
Can you help me with typedef? brownstone Programming 3 06-11-2004 10:09 AM
Typedef Problem in GCC in Linux ashwinipahuja Programming 4 04-26-2004 03:14 AM
explanation on typedef naren Programming 5 03-09-2004 10:48 PM
sizeof my typedef... jhorvath Programming 7 10-11-2002 05:41 PM

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

All times are GMT -5. The time now is 03:04 AM.

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