Search code examples
cstringcharstring-literals

Accepting a char in a function that wants a string-literal.


So I think I messed up. I created a function in my program that accepts a string literal for a place holder.

foo("c");


char foo(char *bar){
    //stuff
}

So i went off to work on another part of the program and when I came back to this part of the problem, I need a single char from a 2d char array to go in that place. I wrote the foo function in such a way that It has to be a string. I've tried everything to make it work. Please Help...


Solution

  • If I understand what you're asking, I believe you want to convert a char from a 2d char array to a string to pass to the foo function.

    Solution

    char arr[2][2] = {{'0', '1'},
                      {'2', '3'}};
    char c = arr[0][0]; // grab a char from the 2d array
    char str[2] = "\0"; // gives {'\0', '\0'}
    str[0] = c;         // fill in the first character
    foo(str);