The type of "Point to array"

A

Alex

I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b ) )
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"
thx
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Alex said:
I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

If you're asking what the type of b is it's an int(*)[100];
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"

That's what you get when you cast to something you should not.
Don't do that.
 
A

Alex

Nils said:
Alex said:
I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

If you're asking what the type of b is it's an int(*)[100];
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"

That's what you get when you cast to something you should not.
Don't do that.

Thanks for your reply so quickly
 
R

Roland Csaszar

Alex said:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b ) )
Here,if I want to convert the type by force, which shall I use?

The correct cast would be (int (*)[100]).

But you should _not_ cast the return value of malloc.
 
A

Alex

Nils said:
Alex said:
I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

If you're asking what the type of b is it's an int(*)[100];
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"

That's what you get when you cast to something you should not.
Don't do that.

Thanks for your quickly reply.
But another question, Why "Don't cast the return value from malloc"?
I just begin to have a job, and I see some code written by colleague
using "cast" in malloc.
appreciate for your reply
 
R

Roland Csaszar

Alex said:
But another question, Why "Don't cast the return value from malloc"?

First, it is not necessary at all (OT: but it is in C++).
Second, if you don't include stdlib.h you (may) mask undefined behaviour, as
malloc defaults to returning an int if the correct prototype isn't
declared.

But his issue _has_ been discussed here before ;)
 
U

Ulrich Eckhardt

Alex said:
But another question, Why "Don't cast the return value from malloc"?

See the FAQ.
I just begin to have a job, and I see some code written by colleague
using "cast" in malloc.

Two reasons occur to me for casting the returnvalue of malloc():
1. You're in fact using C++. Then you should use static_cast<> instead.
2. You don't know any better. There are lots of bad code and bad
programmers out there.

Anyhow, there is eventually no reason for casting the returnvalue of malloc
in C.

Uli
 
M

Michael Mair

Alex said:
Nils said:
Alex said:
I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )

Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

If you're asking what the type of b is it's an int(*)[100];
Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"

That's what you get when you cast to something you should not.
Don't do that.

Thanks for your quickly reply.
But another question, Why "Don't cast the return value from malloc"?
I just begin to have a job, and I see some code written by colleague
using "cast" in malloc.
appreciate for your reply

Because
a) it is not necessary in C:
malloc() returns void * which can be converted into every kind
of object pointer implicitly.
b) it can mask an error:
If you failed to #include <stdlib.h> then you will hopefully
get a warning or error because then your compiler has to assume
that the undeclared function malloc() returns int -- and int has
no automatic conversion to a pointer type.
If you cast, then you tell the compiler to shut up.
The return value of malloc() is taken and interpreted as int
and then converted to an (int **) which is wrong in your case.
Even uglier: There are systems where this process leads not always
to success and not always to abysmal failure, so your programme
fails sometimes but not always. Ideally it works in the debugger
or when you add printf()s to find the error...

If your colleague does it out of habit, then question this habit.
If your colleague does it "so it compiles with a C++ compiler",
then know fear -- either write C++ or C but not both. Semantic
subtleties are out to bite you.


Cheers
Michael
 
A

Alex

Michael said:
Alex said:
Nils said:
Alex wrote:

I saw the topic of "wired code " about "point to array" and know a
little about it. But I am still confused about the question below:
I define a point to array "b"
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )

Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

If you're asking what the type of b is it's an int(*)[100];

Here,if I want to convert the type by force, which shall I use?
I use:
b=(int**)malloc(10*sizeof(*b));
the code can compile, but there is a warring "assignment from
incompatible pointer type"

That's what you get when you cast to something you should not.
Don't do that.

Thanks for your quickly reply.
But another question, Why "Don't cast the return value from malloc"?
I just begin to have a job, and I see some code written by colleague
using "cast" in malloc.
appreciate for your reply

Because
a) it is not necessary in C:
malloc() returns void * which can be converted into every kind
of object pointer implicitly.
b) it can mask an error:
If you failed to #include <stdlib.h> then you will hopefully
get a warning or error because then your compiler has to assume
that the undeclared function malloc() returns int -- and int has
no automatic conversion to a pointer type.
If you cast, then you tell the compiler to shut up.
The return value of malloc() is taken and interpreted as int
and then converted to an (int **) which is wrong in your case.
Even uglier: There are systems where this process leads not always
to success and not always to abysmal failure, so your programme
fails sometimes but not always. Ideally it works in the debugger
or when you add printf()s to find the error...

If your colleague does it out of habit, then question this habit.
If your colleague does it "so it compiles with a C++ compiler",
then know fear -- either write C++ or C but not both. Semantic
subtleties are out to bite you.

