compiling errors

When i compile this code it does create a sprintf.obj and a sprintf.exe files and it works, but it also says:

sprintf.c(19): warning C4047: 'function': 'char *const ' is different from 'char *[50]' in the indirect reference levels
sprintf.c(19): warning C4024: 'sprintf': different types between the formal parameter 1 and the actual one
sprintf.c(20): warning C4047: 'function': 'const char *' is different from 'char *[50]' in the indirect reference levels
sprintf.c(20): warning C4024: 'puts': different types between the formal parameter 1 and the actual one

As i said the program works fine, but i would like to know what is wrong.
i've used the developer commands prompt by visual studio 2015 to compile the code.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#define MAX 20

char * s_gets(char * st, int n);

int main(void)
{
	char * formal[2 * MAX + 10];
	char first[MAX];
	char last[MAX];
	double prize;

	puts("Enter your first name:");
	s_gets(first, MAX);
	puts("Enter your last name:");
	s_gets(last, MAX);
	puts("Enter your prize money:");
	scanf("%lf", &prize);
	sprintf(formal, "%s, %-15s: %5.2f\n", first, last, prize); //this line
	puts(formal); //this line

	return 0;
}

char * s_gets(char * st, int n)
{
	char * ret_val;
	int i = 0;
	
	ret_val = fgets(st, n, stdin);
	if (ret_val)
	{
	while (st[i] != '\n' && st[i] != '\0')
		i++;
	if (st[i] == '\n')
		st[i] = '\0';
	else
		while (getchar() != '\n')
			continue;
	}
	return ret_val;
}
The program fails to compile for me.

I get errors at lines 19 and 20.
[Error] cannot convert 'char**' to 'char*' for argument '1' to 'int sprintf(char*, const char*, ...)'
[Error] cannot convert 'char**' to 'const char*' for argument '1' to 'int puts(const char*)'

The problem is that the first parameter of sprintf(), and that of puts() should be an array of characters (strictly, it is a pointer to char rather than an array). Your parameter formal is an array of pointers to char.

Line 8 should be:
 
    char formal[2 * MAX + 10];
Last edited on
This solved the problem thanks.

Do you know a good paper book for C++? because i ve found "C primer plus" which is considered one of the best for learning the basics of C, but i have no idea how different is C from C++ and since i need to learn C++ i would like to buy a good paper book.
closed account (E0p9LyTq)
You can't go wrong with a book by the guy who created C++:

Programming: Principles and Practice Using C++ (2nd Edition) - https://www.amazon.com/gp/product/0321992784/
Topic archived. No new replies allowed.