By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Passing strings between functions question

P: n/a
Hi,
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STRINGSIZE 10

char isLowercase(char *string, char *string2);

int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];

printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);

isLowercase(string1, string2);

printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}

char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */

int count1 = 0;
int count2 = 0;

while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}
When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void

I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast

So I tried type cast my return string but this didn't help.

I tried running the program and I get the following result.
Please enter a string for checking: QwerT12
isLower String2 = wer
Main String1 = QwerT12

While it does find the correct lower case letters, isLower String2 =
wer, it would appear that the string is not being pased back to main.
Why I don't see a result back in Main, ie "Main String2" at all ?
Thank you.

Pat

Apr 21 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
pt*****@gmail.com said:

<snip>
char isLowercase(char *string1, char *string2)
{
<snip>
return;
}
When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
Why did you think that?
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void

I tried return string2;
Okay.
but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast
char isn't the same as char *.
>
So I tried type cast my return string but this didn't help.
Casting is almost always the wrong solution. The right solution in this
case is to make the function's return type match the type of the thing
you wish to return from it - char *, rather than char.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 21 '07 #2

P: n/a
In article <11*********************@o5g2000hsb.googlegroups.c om>,
<pt*****@gmail.comwrote:

[...]
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}
>When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void
You aren't returning string2. You have "return;" at the end of the
function; to return string2 you would need "return string2;".
>I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast
string2 is a char *. isLowercase is declared like this:
>char isLowercase(char *string1, char *string2)
which says that it returns a char (which is a kind of integer), not
a char *. Change the declaration to "char *isLowercase(..."

Incidentally, "isLowercase" is not a good name for a function that
does this:
>/* copies lowercase elements from string1 to string2. */
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 21 '07 #3

P: n/a

pt*****@gmail.com д
Hi,
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STRINGSIZE 10

char isLowercase(char *string, char *string2);

int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];

printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);

isLowercase(string1, string2);

printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}

char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */

int count1 = 0;
int count2 = 0;

while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}
When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void

I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast

So I tried type cast my return string but this didn't help.

I tried running the program and I get the following result.
Please enter a string for checking: QwerT12
isLower String2 = wer
Main String1 = QwerT12

While it does find the correct lower case letters, isLower String2 =
wer, it would appear that the string is not being pased back to main.
Why I don't see a result back in Main, ie "Main String2" at all ?
Thank you.

Pat
1. char is a small integer
2. string is a pointer of char in compiler's view
3. Fuction isLowercase return a char,that is it returns an integer.
If you return string2, you return a pointer
that is why she gives you the warning;
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer
without a cast

4.The "Main String2" is outputed. It maybe a effect of the IO
buffer. The output will be first stored in a buffer,
when there is enough characters in the buffer or some event
happens ( a '\n' is put in, for example),
the contents will be flushed to screen.

Solution 1:
Just modify the statement :
printf("Main String2 = %s",string2);
to
printf("Main String2 = %s\n",string2);

And you will see it .

Solution 2:
Add statement
getchar();
after
printf("Main String2 = %s",string2);

Apr 21 '07 #4

P: n/a
pt*****@gmail.com wrote:
>
Hi,
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STRINGSIZE 10

char isLowercase(char *string, char *string2);

int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];

printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);

isLowercase(string1, string2);

printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}

char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */

int count1 = 0;
int count2 = 0;

while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}

When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void

I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast

So I tried type cast my return string but this didn't help.

I tried running the program and I get the following result.
Please enter a string for checking: QwerT12
isLower String2 = wer
Main String1 = QwerT12

While it does find the correct lower case letters, isLower String2 =
wer, it would appear that the string is not being pased back to main.
Why I don't see a result back in Main, ie "Main String2" at all ?
Thank you.
/* BEGIN new.c */

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define STRINGSIZE 10

char *copyLowercase(char *string1, char *string2);

int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];

printf("Please enter a string for checking: ");
if (fgets(string1, STRINGSIZE, stdin) != NULL) {
string1[strlen(string1) - 1] = '\0';
printf("Main String1 = %s\n", string1);
printf("Main String2 = %s\n",
copyLowercase(string1, string2));
} else {
puts("fgets returned NULL");
}
return 0;
}
/*
** copies lowercase elements from string1 to string2.
*/
char *copyLowercase(char *string1, char *string2)
{
char *const retstring = string2;

do {
if (islower((unsigned char)*string1)) {
*string2++ = *string1;
}
} while (*string1++ != '\0');
*string2 = '\0';
return retstring;
}

