473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

an array function... - I need help

void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++ )
for(j=0;j<N;j++ )
A[i][j]=((i+j)%2==0)?1 :-1;
}

I have never used such a thing before, so it might be a really stupid
question but I cannot find the answer.

I have that function, but I don't know how to use it.
How should I call this function? How can I pass the first parameter?

Thank you
Oct 24 '08 #1
7 1403
DD*****@gmail.c om wrote:
void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++ )
for(j=0;j<N;j++ )
A[i][j]=((i+j)%2==0)?1 :-1;
}

I have never used such a thing before, so it might be a really stupid
question but I cannot find the answer.

I have that function, but I don't know how to use it.
How should I call this function? How can I pass the first parameter?

Thank you

The simplest way is as follows:

#define DIM 4

int twod[DIM][DIM];
int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};

func(pointers, DIM);

A more typical approach would be to dynamically allocate each element of
pointers[] using malloc(). However, you would still call func() the same
way, no matter how pointers[] is initialized.
Oct 24 '08 #2
On Oct 24, 2:47*pm, James Kuyper <jameskuy...@ve rizon.netwrote:
DDP3...@gmail.c om wrote:
void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++ )
for(j=0;j<N;j++ )
A[i][j]=((i+j)%2==0)?1 :-1;
}
I have never used such a thing before, so it might be a really stupid
question but I cannot find the answer.
I have that function, but I don't know how to use it.
How should I call this function? How can I pass the first parameter?
Thank you

The simplest way is as follows:

* * * * #define DIM 4

* * * * int twod[DIM][DIM];
* * * * int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};

* * * * func(pointers, DIM);

A more typical approach would be to dynamically allocate each element of
pointers[] using malloc(). However, you would still call func() the same
way, no matter how pointers[] is initialized.- Hide quoted text -

- Show quoted text -
Thank you. It worked.
But I have one more question, please.
I ve printed the values and I realised that twod[0] points to the
memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
it where twod[] is declared. Using &twod[][] works as well.
Oct 24 '08 #3
DDP3...@gmail.c om wrote:
On Oct 24, 2:47�pm, James Kuyper <jameskuy...@ve rizon.netwrote:
DDP3...@gmail.c om wrote:
void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++ )
for(j=0;j<N;j++ )
A[i][j]=((i+j)%2==0)?1 :-1;
}
I have never used such a thing before, so it might be a really stupid
question but I cannot find the answer.
I have that function, but I don't know how to use it.
How should I call this function? How can I pass the first parameter?
Thank you
The simplest way is as follows:

� � � � #define DIM 4

� � � � int twod[DIM][DIM];
� � � � int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};

� � � � func(pointers, DIM);

A more typical approach would be to dynamically allocate each element of
pointers[] using malloc(). However, you would still call func() the same
way, no matter how pointers[] is initialized.

Thank you. It worked.
But I have one more question, please.
I ve printed the values and I realised that twod[0] points to the
memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
it where twod[] is declared. Using &twod[][] works as well.
I sometimes use the notation pointers[] when writing about C code in
English, to make it clear that "pointers" is a C array. If that's what
you're doing, too, then twod[] is declared on the line which says:

int twod[DIM][DIM];

That answer seems too obvious, so I suspect that you're really asking
a different question. Could you explain it in more detail?

I'm not sure what you mean by your comment about &twod[][]. There's no
context in which you could legally write that in a C program. However,
&twod[0][1], for instance, is a perfectly valid int* which points at
twod[0][1]. I'm not quite sure what you mean when you say it "works".
How are you using it?
Oct 24 '08 #4
On Oct 24, 8:32*pm, jameskuy...@ver izon.net wrote:
DDP3...@gmail.c om wrote:
On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
DDP3...@gmail.c om wrote:
void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++ )
for(j=0;j<N;j++ )
A[i][j]=((i+j)%2==0)?1 :-1;
}
I have never used such a thing before, so it might be a really stupid
question but I cannot find the answer.
I have that function, but I don't know how to use it.
How should I call this function? How can I pass the first parameter?
Thank you
The simplest way is as follows:
#define DIM 4
int twod[DIM][DIM];
int *pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
func(pointers, DIM);
A more typical approach would be to dynamically allocate each elementof
pointers[] using malloc(). However, you would still call func() the same
way, no matter how pointers[] is initialized.
Thank you. It worked.
But I have one more question, please.
I ve printed the values and I realised that twod[0] points to the
memory address of twod[0][0], twod[1] to twod[1][0] etc. I don't get
it where twod[] is declared. Using &twod[][] works as well.