I have just seen the FAQ . After your explanation , I think I know a
little.
Thank you for your particular explanation and suggestion!
 
A

arajak

int (*b)[100];
Y it shudnt b cast... ??

1. int (*b)[100];
// Explains b is a pointer that points to an array of
INTEGERS. Thus the malloc function can be used without any warning as :

2. b = (int*[]) malloc(100*sizeof(int));
// argument to the malloc function is size of the array to
which b points to

Hope it clears all confusion.
 
F

Flash Gordon

(e-mail address removed) wrote:

Please don't snip attribution. It is useful to be able to see who wrote
what you are replying to.
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

Y it shudnt b cast... ??

1. int (*b)[100];
// Explains b is a pointer that points to an array of
INTEGERS. Thus the malloc function can be used without any warning as :

2. b = (int*[]) malloc(100*sizeof(int));
// argument to the malloc function is size of the array to
which b points to

Hope it clears all confusion.

That is *extremely* bad advice. If you get a warning without the cast
then you are doing something fundamentally wrong. Either you are
compiling as C++, and that is just as wrong as compiling it as Fortran,
or you have not included stdlib.h

In addition you have changed the amount of memory being allocated quite
a lot which will cause even more problem. The use of sizeof *b was
extremely sensible since it ensures you are allocating memory for the
number and type of objects you intend.
 
A

arajak

Flash said:
(e-mail address removed) wrote:

Please don't snip attribution. It is useful to be able to see who wrote
what you are replying to.
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

Y it shudnt b cast... ??

1. int (*b)[100];
// Explains b is a pointer that points to an array of
INTEGERS. Thus the malloc function can be used without any warning as :

2. b = (int*[]) malloc(100*sizeof(int));
// argument to the malloc function is size of the array to
which b points to

Hope it clears all confusion.

That is *extremely* bad advice. If you get a warning without the cast
then you are doing something fundamentally wrong. Either you are
compiling as C++, and that is just as wrong as compiling it as Fortran,
or you have not included stdlib.h

In addition you have changed the amount of memory being allocated quite
a lot which will cause even more problem. The use of sizeof *b was
extremely sensible since it ensures you are allocating memory for the
number and type of objects you intend.

Sorry for the way I replied last time. I will keep in mind, it never
happens in future.
Thank you Flash,
 
B

Bill Pursell

Flash said:
int (*b)[100];
then I locate the memory to b,using "malloc"
b= (???) malloc( 10*sizeof ( *b )
Don't cast the return value from malloc.
b= malloc( 10*sizeof ( *b ) ;

Y it shudnt b cast... ??

1. int (*b)[100];
// Explains b is a pointer that points to an array of
INTEGERS. Thus the malloc function can be used without any warning as :

2. b = (int*[]) malloc(100*sizeof(int));
// argument to the malloc function is size of the array to
which b points to

If a declaration for malloc is in scope, no warnings
will be generated when used without the cast.
Sorry for the way I replied last time. I will keep in mind, it never
happens in future.
Thank you Flash,

You shouldn't apologize for the manner of your reply, nor
even for being incorrect. I've learned far more by posting
an incorrect response and being subsequently corrected
than I have by any other method. Answering a question
incorrectly is far more likely to generate a flood of
responses than anything except possibly asking
a platform-specific question. (or top-posting or
using those annoying text-message abbreviations...)
 
F

Flash Gordon

(e-mail address removed) wrote:

Sorry for the way I replied last time. I will keep in mind, it never
happens in future.
Thank you Flash,

Making honest mistakes is not a problem. Everyone makes mistakes. Those,
like you, who accept graciously when errors are pointed out will
generally be granted a reasonable amount of leniency and are very
welcome here.

The other thing I would point out, as helpful advice rather than beating
you over the head with a sledge hammer, is that you did not need to
quote the entire message you were responding to. You could have snipped
(deleted) most of it as I have done and still had your reply make sense.

So welcome to the group, and continue learning, including from when
other point out the errors in *my* posts.
 
W

Walter Roberson

Bill Pursell said:
You shouldn't apologize for the manner of your reply, nor
even for being incorrect. I've learned far more by posting
an incorrect response and being subsequently corrected
than I have by any other method. Answering a question
incorrectly is far more likely to generate a flood of
responses than anything except possibly asking
a platform-specific question. (or top-posting or
using those annoying text-message abbreviations...)

But in the meantime someone has read and believed your response,
and some people are going to read it and -not- see the followup corrections
or not pay as much attention to the follow-ups corrections.
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top