Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm using a library for an Arduino project that has a function I need to call. This function accepts just one parameter of type const char*. Let's call it foo.
I need to pass some int values to foo, so I convert them first using sprintf. So far so good.
The problem comes when I try to fill an array with int values converted to char and then call foo with each of the values in the array.
I hope this explains the problem better:
#include <iostream>
using namespace std;
// This function cannot be modified because
// is a part of a library
void foo(const char *bar){
cout << "Result: " << bar << endl;
}
int main() {
char *values[10]; // My array of values
char tmp[10]; // Temporary buffer for the int > char conversion
for(int i = 0; i < 10; i++){
int samplevalue = i * 2; // Just a sample value, not important
sprintf(tmp, "%d", samplevalue); // Copy the sample value to the temporary buffer
values[i] = tmp; // Assign the value of the temp var to a position in my values array
cout << values[i] << endl;
}
cout << "==============" << endl;
// Here comes the problem:
for(int i = 0; i < 10; i++){
foo(values[i]);
}
return 0;
}
The output of that code is the following:
0
2
4
6
8
10
12
14
16
18
==============
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
Result: 18
As you can see, all of the Result lines are equal to the last value assigned to the tmp var. I guess it's because each of the values in the values[10] array contains a pointer to the tmp variable instead of its actual value.
What I'd like to have is a different number on each Result line, as in the first for loop.
I guess it's pretty obvious that I'm not even near to be a C++ expert and any help will be highly appreciated.
Thanks!
A char * pointer and an array are not a string. Use std::string instead.
#include <iostream>
using namespace std;
// This function cannot be modified because
// is a part of a library
void foo(const char *bar)
{
cout << "Result: " << bar << endl;
}
int main(void)
{
std::string values[10]; // My array of values
char tmp[10]; // Temporary buffer for the int > char conversion
for (int i = 0; i < 10; i++) {
int samplevalue = i * 2; // Just a sample value, not important
sprintf(tmp, "%d", samplevalue); // Copy the sample value to the temporary buffer
values[i] = tmp; // Assign the value of the temp var to a position in my values array
cout << values[i] << endl;
}
cout << "==============" << endl;
// Here comes the problem:
for (int i = 0; i < 10; i++) {
foo(values[i].c_str());
}
return 0;
}
When using an array, all the pointers in your values array point to tmp, you can check that by looping through values and printing the address like this
fprintf(stdout, "%p\n", values[i]);
So because you sprintf() into tmp all the values, the value that will be printed is always the last one, there is no copy implied in
values[i] = tmp;
that just makes values[i] point to tmp, so when you access values[i] you really access tmp.
With std::string copy occurs.
Also, you should probably use a string stream to write numbers into each values[i] directly, because sprintf() is very dangerous.
Or even better use a real c++ solution like this one,
#include <iostream>
#include <vector>
#include <sstream>
// This function cannot be modified because
// is a part of a library
void foo(const char *bar)
{
std::cout << "Result: " << bar << std::endl;
}
int main(void)
{
std::vector<std::string> values;
for (int i = 0; i < 10; i++) {
values.push_back(std::to_string(2 * i));
std::cout << values[i] << std::endl;
}
std::cout << "==============" << std::endl;
for (size_t i = 0; i < values.size(); i++) {
foo(values[i].c_str());
}
return 0;
}
Note that now, you can change the number of elements in values and you can use it as an array if you need to, just read the documentation for std::vector.
Ok, I finally got to get it working. In Arduino strings are declared as String variable;, and the c_str() function converts a string into a const char *, so I convert the int number to String, and then to const char *:
for(int i = 0; i < 10; i++){
String tmp = String(i * 2);
values[i] = tmp.c_str();
}
And that's it! It works now :)
Related
As part of my homework assignment, I have to split a char[] by its indices. So for example, the main function looks like:
int main()
{
char str[] = "A string to be split into given number of parts";
int split_size;
cout << "Enter the size of the part: ";
cin >> split_size;
int size = sizeof(str) / sizeof(str[0]);
SplitString(str, split_size, size);
int wait;
cin >> wait;
return 0;
}
Then using the function SplitString, the first x elements are printed, new line, then the next.
My first idea, was to use two for loops. One loops through the splits (i.e. if there are 4 splits, the range on this loop is 0 to 3), then the second loops through the split itself, iterating over the array elements.
My SplitString() function looks like this:
void SplitString(char str[], int split_size, int size) {
int parts = size / split_size;
for (int i = 0; i < parts; i++) {
for (int j = 0; j < split_size; j++) {
j = split_size * i;
cout << str[j];
}
cout << endl;
}
}
Is there an easier way to do this? I know in Python, you can use the arr[1:] to grab a range of elements from the array. Is there anything similar in C++? Is there some flaw in my logic? Is there something wrong with my code?
cout comes with a write function that takes a pointer and a size argument.
for (int i = 0; i < parts; i++) {
cout.write (str+i*split_size, split_size)
cout << endl;
}
Note that the code above does not check if the string is actually long enough. If the total size is not equal the split_size times a whole number, you will have to add an additional check.
Also, note that this:
int size = sizeof(str) / sizeof(str[0]);
can be written as:
int size = sizeof(str);
instead because the size of a char is always 1.
You can use std::string for this. Alternatively, if your compiler supports C++17, you can use std::string_view as the first argument of SplitString to avoid unnecessary copying.
#include <algorithm>
#include <iostream>
#include <string>
void SplitString(std::string s, std::size_t split_size)
{
while(!s.empty())
{
auto size = std::min(split_size, s.size());
std::cout << s.substr(0, size) << '\n';
s = s.substr(size, std::string::npos);
}
}
int main()
{
char str[] = "A string to be split into given number of parts";
int split_size = 5;
SplitString(str, split_size);
return 0;
}
Live example.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Something wrong with my code, I want to multiply a string of numbers with an array of numbers(same length) and store the product in variable product, then I want to store this product of each column in my string variable (as a new values)
string var1 = "1232253759";
int arr[] = {5,3,7,1,2,8,9,2,2,1};
for(int i = 0; i < var1.size(); i++)
{
for(int n = 0; n < 10; n++)
{
int product = 0;
product = var1[i] * arr[n];
var1[i] = product;
}
}
there is a short output of this result:
245
-33
-231
25
50
400
-1008
32
So if im not mistaken this is what you want right.
where totalProduct will hold the product of every product and arr2 holds your columns. I added resultAsString so you have the result as string
note the var.at(i)-'0' which does the convertion you want or i think you are looking for.
for the conversion from int to string im using
std::stringstream ss;
ss << product;
a less C++ aproach would have been the atoi(product) function. or if using c++ 11 std:to_string(product)
Hope it helps
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string var1 = "1232253759";
int arr[] = {5,3,7,1,2,8,9,2,2,1};
int arr2[var1.size()];
int totalProduct = 0;
std::string resultAsString = "";
for(int i = 0; i < var1.size(); i++)
{
// for(int n = 0; n < 10; n++)
// {
int product = (var1.at(i)-'0') * arr[i];
// std::cout << product << "\n";
arr2[i] = product;
std::stringstream ss;
ss << product;
resultAsString += ss.str();
totalProduct += product;
//}
}
// for (int i = var1.size() - 1; i >= 0; i--)
// std::cout << arr2[i] << " ";
std::cout << resultAsString;
}
var1[i] is a char, not an int. char implicitly cast to int and calculation goes wrong. You need to map char with number to number itself ('1' -> 1, '2' -> 2). To do it subtract value of '0'. I.e. var1[i] - '0' it's number for char
This question already has answers here:
Why is there a compiler error, when declaring an array with size as integer variable?
(3 answers)
Closed 4 years ago.
VC++ is giving an error on below code:
#include <vector>
#include <iostream>
int main()
{
std::vector<int> a;
a.push_back(10);
a.push_back(20);
a.push_back(30);
int arr[a.size()];
std::copy(a.begin(), a.end(), arr);
for(int index = 0 ; index < a.size(); index++)
{
std::cout << " The value is " << arr[index] << std::endl;
}
}
It errors out at the integer array declaration stating that the value of variable 'a' cannot be used as constant?
How can we resolve the issue where my objective is to transfer content of vector into a 'C' style array?
This error is compiler dependent. C++ demands constants in array definitions. Some compilers offer an extension that help to use non-constants while declaring Array. I successfully compiled your code on Xcode 10 (GCC).
For your compiler you can simply add int *arrPtr = a.data(); to get c-style array pointer for given array.
int main()
{
std::vector<int> a;
a.push_back(10);
a.push_back(20);
a.push_back(30);
//int arr[a.size()];
//std::copy(a.begin(), a.end(), arr);
//for(int index = 0 ; index < a.size(); index++)
//{
// std::cout << " The value is " << arr[index] << std::endl;
//}
int *arrPtr = a.data();
for(int index = 0 ; index < a.size(); index++)
std::cout<< " The value is " << arrPtr[index] << std::endl;
for(int index = 0 ; index < a.size(); index++)
{
std::cout<< " The value is " << *arrPtr << std::endl;
arrPtr++;
}
}
What compiler tells you, is that a.size() is not a compile time constant ?. Hence, you cannot declare array like this. You need to call int *arr = new int[a.size()]; and then delete it later.
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 7 years ago.
I'm trying to write a function that casts an array of ints into doubles. It takes in a constant pointer to the original int[] array (to avoid unwanted modifications?) and returns a pointer to an array of casted double[]. However, the code that I wrote doesn't seem to be working. Can anyone point out what's wrong with it?
#include <iostream>
using namespace std;
double* castToDouble(const int *input);
int main(){
int integers[] = {1,2,3,4};
double *doubles = castToDouble(integers);
cout << "numElements in doubles: " << sizeof(doubles)/sizeof(double) << endl;
for(int i = 0; i < sizeof(doubles)/sizeof(double); i++){
cout << doubles[i] << endl;
}
return 0;
}
double* castToDouble(const int *input){
// Obtain the number of elements in input.
int numElements = sizeof(input)/sizeof(int);
double *doubleAry = new double[numElements];
cout << "numElements in input: " << numElements << endl;
for(int i = 0; i < numElements; i++){
doubleAry[i] = static_cast<double>(input[i]);
}
return doubleAry;
}
The output of the program is the following:
numElements in input: 2
numElements in doubles: 1
1
The numElements' calculated seem to be arbitrary too. I'm pretty new to c++ and am unable to pinpoint the problem. Thanks in advance.
As you marked it C++, I thought this might be more idiomatic:
#include <vector>
#include <algorithm>
std::vector<double> CastToDouble(std::vector<int> const & ints)
{
auto doubles = std::vector<double>(ints.size());
std::transform(ints.begin(), ints.end(), doubles.begin(), [](int value) -> double {
return static_cast<double>(value);
});
return doubles;
}
int main(int argc, char* argv[])
{
auto values = std::vector<int>() = {
1, 2, 3, 4
};
auto doubles = CastToDouble(values);
}
#include <iostream>
using namespace std;
int main(){
int findMax(int *);
const int MAX = 100;
int values[MAX];
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
char *helper;
// Clean input array and transfer it to values.
for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
helper = ivals[i * 2];
values[i] = atoi(helper);
}
int mval = findMax(values);
cout << values << endl << mval;
return 0;
}
//Function to find the maximum value in the array
int findMax(int arr[]){
int localmax = 0;
for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){
if(arr[i] > localmax){
localmax = arr[i];
}
}
return localmax;
}
The purpose of this program is for the user to input a space-separated series of values ended by a 0. That array is then to be analyzed to find the max. I figured out how to convert what is originally a char[] into an int[] so that I can use the findMax() function on it without error but the sorting loop seems to have a problem of its own and when "cout << values << endl << mval;" is called, it returns only a memory address instead of what should be a non-spaced sequence of ints. Can anybody explain what I am doing wrong? It seems that I may have made some mistake using the pointers but I cannot figure out what.
Printing values won't print the contents of the array as you expect, it will print the memory location of the first element of the array.
Try something like this instead:
#include <iterator>
#include <algorithm>
// ...
copy(&values[0], &values[MAX], ostream_iterator(cout, " "));
Sorry I can't post actual working code, but your original post is a mess with many syntax and syntactic errors.
EDIT: In the interest of being more complete and more approachable & understandable to beginners, I've written a small program that illustrates 4 ways to accomplish this.
Method 1 uses copy with an ostream_iterator as I've done above.
Method 2 below is probably the most basic & easiest to understand.
Method 3 is a C++0x method. I know the question is tagged C++, but I thought it might be educational to add this.
Method 4 is a C++ approach using a vector and for_each. I've implemented a functor that does the dumping.
Share & Enjoy
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
struct dump_val : public unary_function<int,void>
{
void operator()(int val)
{
cout << val << " ";
}
};
int main(){
int vals[5] = {1,2,3,4,5};
// version 1, using std::copy and ostream_iterator
copy(&vals[0], &vals[5], ostream_iterator<int>(cout, " "));
cout << endl;
// version 2, using a simple hand-written loop
for( size_t i = 0; i < 5; ++i )
cout << vals[i] << " ";
cout << endl;
// version 3, using C++0x lambdas
for_each(&vals[0], &vals[5], [](int val)
{
cout << val << " ";
}
);
cout << endl;
// version 4, with elements in a vector and calling a functor from for_each
vector<int> vals_vec;
vals_vec.push_back(1);
vals_vec.push_back(2);
vals_vec.push_back(3);
vals_vec.push_back(4);
vals_vec.push_back(5);
for_each( vals_vec.begin(), vals_vec.end(), dump_val() );
cout << endl;
}
When you pass around an array of X it's really a pointer to an array of X that you're passing around. So when you pass values to cout it only has the pointer to print out.
You really should look into using some of the standard algorithms to make your life simpler.
For example to print all the elements in an array you can just write
std::copy(values, values+MAX, std::ostream_iterator<int>(std::cout, "\n"));
To find the max element you could just write
int mval = *std::max_element(values, values+MAX);
So your code becomes
#include <iostream>
using namespace std;
int main(){
const int MAX = 100;
int values[MAX];
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
char *helper;
// Clean input array and transfer it to values.
for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
helper = ivals[i * 2];
values[i] = atoi(helper);
}
copy(values, values+MAX, ostream_iterator<int>(cout, "\n"));
cout << *std::max_element(values, values+MAX);
return 0;
}
Doing this removes the need for your findMax method altogether.
I'd also re-write your code so that you use a vector instead of an array. This makes your code even shorter. And you can use stringstream to convert strings to numbers.
Something like this should work and is a lot less code than the original.
int main(){
vector<int> values;
char ivals[256];
// Get the space-separated values from user input.
cin.getline(ivals, 256, '0');
int temp = 0;
stringstream ss(ivals);
//read the next int out of the stream and put it in temp
while(ss >> temp) {
//add temp to the vector of ints
values.push_back(temp);
}
copy(values.begin(), values.end(), ostream_iterator<int>(cout, "\n"));
cout << *std::max_element(values.begin(), values.end());
return 0;
}
Array of int is promoted to a pointer to int when passed to a function. There is no operator << taking ordinary array. If you want to use operator << this way, you need to use std::vector instead.
Note: it is possible technically to distinguish array when passed to a function using template, but this is not implemented for standard operator <<.
for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){
sizeof(arr) here is the size of the pointer to the array. C++ will not pass the actual array, that would be grossly inefficient. You'd typically only get one pass through the loop. Declare your function like this:
int findMax(int* arr, size_t elements) {
//...
}
But, really, use a vector.
Oh, hang on, the question. Loop through the array and print each individual element.