question about arrays

M

mdh

Hi Group,

This is part of a program to convert Upper to lower case:


mstring[]=SDJDASjdsaldjSFDJDF;
printf ( " The lower case equivalents of this mixed string %s is %s",
mstring, atolc(mstring));

/* atolc is a function which when passed the array, replaces all uppper
case letters with lower case*/


What I am getting is:

sdjdasjdsaldjsfdjdf sdjdasjdsaldjsfdjdf

but I expected to get:

SDJDASjdsaldjSFDJDF sdjdasjdsaldjsfdjdf

What I would like to know is this.

Does the above printf function not intially print "SDJDASjdsaldjSFDJDF"
then later, after the string is passed to 'atolc" print the string
"sdjdasjdsaldjsfdjdf". The answer is obviously not, but I would
appreciate it if someone could explain why not.
thanks in advance.
 
T

Thad Smith

mdh said:
mstring[]=SDJDASjdsaldjSFDJDF;

That is not valid syntax for initializing a string.
printf ( " The lower case equivalents of this mixed string %s is %s",
mstring, atolc(mstring));

/* atolc is a function which when passed the array, replaces all uppper
case letters with lower case*/


What I am getting is:

sdjdasjdsaldjsfdjdf sdjdasjdsaldjsfdjdf

but I expected to get:

SDJDASjdsaldjSFDJDF sdjdasjdsaldjsfdjdf
Does the above printf function not intially print "SDJDASjdsaldjSFDJDF"
then later, after the string is passed to 'atolc" print the string
"sdjdasjdsaldjsfdjdf". The answer is obviously not, but I would
appreciate it if someone could explain why not.
thanks in advance.

All parameters to a function are evaluated before calling the function.
In your case, the value of the parameter is a pointer to the string.
Although you did not show your code for atolc(), you are obviously
converting the string in-place, as opposed to converting to a separate
array, then returning the address of the original array. You are
passing printf two copies of the same address.

If you want to print the array before calling atolc(), you can do that
by splitting the printf() into two statements:

printf ( " The lower case equivalents of this mixed string %s",
mstring);
printf ( " is %s", atolc(mstring));
 
M

mdh

Sorry, Just left out the " " for the sake of the example.

All parameters to a function are evaluated before calling the function.

ok...If I understand what you are saying .... atolc() is evaluated.
Only then is the printf function called, which then obviously is
presented the same converted string twice. I see! :)

You are
passing printf two copies of the same address.

yep...I understand now.
If you want to print the array before calling atolc(), you can do that
by splitting the printf() into two statements:


Thank you.
 
C

CBFalconer

mdh said:
Thad Smith wrote:
.... snip ...


ok...If I understand what you are saying .... atolc() is evaluated.
Only then is the printf function called, which then obviously is
presented the same converted string twice. I see! :)


yep...I understand now.

The root cause is the poor design of atolc(), which should never
return the string pointer and thus encourage that sort of misuse.
Instead have it return something useful, such as the string length,
or even void.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
M

mdh

CBFalconer said:
The root cause is the poor design of atolc(), which should never
return the string pointer and thus encourage that sort of misuse.


I will bear this in mind...right now am slowly working my way through
K&R, so this "misuse" is all part of learning for me.
Tks for the advice.
 
M

mdh

CBFalconer said:
The root cause is the poor design of atolc(), which should never
return the string pointer and thus encourage that sort of misuse.


I will bear this in mind...right now am slowly working my way through
K&R, so this "misuse" is all part of learning for me.
Tks for the advice.
 

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
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top