如何循环char * char? C ++

时间:2015-05-26 10:47:43

标签: c++ char

基本上我想知道如何在for循环中连接char *,并返回一个char *,它是deque中所有char *的concat。返回char *,不是 const char * 字符串非常重要。

我试过这个:

#include <iostream>
#include <deque>
#include <stdio.h>
#include <string.h>
using namespace std;

int main()
{
    deque <char*> q;
    q.push_back("hello");
    q.push_back("world");
    char* answer = (char*)malloc(10);
    while (!q.empty())
    {
        strcat(answer, q.front());
        q.pop_front();
    }
    cout << answer<<endl;
    return 0;
}

输出真的是我想要的“helloworld”,但我得到了这个:

main.cpp:12:23: warning: deprecated conversion from string constant to 'std::deque<char*>::value_type {aka char*}' [-Wwrite-strings]                                         
q.push_back("world"); 

我怎样摆脱这个警告?我找到的每个解决方案都告诉我在char *之前加上“const”但是我必须返回char *。 TNX!

1 个答案:

答案 0 :(得分:1)

要摆脱警告并正确使用$username = filter_var($_POST['username'] , FILTER_SANITIZE_STRING); $password = filter_var($_POST['password'],FILTER_SANITIZE_STRING); // i assume the connection to the database has been established already $check = mysqli_query($con , "SELECT passwordtable FROM tablename WHERE usertable=$username") ; if(mysqli_num_rows($check) === 1){ //fetch the assoc data,would skip that //since the data has been fetched,we can now use the password_verify function,assuming you saved the fetched data in a variable called $dbPass if(password_verify($password , $dbPass)){ //the function takes in two parameters, the first being the inputted pass from your form and the second the hashed password from the database header('Location: dictionary.php'); exit(); } else { echo 'Invalid password' ; } } ,您应该像这样修改代码:

strcat()

#include <iostream> #include <deque> #include <string.h> int main() { std::deque <const char*> q; // ^^^^^ q.push_back("hello"); q.push_back("world"); char* answer = (char*)malloc(11); // ^^ preserve enough space to hold the // terminating `\0` character added // by strcat() answer[0] = 0; // << set the initial '\0' character while (!q.empty()) { strcat(answer, q.front()); q.pop_front(); } std::cout << answer<< std::endl; return 0; } 可以按照您在问题中的要求保持声明为answer