format ‘%s’ expects argument of type ‘char*’

I am running a program that initializes a vector full of integers, assigns randomly generated integers to it, does a bubble sort on the integers, and then finds the kth largest integer (determined by the size of the integer divided by two). I have run my program in a Unix shell and everything works except for the finding of the kth largest integer. When I run my program, this is the error that is printed to the console:

"warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}’ [-Wformat=]
printf("Program will now find the %s largest integer:", k);"

I have looked at the cplusplus documentation for "printf" and I am pretty sure I used the right specifier for returning a string (%s), but the compiler says it is expecting the "char*" specifier. Does anyone know why this is happening?

Here is my test1.cpp file:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include "time.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
//#include "pch.h"

void placeRandNumsInVector(std::vector<int> &listOfNums);

void bubbleSortVector(std::vector<int> &listOfNums);

void printVectorValues(std::vector<int> &listOfNums);

std::string determineK(std::vector<int> &listOfNums);

//void findKthLargestValue(std::vector<int> &listOfNums);

int main()
{
	//determines seed based on the time of program execution
	srand( (unsigned int)time(0));

	std::vector<int> listOfNums ( 10 );

	printf( "initial unsorted vector values: \n" );

	placeRandNumsInVector(listOfNums);

	printf( "\n\nProgram will now sort integers in listOfNums from least to greatest \n "  );

	//Sorts integers in listOfNums from least to greatest
	printf ( "\nSorted vector: \n" );

	bubbleSortVector(listOfNums);

	printVectorValues(listOfNums);

	std::string k = determineK(listOfNums);

	printf("Program will now find the %s largest integer:", k);
	
	return 0;

};

void placeRandNumsInVector(std::vector<int> &listOfNums) {
	for (unsigned int i = 0; i < listOfNums.size(); i++) {

		int randomNumber = rand() % 1000 + 1;

		listOfNums[i] = randomNumber;

		printf("%d ", listOfNums[i]);
	}
}

void bubbleSortVector(std::vector<int> &listOfNums) {

	bool whileLoopEnd = false;

	while (whileLoopEnd == false) {

		bool forLoopEnd = true;

		for (unsigned int i = 0; i < listOfNums.size(); i++) {

			//if loop is on final iteration, then listOfNums does not compare its elements to each other
			if (i != (listOfNums.size() - 1)) {
				if (listOfNums[i] > listOfNums[i + 1]) {
					//listOfNums[i] and listOfNums[i + 1] swap values
					int tempVar = (listOfNums)[i + 1];
					listOfNums[i + 1] = (listOfNums)[i];
					listOfNums[i] = tempVar;
					forLoopEnd = false;
				}
			}
			//if no values were swapped, then while loop ends and sorting ends
			else if (i == (listOfNums.size() - 1)) {
				if (forLoopEnd == true) {
					whileLoopEnd = true;
				}
			}

		}
	}
}

void printVectorValues(std::vector<int> &listOfNums) {
	for (unsigned int i = 0; i < listOfNums.size(); i++) {
		printf("%d ",  listOfNums[i]);
	}
}

std::string determineK(std::vector<int> &listOfNums) {
	std::string k = "";
	int kValue = 0;

	kValue = listOfNums.size() / 2;

	if(kValue == 1){
		k = "1st";
	}
	else if(kValue == 2) {
		k = "2nd";
	}
	else if(kValue == 3) {
		k = "3rd";
	}
	else if(kValue >= 4) {
		k = kValue + "th";
	}

	return k;
}
I have looked at the cplusplus documentation for "printf" and I am pretty sure I used the right specifier for returning a string (%s), but the compiler says it is expecting the "char*" specifier. Does anyone know why this is happening?


Well C doesn't know anything about C++ strings why would you expect a C function to know how to print a C++ string?

Your best bet is to stop using the C-stdio function like printf() and scanf() and use C++ streams like cin, and cout instead.

In other words if you want to write a C++ program use C++ methods. If you'd rather write a C program then stick with C features instead of C++ features like std::string.



To print a std::string using printf you can use the c_str() member function which returns a C string (i.e. const char*).

1
2
std::string str = "world";
std::printf("hello %s.\n", str.c_str());
Peter87, that did the trick! Thank you!
Topic archived. No new replies allowed.