Im having trouble sending a bool type to a function which returns a char string to a string.

I have provided the code below

/*
  Name: bool2.cpp   
  Copyright: 
  Author: Toshkin   
  Date: 06/09/05 21:56
  Description: passing string to boolean function returning boolean to 
*/

#include <iostream>
#include <cstring>
using namespace std;



bool inout(char *string); // function prototype
char outin(char *string,bool *bl);     //
int main()
{
    char str1[5];
    bool b;
    cout <<"Please enter the word \"in\" or \"out\": ";
    cin >>str1;
    /*if (strcmp (str1,"in") == 0) {        // redundant code....
             b = true;
             }
    else {
         b = false;
         }
    */
    b = inout(str1);
    cout << boolalpha << b << endl;
    cout <<"str1: "<<str1<<endl;
    outin(str1,b);
    cout <<"str1: "<<str1<<endl;
    system("PAUSE");
    return 0;
}

bool inout(char *string)                             // this works
{
     
     if (strcmp(string,"in") == 0){
        return true;
        }
     else if (strcmp(string,"out") == 0){
        return false;
          }
     
} // end inout

char outin(char *string,bool *bl)                    //  this doesn't work
{
    
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}

Im using Bloodshed DevC++
Your help will be greatly appreciated.
Cheers Toshkin!!!

Dani AI

Generated

A couple of things are tripping you up here, . The bool is fine; the issues are pointer semantics and const-correctness. Your function takes a bool* but you call it with a bool. If you really want a pointer, you must pass &b and compare *bl (not bl). Also, reassigning the local pointer parameter (string = "in") does not modify the caller’s array; it only changes the local pointer. In standard C++, string literals like "in" and "out" are const char[], so assigning them to a non-const char* also discards constness. Either write into the buffer, or return a string object.

If you want to keep C-style strings and modify the caller’s buffer, pass the capacity and write into it safely. This keeps the API simple and avoids pointer-to-bool confusion.

#include <cstdio>   // std::snprintf

void outin(char* dst, std::size_t cap, bool bl) {
    const char* src = bl ? "in" : "out";
    std::snprintf(dst, cap, "%s", src);  // ensures null-termination
}

// usage:
char str1[5]{};
bool b = /* set from input */;
outin(str1, sizeof str1, b);

If you switch to std::string (as hinted), the intent becomes clearer and you avoid manual buffer management entirely.

#include <string>

std::string outin(bool bl) {
    return bl ? "in" : "out";
}

// usage:
std::string s = outin(b);

Either approach fixes the core problems: pass the bool by value (no pointer needed), do not compare a pointer to 0/1, and write into the destination buffer or return a proper string object. As and pointed out in different ways, the key is to change the contents (or return a new string), not just reassign a local pointer.

Recommended Answers

All 5 Replies

You can't return arrays in C or C++. Nor can you copy arrays using assignment [edit]use strcpy to copy strings[/edit]. Is there some reason you aren't using std::string?

Im having trouble sending a bool type to a function which returns a char string to a string.

I have provided the code below

char outin(char *string,bool *bl)                    //  this doesn't work
{
    
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}

Im using Bloodshed DevC++
Your help will be greatly appreciated.
Cheers Toshkin!!!

since you are passing the string in by pointer and not by value, you can directly alter the string in the function... however you can not simply do string = whatever... you need to do a strcpy(string, whatever) or if you want, use stricpy(string, whatever, length) and then remember to put the null terminator at the end... then your function doesn't need to return anything... both strcpy and stricpy are in <cstring> and you'll need to #include that

also when comparing a bool to a value you can simply write true or false instead of 0 and 1... works better for clarity purposes... makes it easier to read

hope this helps

both strcpy and stricpy are in <cstring>

Not necessarily -- stricpy is not standard.

This will work -- the function must return char* which is a pointer just like the parameter. But there is no point passing the parameter if the function is going to change and return the pointer.

char* outin(char *string,bool *bl)
{
    
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}

This would be more logical. And why are you passing the bool by reference instead of by value? The function isn't changing the value so you might as well pass by value.

char* outin(bool bl)
{
    char* string = 0;
     if (bl==1){
            string = "in";
            }
     else if (bl==0) {
          string = "out";
          }
     return string;
}

Just a quick thanks for all the guys.

Cheers Toshkin!!! :D

PS. Sorry for the late reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.