473,320 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

The type of "Point to array"

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

Aug 25 '06 #1
14 2218
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.
Aug 25 '06 #2

Nils O. Selåsdal wrote:
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 reply so quickly

Aug 25 '06 #3
Alex wrote:
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.

--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
Aug 25 '06 #4

Nils O. Selåsdal wrote:
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

Aug 25 '06 #5
Alex wrote:
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 ;)

--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
Aug 25 '06 #6
Alex wrote:
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

Aug 25 '06 #7
Alex schrieb:
Nils O. Selåsdal wrote:
>>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.hthen 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 25 '06 #8

Michael Mair wrote:
Alex schrieb:
Nils O. Selåsdal wrote:
>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.hthen 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!
>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 25 '06 #9
>> 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.

Aug 25 '06 #10
ar****@gmail.com 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.
--
Flash Gordon
Aug 25 '06 #11

Flash Gordon wrote:
ar****@gmail.com 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.
--
Flash Gordon
Sorry for the way I replied last time. I will keep in mind, it never
happens in future.
Thank you Flash,

--
rajak ajit

Aug 26 '06 #12
ar****@gmail.com wrote:
Flash Gordon wrote:
ar****@gmail.com wrote, re: casting the return of malloc:
>>>>> 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.
>
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,
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...)

--
Bill Pursell

Aug 26 '06 #13
ar****@gmail.com wrote:

<snip>
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.
--
Flash Gordon
Far from being perfect.
Aug 26 '06 #14
In article <11**********************@75g2000cwc.googlegroups. com>,
Bill Pursell <bi**********@gmail.comwrote:
>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.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Aug 26 '06 #15

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

Similar topics

4
by: Daryl Davis | last post by:
I am having trouble with ReDim (see code below) SaleTable2's structure includes an array for SaleDetailTable2 Dim newsale As New hallsales.SaleTable2 Dim detail As New hallsales.SaleDetailTable2...
2
by: Rick Francis | last post by:
I need help serializing an array without including the array "name". I am writing in C# and using the XmlSerializer to serial classes. I am trying to serialize a class with an array in it like...
5
by: hzgt9b | last post by:
I'm building a dataset that writes out a Point type value. Here's the code that I've got: 1 Dim dsContent As DataSet = New DataSet("content") 2 Dim dtAsset As DataTable = New...
0
by: Andrea | last post by:
My C# application is a drop target for the drop format Shell IDList Array I have some problems actually getting the data. I was using the same code used for the FileDrop data target but I'm...
9
by: Frederick Gotham | last post by:
What are your thoughts on the following code? typedef int Coords; int main() { Coords *const p = new Coords; delete p; }
30
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
4
faugustin
by: faugustin | last post by:
Hi, I have a problem. I have av form with textfields, textareas and a vector type fieldset of checkboxes. Everything but the checkboxes works fine, they result in "array" in the field in the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.