strange behavior with **double

I

iesvs

I got a double ** (double **coef).
And gcc accept the syntax coef[j], it works.
I don't understand why. For me coef[j] is not correct because it's
a double ** and not type[][].
If someone could explain me that.
 
A

Army1987

I got a double ** (double **coef).
And gcc accept the syntax coef[j], it works.
I don't understand why. For me coef[j] is not correct because it's
a double ** and not type[][].
If someone could explain me that.

I don't understand, where do you use that syntax? Please copy and
paste the code. (It is well possible that it is correct.)
 
I

iesvs

I got this structure:

struct matrix
{
int c, r;
double **coef;
};

and this function to access it:

double matrix_get (MATRIX *m, int i, int j)
{
return m->coef[j];
}

It works. But for me it's amazing.
 
R

Richard Tobin

I got this structure:

struct matrix
{
int c, r;
double **coef;
};

and this function to access it:

double matrix_get (MATRIX *m, int i, int j)
{
return m->coef[j];
}

It works. But for me it's amazing.


I think you really need to learn the basics of C. Array subscripting
in C works by converting the array to a pointer (to its first element)
and then adding the subscript and dereferencing. So subscripting is
really a pointer operation, not an array operation.

I could describe it in more detail, but that's what books are for.

-- Richard
 
I

iesvs

I think you really need to learn the basics of C.

HA HA HA. You made me laugh. Why did you say that ? Explain.
Array subscripting
in C works by converting the array to a pointer (to its first element)
and then adding the subscript and dereferencing.

I never use array because one day gcc ask me a cast because I was
assuming that an int[] is an int *, but for it no, it's two different
types.
 
H

Hyuga

I think you really need to learn the basics of C.

HA HA HA. You made me laugh. Why did you say that ? Explain.
Array subscripting
in C works by converting the array to a pointer (to its first element)
and then adding the subscript and dereferencing.

I never use array because one day gcc ask me a cast because I was
assuming that an int[] is an int *, but for it no, it's two different
types.

No offense, but I think you've proven Mr. Tobin's point.

Hyuga
 
I

iesvs

HA HA HA. You made me laugh. Why did you say that ? Explain.
I never use array because one day gcc ask me a cast because I was
assuming that an int[] is an int *, but for it no, it's two different
types.

No offense, but I think you've proven Mr. Tobin's point.

Hyuga

Explain please ? I like to learn things in C.
 
J

Joachim Schmitz

I think you really need to learn the basics of C.
HA HA HA. You made me laugh. Why did you say that ? Explain.
Array subscripting
in C works by converting the array to a pointer (to its first
element)
and then adding the subscript and dereferencing.
I never use array because one day gcc ask me a cast because I was
assuming that an int[] is an int *, but for it no, it's two different
types.

No offense, but I think you've proven Mr. Tobin's point.

Hyuga

Explain please ? I like to learn things in C.
checkout the FAQs at http://c-faq.com/, esp. Chapter 6

Bye, Jojo
 
C

CBFalconer

I got a double ** (double **coef). And gcc accept the syntax
coef[j], it works. I don't understand why. For me coef[j]
is not correct because it's a double ** and not type[][]. If
someone could explain me that.


coef is a pointer to a pointer to double. coef[x] reads the gets
the pointer coef, adds x to it (in units of sizeof *coef) returning
a pointer to double. coef[x][y] does all that, adds y to the
pointer to double, and returns the double to which that points.
All this assumes that the various addresses are in range and
contain what they are reputed to hold.
 
I

iesvs

checkout the FAQs athttp://c-faq.com/, esp. Chapter 6

Very very strange.

//example
#include <stdio.h>
#include <stdlib.h>

void aff (int *a)
{
int i;
for (i = 0; i < 6; i++)
printf ("%d", a);
}

int main (void)
{
int a[6] = {1, 2, 3, 4, 5, 6};
aff (a);
return 0;
}

//end of example

They said that "a" is a direct value but I can use it like it was an
indirect value. It's very strange. And nearly inconsistent. So it's
like gcc transform aff (a) by aff (&a). It's ugly. The arithmetic of
pointer is kicked with that. Not the classical arithmetic, the other
arithmetic (void is 0, void * is 1, void ** is 2 etc., there gcc make
-1 == 0, horrible for a mathematician, I know why I don't use array
now).
 
J

Joachim Schmitz

checkout the FAQs athttp://c-faq.com/, esp. Chapter 6

Very very strange.

//example
#include <stdio.h>
#include <stdlib.h>

void aff (int *a)
{
int i;
for (i = 0; i < 6; i++)
printf ("%d", a);
}

int main (void)
{
int a[6] = {1, 2, 3, 4, 5, 6};
aff (a);
return 0;
}

//end of example

