Need help with C Language

V

vinod.bhavnani

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)
{
}
how will this loop work? when will the IF loop be true and when will it
be false

the next qs is have is

:
if i have a statement array[j++]=3*bar;
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


Thanks,
Vinod
 
C

Clever Monkey

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)
{
}
how will this loop work? when will the IF loop be true and when will it
be false

First, the nit-picking: This snippet will not compile, probably. There
is no standard keyword "If" in standard C.

The answer is whether or not the a expression resolves to nonzero or
not. What is the value contained in a? Only you know at this point.
if i have a statement array[j++]=3*bar;
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

Post-increment. Check the c.l.c. FAQ <http://c-faq.com/>
 
R

Richard Heathfield

(e-mail address removed) 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)


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. 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 % 2;
}

and then this:

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

then it will print:

a is true for 1
a is true for 3
a is true for 5
...
all the way down to
...
a is true for 253
a is true for 255
the next qs is have is

:
if i have a statement array[j++]=3*bar;
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;

can be re-written as:

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

Ronald Bruck

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)
{
}
how will this loop work? when will the IF loop be true and when will it
be false

the next qs is have is

:
if i have a statement array[j++]=3*bar;
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


I am often amazed at reading questions like this. I remember when I
was learning C (and Fortran, and Mathematica, and...) When I had a
question like this, I'd program up a little example, change the
parameters, and see what I got. It may not be what the manual strictly
says it should be, but that's an even MORE informative fact about the
compiler (or other program). You learn best by figuring it out
yourself.

(I particularly remember puzzling through the copy protection in
Visicalc--for my own edification and admiration, not for nefarious
purposes--before realizing it was exploiting a BUG in the 6502
instruction set! What the code loop DID was different from what the
6502 designers promised it WOULD do.)

Has the habit of experimentation died? Has the Internet made us all
lazy?

In this case, of course, he has to put something IN his if... statement
to distinguish what happens. (And I suggest he NOT make it an If
statement, as posted. But he'd probably figure that out real quick.)
As for the j++ question, ... Oh, I give up. Hint: why is j++ called
POST increment? Second hint: it's not a USENET post.
 
V

vinod.bhavnani

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 said:
(e-mail address removed) 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)


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. 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 % 2;
}

and then this:

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

then it will print:

a is true for 1
a is true for 3
a is true for 5
...
all the way down to
...
a is true for 253
a is true for 255
the next qs is have is

:
if i have a statement array[j++]=3*bar;
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;

can be re-written as:

array[j] = 3 * bar;
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)
 
B

Ben Pfaff

Ronald Bruck said:
I am often amazed at reading questions like this. I remember when I
was learning C (and Fortran, and Mathematica, and...) When I had a
question like this, I'd program up a little example, change the
parameters, and see what I got. It may not be what the manual strictly
says it should be, but that's an even MORE informative fact about the
compiler (or other program). You learn best by figuring it out
yourself.

Sometimes experimentation is valuable. Other times, it's just
misleading. What do you learn from an experiment that tests the
behavior of "i = i++"? You learn how that kind of undefined
behavior manifests on your compiler, today.
 
A

Andrew Poelstra

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]

You mean "if I have an array named `a'...", right? Because you can't
have `int', spaces or brackets in a identifier name.
and if i use a staement like

for(i=0;i<256;i++)
If(a)
{
}
how will this loop work? when will the IF loop be true and when will it
be false


Nope. It won't work.
a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
b) What you do have, a "If" construct, isn't in C either.
c) What you /should/ have, a "if statement", is not part of C.
d) If you /did/ have a "if statement", it may never evaluate to false,
because you haven't initialized the array. Reading an uninitialized
array is Undefined Behavior, my friend.
e) Where did you define i? It isn't a char, is it?
the next qs is have is

Please say `question'. It takes two seconds.
:
if i have a statement array[j++]=3*bar;
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


What loop? Where are array, j, bar, and i defined? "happ" is not a word.
 
A

Andrew Poelstra

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

You are abusing the letter m. That's what's going on. ;-)
int do_mval=0;

if( mask_dset == NULL ){
mmm = (byte *) malloc( sizeof(byte) * nvox ) ;
Assume mmm is a pointer, use
mmm = malloc (nvox * sizeof *mmm);
if( mmm == NULL )
return " \n*** Can't malloc workspace! ***\n" ;
Why are you returning a string literal? Is your function defined to
return a string literal? This snippit is not doing what you think it
is. http://www.c-faq.com and buy a copy of _The C Programming Language_
by Dennis Ritchie and Brian Kernighan (sp?).
memset( mmm , 1, nvox ) ; mcount = nvox ;
That 1 would be better as "sizeof *mmm".
} else {

mmm = THD_makemask( mask_dset , miv , mask_bot , mask_top );
When you call functions you haven't defined, we can't tell you what
happens. We aren't psychic.
if( mmm == NULL )
return " \n*** Can't make mask for some reason! ***\n" ;
return sends a value back to the calling function. You know that, right?

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

I don't think you do.
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

I dunno. What do all of your other functions do? What scope is mmm?



Finally, some netiquette:
1) Don't top post!
2) Don't forget to snip signatures.
Thanks for your consideration.
 
A

Andrew Poelstra

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]

You mean "if I have an array named `a'...", right? Because you can't
have `int', spaces or brackets in a identifier name.
and if i use a staement like

for(i=0;i<256;i++)
If(a)
{
}
how will this loop work? when will the IF loop be true and when will it
be false


Nope. It won't work.
a) You don't have an "IF loop" in that code. There's no "IF loop" in C.
b) What you do have, a "If" construct, isn't in C either.
c) What you /should/ have, a "if statement", is not part of C.

Ack! I meant "is not a loop". Sorry for the confusion.
d) If you /did/ have a "if statement", it may never evaluate to false,
because you haven't initialized the array. Reading an uninitialized
array is Undefined Behavior, my friend.
e) Where did you define i? It isn't a char, is it?
the next qs is have is

Please say `question'. It takes two seconds.
:
if i have a statement array[j++]=3*bar;
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


What loop? Where are array, j, bar, and i defined? "happ" is not a word.
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top