Passing strings between functions question

P

ptq2238

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
 
R

Richard Tobin

[...]
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
 
R

Richard Heathfield

(e-mail address removed) said:

char isLowercase(char *string1, char *string2)
{

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.
 
T

tinysisi

(e-mail address removed) дµÀ£º
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);
 
P

pete

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 */
 
P

ptq2238

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
 
W

Walter Roberson

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.
 
P

pete

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 */
 
P

pete

pete said:
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"
 
O

Old Wolf

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.
 
B

Barry Schwarz

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
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top