[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: I need help

janus

5/14/2011 1:03:00 PM

Thanks you all.

However, I have additional question ..

Is this acceptable?
char string[] = "Simulated Annealing = 857747716";
char astring[] = "Karmarkar 625874153";

char * value = strchr(astring ,'=');
char * avalue;

if(value != NULL){
*(value ++) = 0;
printf("%s=====%s\n", value, string);
}
else if((value == NULL) && (avalue = strchr(astring, ''))){
*(avalue ++) = 0;
printf("%s====%s\n", avalue, astring);
}

Is the above correct?

Janus
4 Answers

pete

5/14/2011 2:02:00 PM

0

janus wrote:
>
> Thanks you all.
>
> However, I have additional question ..
>
> Is this acceptable?
> char string[] = "Simulated Annealing = 857747716";
> char astring[] = "Karmarkar 625874153";
>
> char * value = strchr(astring ,'=');
> char * avalue;
>
> if(value != NULL){
> *(value ++) = 0;
> printf("%s=====%s\n", value, string);
> }
> else if((value == NULL) && (avalue = strchr(astring, ''))){
> *(avalue ++) = 0;
> printf("%s====%s\n", avalue, astring);
> }
>
> Is the above correct?

It's close.

I think you misspelled (astring, ' ').

And the (value == NULL) expression is pointless,
since you already know that (value == NULL) is true,
if that part of the code is executing.

--
pete

China Blue Veins

5/14/2011 2:03:00 PM

0

In article
<e279cb75-8a72-4ad9-9877-62a9ca1b0dfe@glegroupsg2000goo.googlegroups.com>,
janus <emekamicro@gmail.com> wrote:

> Thanks you all.
>
> However, I have additional question ..
>
> Is this acceptable?
> char string[] = "Simulated Annealing = 857747716";
> char astring[] = "Karmarkar 625874153";
>
> char * value = strchr(astring ,'=');
> char * avalue;
>
> if(value != NULL){
> *(value ++) = 0;

This is safe because value is pointing into a writable char array. It will zero
the '=' is the array string.

> printf("%s=====%s\n", value, string);
> }
> else if((value == NULL) && (avalue = strchr(astring, ''))){
> *(avalue ++) = 0;

This is safe also.

The predicate (value == NULL) will always be true at this point in the
conditional so it is redundant. You can keep it or omit without effecting the
code.

> printf("%s====%s\n", avalue, astring);
> }
>
> Is the above correct?

I do something similar when I'm parsing and editting a string, except it's
char string[strlen(S)+1]; strcpy(string, S);
With a constant initialiser, it has the same effect.

--
Damn the living - It's a lovely life. I'm whoever you want me to be.
Silver silverware - Where is the love? At least I can stay in character.
Oval swimming pool - Where is the love? Annoying Usenet one post at a time.
Damn the living - It's a lovely life. In 1492....

Eric Sosman

5/14/2011 2:19:00 PM

0

On 5/14/2011 9:02 AM, janus wrote:
> Thanks you all.
>
> However, I have additional question ..
>
> Is this acceptable?
> char string[] = "Simulated Annealing = 857747716";
> char astring[] = "Karmarkar 625874153";
>
> char * value = strchr(astring ,'=');
> char * avalue;
>
> if(value != NULL){

Since `value' will be NULL, this part is not executed.

> *(value ++) = 0;
> printf("%s=====%s\n", value, string);
> }
> else if((value == NULL)&& (avalue = strchr(astring, ''))){

This is a syntax error. You probably meant ' ', not ''. (Also,
since "value != NULL" has already been found false, "value == NULL"
is sure to be true. There's no actual harm in making a test that
can only turn out one way, but it's silly.)

> *(avalue ++) = 0;
> printf("%s====%s\n", avalue, astring);

With the correction, this should print " 625874153====Karmarkar".

> }
>
> Is the above correct?

Impossible to say, since you haven't told us what you want
the code to do!

--
Eric Sosman
esosman@ieee-dot-org.invalid

Keith Thompson

5/14/2011 5:59:00 PM

0

Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> On 5/14/2011 9:02 AM, janus wrote:
>> Thanks you all.
>>
>> However, I have additional question ..
>>
>> Is this acceptable?
>> char string[] = "Simulated Annealing = 857747716";
>> char astring[] = "Karmarkar 625874153";
>>
>> char * value = strchr(astring ,'=');
>> char * avalue;
>>
>> if(value != NULL){
>
> Since `value' will be NULL, this part is not executed.

That's true for this program, but this is probably an early
draft of a program that will later get its input from elsewhere.
Checking whether strchr() actually found the character you're
looking for is a good habit.

[snip]

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"