pointer to int array question 
Author Message
 pointer to int array question

Hi, all:
  am i right here ? It certainly can run.

  /*** code start ***/
  int *arr; int i;  

  arr = (int *) malloc (5*sizeof (int));
  assert (arr != NULL);
  for (i=0; i<5; i++) {
     arr[i]= i*i;
     printf ("arr[%d]->%d\n",i,arr[i]);
  /*** code end ***/


P.S. in my previous posting about malloc syntex, char ***3d should be
     char *** g3d; sorry for the typo.

Sat, 27 Apr 2002 03:00:00 GMT  
 pointer to int array question


> Hi, all:
>   am i right here ? It certainly can run.

Did you expect it not to run?

>   /*** code start ***/
>   int *arr; int i;  

>   arr = (int *) malloc (5*sizeof (int));

The comp.lang.c approved method is
arr = malloc(5 * sizeof *arr);

>   assert (arr != NULL);

A simple if would suffice.

>   for (i=0; i<5; i++) {
>      arr[i]= i*i;
>      printf ("arr[%d]->%d\n",i,arr[i]);
>   }



The important thing is not to stop questioning.

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+

Sat, 27 Apr 2002 03:00:00 GMT  
 pointer to int array question
Tong Zhai a crit dans le message ...
>  arr = (int *) malloc (5*sizeof (int));

No cast need, malloc() returns a void *.

>  assert (arr != NULL);

assert() supplies a wonderful tool for debugging, because it forces you to
correct the code that is not running. But assert() should be disabled at
validation and release time (by activating the NDEBUG macro). The abort()
function (that is called by the assert() macro) shoul be used in extrem
emergency only. Good protective test code has to be written to prevent the
use of say a NULL pointer by example. The protection will prevent illegal
code to be executed, and will gently warn the user that he is doing
something wrong, or that no more ressource like memory is available.

Don't forget that there is no guarantee that an abort() will return the
allocated memory to the system or close the open files. It can have
catastrophic results. (I can tell you that my MS-DOS window open under Win
98 has been suddenly closed several times a day during the debug of by
current project)

I have a lot a assert() that are active in the code that I write and test on
my PC, but when the code is downloaded to some embedded system (sort of
networking electronic device to be specific), it is first recompiled with
the NDEBUG macro defined, and the assert() macros are even not compiled at
all. (I have no stderr on my electronic card !)

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
About gets() and fflush(stdin) :
- "Even Buffy cannot slay the resulting RhinoDaemons."
Martin Ambuhl clc.

Sat, 27 Apr 2002 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. int pointer to int array

2. how to modify a pointer to an array one sizeof(int)

3. Sorting array of pointers to int

4. passing of pointers to int arrays?

5. Returning Int array pointers from functions

6. initializing array of pointers to int

7. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

8. Array of pointers, pointer to array...

9. array pointer/pointer array

10. arrays pointers array of pointers

11. int to int array problem

12. Pointer of Pointers was Pointer of arrays...


Powered by phpBB® Forum Software