/* END new.c */
--
pete
Apr 21 '07 #5

P: n/a
Thank you for pointing out the errors on my part... guess that's part
of learning.

Now I'm curious ... from my very basic understanding of arrays, I know
that the * operator is "dereferencing a pointer", ie *arraypointer and
you get the value that the pointer is pointing to.

But what does char *function_name mean ?
I can't seem to locate anything on this in my reference book on
functions or arrays.

Pat

Apr 21 '07 #6

P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>,
<pt*****@gmail.comwrote:
>Now I'm curious ... from my very basic understanding of arrays, I know
that the * operator is "dereferencing a pointer", ie *arraypointer and
you get the value that the pointer is pointing to.
>But what does char *function_name mean ?
I can't seem to locate anything on this in my reference book on
functions or arrays.
If you are referring to a function declaration or definition, such
as

char *num2str(int num) { /* some code here */ }

then it means that the function is expected to return a pointer to
a character, and it is exactly the same thing as

char* num2str(int num) { /* some code here */ }

or as

(char *) num2str(int num) { /* some code here */ }

The reading of this is that num2str is a function which takes an int
argument and returns a pointer, and the type of the object pointed
to is char .

Executing a unary * does mean to dereference a pointer, but that's
execution: in a declaration, the * indicates that a pointer is involved.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Apr 21 '07 #7

P: n/a
pt*****@gmail.com wrote:
>
Thank you for pointing out the errors on my part... guess that's part
of learning.

Now I'm curious ... from my very basic understanding of arrays, I know
that the * operator is "dereferencing a pointer", ie *arraypointer and
you get the value that the pointer is pointing to.

But what does char *function_name mean ?
I can't seem to locate anything on this in my reference book on
functions or arrays.
In all but two cases, an expression of function type is implicitly
converted to a pointer to the function.

The two exceptions are:
1 &function_name
2 sizeof function_name

Case 2, is undefined since sizeof only operates on object types.
And so, you see, the only thing that you can do with and expression
of function type, is to derive a pointer from it.

/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
puts ("These lines all mean the same thing");
( &puts)("These lines all mean the same thing");
( *puts)("These lines all mean the same thing");
(*&puts)("These lines all mean the same thing");
(&*puts)("These lines all mean the same thing");
return 0;
}

/* END new.c */

--
pete
Apr 21 '07 #8

P: n/a
pete wrote:
>
pt*****@gmail.com wrote:

Thank you for pointing out the errors on my part... guess that's part
of learning.

Now I'm curious ... from my very basic understanding of arrays, I know
that the * operator is "dereferencing a pointer", ie *arraypointer and
you get the value that the pointer is pointing to.

But what does char *function_name mean ?
I can't seem to locate anything on this in my reference book on
functions or arrays.

In all but two cases, an expression of function type is implicitly
converted to a pointer to the function.

The two exceptions are:
1 &function_name
2 sizeof function_name

Case 2, is undefined since sizeof only operates on object types.
And so, you see, the only thing that you can do with
and expression of function type,
Should be "an expression of function type"
is to derive a pointer from it.
--
pete
Apr 21 '07 #9

P: n/a
On Apr 22, 12:44 am, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
char *num2str(int num) { /* some code here */ }

it is exactly the same thing as

char* num2str(int num) { /* some code here */ }

or as

(char *) num2str(int num) { /* some code here */ }
The latter is a syntax error.

Apr 22 '07 #10

P: n/a
On 21 Apr 2007 03:28:53 -0700, pt*****@gmail.com wrote:
>Hi,
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.
In addition to the other advice you have been given, there is an "off
by 1" error in isLowercase.
>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define STRINGSIZE 10

char isLowercase(char *string, char *string2);

int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];

printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);

isLowercase(string1, string2);

printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}

char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */

int count1 = 0;
int count2 = 0;
You should also set string2[0] to '/0' here to handle the case where
none of the characters in string1 is lower case.
>
while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
Remove the +1.
printf("isLower String2 = %s\n",string2);
return;
}

Remove del for email
Apr 29 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.