为什么我的程序无法编译?

Why is my program not compiling?

我的代码应该初始化你键入的任何单词,但它拒绝编译 . 我不明白它给我的错误信息。

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;         

           }
     }


}

printf 需要类型为 const char* 的格式字符串作为其第一个参数,因此:

改变

printf(toupper(s[i])); 

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

正如@Matt McNabb 指出的那样,strncmp 也有类似的问题。因为您倾向于只比较第一个字符,所以您应该更改

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

if (s[i] == ' ')

让它更清晰有效。

这是代码,使其可移植后

但不处理用户输入错误

它有必要的更正,因此可以干净地编译

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


void initialize(char * s);

int main( void )
{
     printf("May I have your name?");
     char name[50] = {'[=10=]'};
     fgets(name, sizeof(name), stdin ); // should check returned value
                                        // to assure the line input was successful

     initialize(name);
     return 0;
} // end function: main


void initialize(char * s)
{
    int space = 1;

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

        if( ' ' == s[i] ) // always place literal on left so compiler
                          // catches any place where '=' was used
                          // when it should have been '=='
        {
            space += 1;
        }
    }
} // end function: initialize