They said that "a" is a direct value but I can use it like it was an
indirect value. It's very strange. And nearly inconsistent. So it's
like gcc transform aff (a) by aff (&a). It's ugly. The arithmetic of
pointer is kicked with that. Not the classical arithmetic, the other
arithmetic (void is 0, void * is 1, void ** is 2 etc., there gcc make
-1 == 0, horrible for a mathematician, I know why I don't use array
now).

I can only repeat myself: read the FAQs. esp. Chapter 6...

Bye, Jojo
 
I

iesvs

I can only repeat myself: read the FAQs. esp. Chapter 6...

I rode the FAQ and understand it, but it's TERRIBLE, confuse a int *
and int, what a terrible concept. I'm disappointed by the C. I thought
it was mathematical consistent. But no.
 
F

Francine.Neary

I rode the FAQ and understand it, but it's TERRIBLE, confuse a int *
and int, what a terrible concept. I'm disappointed by the C. I thought
it was mathematical consistent. But no.

and in another thread...

When I code I don't assume that sizeof (char) == 1 and I'll never
assume it.

and in another thread...

No. I know when I programm if * 2 will be used 10 times or 10000....
I know from the beginning where are the important and no important
part. I don't need a software to say me these kind of things, moreover
there are more dumbs than me (a computer beat myself, ha ha ha).

Perhaps it's time to ask the city watch to step up patrols near the
bridge?
 
J

Joachim Schmitz

I rode the FAQ and understand it, but it's TERRIBLE, confuse a int *
and int, what a terrible concept. I'm disappointed by the C. I thought
it was mathematical consistent. But no.
I didn't ask you to ride the FAQs bit to read them :cool:
And no, an "int *" is not the same as an "int" and the FAQ doesn't claim
that, I'm fairly certain. Read it again.

Bye, Jojo
 
I

iesvs

I didn't ask you to ride the FAQs bit to read them :cool:
And no, an "int *" is not the same as an "int" and the FAQ doesn't claim
that, I'm fairly certain. Read it again.

Ok I past the FAQ :

http://c-faq.com/aryptr/aryptrstring.gif

You see the image where it says that
"a" is a "char"
and
"p" is a "char *"
, OK ? Look scrupulously the picture please.
And after that you explain me why when I got a function like that :
void f (char *)
{
...
}

I could write f (p) like f(a) so it confuse char and char *.
It's a proof.
 
J

Joachim Schmitz

Ok I past the FAQ :

http://c-faq.com/aryptr/aryptrstring.gif

You see the image where it says that
"a" is a "char"
No it's a char []
and
"p" is a "char *"
, OK ? Look scrupulously the picture please.
And after that you explain me why when I got a function like that :
void f (char *)
{
...
}

I could write f (p) like f(a) so it confuse char and char *.
No it tells you that in this context a char[] is the same as a char *

Bye, Jojo
 
I

iesvs

No it's a char []

Ok, but it's a direct value, not an indirect value (p is an indirect
value). And I can use it like an indirect value. For me it's the same
thing than saying 0 = 1.

In the past I thought that a char[] was exactly a char * (the same
thing for the computer), so I find it correct to make f (a) where f
is ... f (char *) and a a char[]. But now no I saw that it's not the
same thing, and f (a) is incorrect for a mathematician (confuse a
direct value and an indirect what a silly idea).
 
D

Default User

No it's a char []

Ok, but it's a direct value, not an indirect value (p is an indirect
value). And I can use it like an indirect value. For me it's the same
thing than saying 0 = 1.

In the past I thought that a char[] was exactly a char * (the same
thing for the computer), so I find it correct to make f (a) where f
is ... f (char *) and a a char[]. But now no I saw that it's not the
same thing, and f (a) is incorrect for a mathematician (confuse a
direct value and an indirect what a silly idea).

1. Get a decent book on C.

2. Read it.





Brian
 
J

J. J. Farrell

No it's a char []

Ok, but it's a direct value, not an indirect value (p is an indirect
value). And I can use it like an indirect value. For me it's the same
thing than saying 0 = 1.

In the past I thought that a char[] was exactly a char * (the same
thing for the computer), so I find it correct to make f (a) where f
is ... f (char *) and a a char[]. But now no I saw that it's not the
same thing, and f (a) is incorrect for a mathematician (confuse a
direct value and an indirect what a silly idea).

C is a relatively simple, logical, and straightforward language. It is
clear that it doesn't suit your way of thinking. I advise you not to
use it, which would also save you making such a fool of yourself in
public.
 
T

Tor Rustad

J. J. Farrell wrote:

[...]

C is a relatively simple, logical, and straightforward language.

"C is quirky, flawed and an enormous success"
Dennis Ritchie

It is
clear that it doesn't suit your way of thinking. I advise you not to
use it, which would also save you making such a fool of yourself in
public.

OP might be young, and without prior knowledge of K&R2, C FAQ and "C
Traps and Pitfalls", there are lots of bad habits outside c.l.c to pick
up. :)
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top