444,077 Members | 2,121 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

# The type of "Point to array"

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a Alex schrieb: Nils O. Selåsdal wrote: >>Alex wrote: >>>I saw the topic of "wired code " about "point to array" and know alittle 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 fromincompatible 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

 P: n/a 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 alittle 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 fromincompatible 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 Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. Aug 25 '06 #9

 P: n/a >> 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a ar****@gmail.com 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. -- Flash Gordon Far from being perfect. Aug 26 '06 #14

 P: n/a In article <11**********************@75g2000cwc.googlegroups. com>, Bill Pursell You shouldn't apologize for the manner of your reply, noreven for being incorrect. I've learned far more by postingan incorrect response and being subsequently correctedthan I have by any other method. Answering a questionincorrectly is far more likely to generate a flood ofresponses than anything except possibly askinga platform-specific question. (or top-posting orusing 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 discussion thread is closed

Replies have been disabled for this discussion.