Zubin Jain Zubin Jain - 4 months ago 18
C Question

Why is my program not compiling?

My code is suppose to initialise any word you type but it is refusing to compile
.
I don't understand the error messages it is giving to me.


1 initialize.c:24:23: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the
address with & [-Werror,-Wint-conversion]

2 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]

3 initialize.c:21:23: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
printf(toupper(s[i]));


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

void initialize(string s);
int main(int argc, string argv[])
{
printf("May I have your name?");
string name = GetString();
initialize(name);

}
void initialize(string s)
{
int space = 1;

for (int i = 0;i < strlen(s); i++)
{ if(space == 1)
{
printf(toupper(s[i]));
space -= 1;
}
if(strncmp(s[i]," ",1 ) )
{

space += 1;

}
}


}

Answer

printf expects a format string with type const char* as its 1st argument, so:

change

printf(toupper(s[i])); 

to

printf("%c", toupper(s[i])); 

And as @Matt McNabb pointed, strncmp has the similar problem here. Because you tend to compare the 1st char only, you chould change

if(strncmp(s[i]," ",1 ) )

to

if (s[i] == ' ')

to make it clearer and more effective.