an array function... - I need help

D

DDP3000

void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[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
 
J

James Kuyper

void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[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.
 
D

DDP3000

void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[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.
 
J

jameskuyper

void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[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?
 
D

DDP3000

(e-mail address removed) wrote:
void F(int **A, int N)
{
int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[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?- 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?
 
J

jameskuyper

#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.
 
D

DDP3000

(e-mail address removed) wrote:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top