This makes sense logically but why doesn't the following modification work( data type changed from char to int )?
Code:
/*
exercise 8.6
Write a program that inputs a line of text with function gets into char array
s[ 100 ]. Output the line in uppercase letters and in lowercase letters.
*/
#include <stdio.h>
#include <ctype.h>
int main()
{
int *sPtr;
int s[ 100 ] = { 0 };
sPtr = &s[ 0 ];
gets( s );
while( *sPtr != '\0' ) {
printf( "%c", toupper( *sPtr ) );
++sPtr;
}
printf( "\n" );
sPtr = &s[ 0 ];
while ( *sPtr != '\0' ) {
printf( "%c", tolower( *sPtr ) );
++sPtr;
}
printf( "\n" );
return 0;
}
Code:
root[~]# cc -Wall -pedantic 8.6.c
8.6.c: In function 'main':
8.6.c:16: warning: passing argument 1 of 'gets' from incompatible pointer type
/tmp/ccTPmiTC.o: In function `main':
8.6.c:(.text+0x42): warning: the `gets' function is dangerous and should not be used.
root[~]# ./a.out
abcd
a
a
root[~]#
I'm guessing it might have something to do with the while condition. An integer can't be equal or not equal to '\0' ?
I know gets isn't safe to use, but it's part of the exercises in my book. I'm using this as an opportunity to learn about data types and functions.