I sometimes use the notation pointers[] when writing about C code in
English, to make it clear that "pointers" is a C array. If that's what
you're doing, too, then twod[] is declared on the line which says:

* * * * int twod[DIM][DIM];

That answer seems too obvious, so I suspect that you're really asking
a different question. Could you explain it in more detail?

I'm not sure what you mean by your comment about &twod[][]. There's no
context in which you could legally write that in a C program. However,
&twod[0][1], for instance, is a perfectly valid int* which points at
twod[0][1]. I'm not quite sure what you mean when you say it "works".
How are you using it?- Hide quoted text -

- Show quoted text -
-I just omitted the content of the []. Sorry for the trouble.
-I m not very experienced with pointers and arrays and I don't know if
I understand it right.
The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
next line of the array is not a continuation of the first line, it has
a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
points to twod[1][n]. Right?
-Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
[0], &twod[3][0]}; Right?

Oct 24 '08 #5
DDP3...@gmail.c om wrote:
On Oct 24, 8:32�pm, jameskuy...@ver izon.net wrote:
DDP3...@gmail.c om wrote:
On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
....
#define DIM 4
....
� � � � int twod[DIM][DIM];
....
The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
next line of the array is not a continuation of the first line, it has
a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
points to twod[1][n]. Right?
Yes. Keep in mind that the array twod[1] must be come immediately
after twod[0], so it's not a completely different "base" pointer.
Therefore, you might expect that twod[0]+DIM must point at the same
location as twod[1], and on most real systems those are two pointer
which will compare equal and can both be dereferenced to access the
same int object in memory. However, the standard says that
dereferencing twod[0]+DIM has undefined behavior, so you should avoid
writing code which relies upon doing so.
-Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
[0], &twod[3][0]}; Right?
Correct; the meaning is exactly the same, your way just involves more
typing than mine. Some people prefer the extra typing, in the belief
that the meaning is clearer. The shorter method seems clearer to me,
but that may be because I've been programming in C since 1978.

Oct 24 '08 #6
Thank you!
Oct 24 '08 #7
On 24 Ïêô, 23:53, jameskuy...@ver izon.net wrote:
DDP3...@gmail.c om wrote:
On Oct 24, 8:32 pm, jameskuy...@ver izon.net wrote:
DDP3...@gmail.c om wrote:
On Oct 24, 2:47 pm, James Kuyper <jameskuy...@ve rizon.netwrote:
...
#define DIM 4
...
int twod[DIM][DIM];
...
The twod[0] points to twod[0][0] and twod[0]+n to twod[0][n] but the
next line of the array is not a continuation of the first line, it has
a new "base" pointer twod[1] that points to twod[1][0] and twod[1]+n
points to twod[1][n]. Right?

Yes. Keep in mind that the array twod[1] must be come immediately
after twod[0], so it's not a completely different "base" pointer.
Therefore, you might expect that twod[0]+DIM must point at the same
location as twod[1], and on most real systems those are two pointer
which will compare equal and can both be dereferenced to access the
same int object in memory. However, the standard says that
dereferencing twod[0]+DIM has undefined behavior, so you should avoid
writing code which relies upon doing so.
-Instead of int* pointers[DIM] = {twod[0], twod[1], twod[2], twod[3]};
I could use int *pointers[DIM] = {&twod[0][0], &twod[1][0], &twod[2]
[0], &twod[3][0]}; Right?

Correct; the meaning is exactly the same, your way just involves more
typing than mine. Some people prefer the extra typing, in the belief
that the meaning is clearer. The shorter method seems clearer to me,
but that may be because I've been programming in C since 1978.
Thank you
Oct 24 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2112
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in the array is 2, the output should be 2 for the number and 1 for the location. If anyone could help or point me in the right direction that would be great. Thanks. ------------------- #include <iostream>
1
2204
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done -seach for a specific flight number in queue ( didn't get there yet) -move a flight number one one position in the queue to another ( didn't get there yet) this is what i have so far. it runs but something is wrong and i don't know what it is.
7
25175
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
3
2888
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
4
8830
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
8
10722
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
7
3170
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change thorughout the course the programs use so it will need to
23
7425
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
10
5621
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
10
4000
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item in the list A i want to associate an undetermined number of items. The complication for me is the fact that the items to associate in list A are undetermined.
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.