Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #3950 > unrolled thread
| Started by | janus <emekamicro@gmail.com> |
|---|---|
| First post | 2011-05-14 06:02 -0700 |
| Last post | 2011-05-14 10:59 -0700 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.c
Re: I need help janus <emekamicro@gmail.com> - 2011-05-14 06:02 -0700
Re: I need help pete <pfiland@mindspring.com> - 2011-05-14 10:01 -0400
Re: I need help Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> - 2011-05-14 07:03 -0700
Re: I need help Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-05-14 10:19 -0400
Re: I need help Keith Thompson <kst-u@mib.org> - 2011-05-14 10:59 -0700
| From | janus <emekamicro@gmail.com> |
|---|---|
| Date | 2011-05-14 06:02 -0700 |
| Subject | Re: I need help |
| Message-ID | <e279cb75-8a72-4ad9-9877-62a9ca1b0dfe@glegroupsg2000goo.googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-05-14 10:01 -0400 |
| Message-ID | <4DCE8B46.A58@mindspring.com> |
| In reply to | #3950 |
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
[toc] | [prev] | [next] | [standalone]
| From | Columbus sailed the ocean China Blue <chine.bleu@yahoo.com> |
|---|---|
| Date | 2011-05-14 07:03 -0700 |
| Message-ID | <chine.bleu-99230C.07031714052011@reserved-multicast-range-not-delegated.example.com> |
| In reply to | #3950 |
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....
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-05-14 10:19 -0400 |
| Message-ID | <iqm332$vm8$1@dont-email.me> |
| In reply to | #3950 |
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
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-05-14 10:59 -0700 |
| Message-ID | <lnwrhtgo45.fsf@nuthaus.mib.org> |
| In reply to | #3955 |
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.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web