Re: Need help with C Language



hey ,

Thanks for all yr help .The actual piece ofcode is below:_ I was
wondering if someone could explain whats going on in the code below

int do_mval=0;

if( mask_dset == NULL ){
mmm = (byte *) malloc( sizeof(byte) * nvox ) ;
if( mmm == NULL )
return " \n*** Can't malloc workspace! ***\n" ;
memset( mmm , 1, nvox ) ; mcount = nvox ;
} else {

mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top ) ;
if( mmm == NULL )
return " \n*** Can't make mask for some reason! ***\n" ;
mcount = THD_countmask( nvox , mmm ) ;


/*-- allocate an array to histogrammatize --*/

flim = mri_new( mcount , 1 , MRI_float ) ;
flar = MRI_FLOAT_PTR(flim) ;

/*-- load values into this array --*/

switch( DSET_BRICK_TYPE(input_dset,iv) ){
default:
free(mmm) ; mri_free(flim) ;
return "*** Can't use source dataset -- illegal data type!
***" ;

case MRI_short:{
short * bar = (short *) DSET_ARRAY(input_dset,iv) ;
float mfac = DSET_BRICK_FACTOR(input_dset,iv) ;
if( mfac == 0.0 ) mfac = 1.0 ;
if( do_mval ){
float val ;
for( ii=jj=0 ; ii < nvox ; ii++ ){
if( mmm[ii] ){
val = mfac*bar[ii] ;
if( val >= val_bot && val <= val_top ) flar[jj++] =
val ;
}
}
mval = jj ;
} else {
for( ii=jj=0 ; ii < nvox ; ii++ )
if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;
}
}


I understand most part of this code..Now the problem is

What exactly is array mmm being assigned to if the maskdatset is not
being assigned to it.
is mmm being assigned to an array of all 1's

if so
could someone explain what exactly is happ in the switch case
statement?

like in case there is no maskdset and mmm array has been set to all 1's
then what exactly does this piece of code do:
I mean is it necessary to put if(mmm[ii] loop before assignment
or is the if statement redudndant?


for( ii=jj=0 ; ii < nvox ; ii++ )
if( mmm[ii] ) flar[jj++] = mfac*bar[ii] ;

Richard Heathfield wrote:
vinod.bhavnani@xxxxxxxxx said:

Hello all,

I have 2 qs about statements and their meanings in the C language.

first If i have an array named

int a[256]

and if i use a staement like

for(i=0;i<256;i++)
If(a[i])

C is a case-sensitive language. The 'if' keyword has a lower case i.

{
}
how will this loop work? when will the IF loop be true and when will it
be false

It depends on the value of a[i]. Since you didn't assign any values, the
value is indeterminate. But let's say you do something like this:

for(i = 0; i < 256; i++)
{
a[i] = i % 2;
}

and then this:

for(i = 0; i < 256; i++)
{
if(a[i])
{
printf("a[i] is true for %d\n", i);
}
}

then it will print:

a[i] is true for 1
a[i] is true for 3
a[i] is true for 5
...
all the way down to
...
a[i] is true for 253
a[i] is true for 255

the next qs is have is

:
if i have a statement array[j++]=3*bar[i];
what exactly is happ to j
does each time the loop increments and a new row of j is beaing
assigned a new value or what is happ

In the example you give,

array[j++] = 3 * bar[i];

can be re-written as:

array[j] = 3 * bar[i];
j = j + 1;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

.



Relevant Pages

  • Re: question about constants
    ... any warning: ... It's the value that was removed from the array and assigned to ... comes as soon as the assignment is not a scalar, ... If you want to loop over a list and "consume" it by the loop you ...
    (perl.beginners)
  • Re: help please
    ... That must be to create an array with the length of "values". ... And here comes a *second* loop, when the first one has finished... ... It simply iterates through the array and prints out the result. ... as the two iterations in the assignment has two different purposes. ...
    (comp.lang.java.help)
  • Re: newbie question
    ... > The second assigns a pointer to an array 1000 of char to an int pointer. ... >> note the printf right after the assignment of input to the first array. ... By the outer loop. ...
    (comp.lang.c)
  • Re: Need help with C Language
    ... You mean "if I have an array named `a'...", ... have `int', spaces or brackets in a identifier name. ... when will the IF loop be true and when will it ... assigned a new value or what is happ ...
    (comp.lang.c)
  • Re: Need help with C Language
    ... You mean "if I have an array named `a'...", ... have `int', spaces or brackets in a identifier name. ... when will the IF loop be true and when will it ... assigned a new value or what is happ ...
    (comp.lang.c)