解除指向char指针c ++的指针

时间:2016-03-17 04:10:02

标签: c++ pointers dereference strdup

我有以下代码:

<form action="paymentinput.php" method="post" type> 
        <label>
        <input type="radio" name="product" value="E-turbine" />
            <img src="ETURBINE250.jpg" height="200px" width="200px">
        </label>
        <label>
        <input type="radio" name="product" value="Robocat"/>
            <img src="RoboCat.jpg" height="200px" width="200px">
        </label>
        <label>
        <input type="radio" name="product" value="QuantumVenture"/>
            <img src="Quanum%20Venture.jpg" height="200px" width="200px">
        </label>

        <br>

        </center>
        <div name='paymentform'>
        <h3>Contact/Personal Details ;</h3>
            <input type="text" placeholder="First Name" name="firstname" required>
            <input type="text" placeholder="Last name" name="lastname" required><br><br>
            <input type="int" placeholder="Mobile"name="mobile" required>
            <input type="int" placeholder="Home Phone" name="homephone"><br><br>
            <input type="varchar" placeholder="Email" name="email" required>
            <h3>Shipping Details ;</h3>
            <input type="text" placeholder="Address, 1A Example road" size="35" length=100% name="address" required>
            <input type="text" placeholder="Region, Orewa" size="35" length=100% name="region" required><br><br>
            <select id="city" name="city" title="city">
            <option name="0">City</option>
                <option name="City" value="auckland">Auckland</option>
                <option name="City" value="wellington">Wellington</option>
                <option name="City" value="christurch">Christurch</option>
                <option name="City" value="hamilton">Hamilton</option>
                <option name="City" value="tauranga">Tauranga</option>
                <option name="City" value="hastings">Hastings</option>
                <option name="City" value="napier">Napier</option>
                <option name="City" value="dunedin">Dunedin</option>
                <option name="City" value="palmerstonnorth">Palmerston North</option>
                <option name="City" value="nelson">Nelson</option>
                <option name="City" value="newplymouth">New Plymouth</option>
                <option name="City" value="whangarei">Whangarei</option>
                <option name="City" value="invercargill">Invercargill</option>
                <option name="City" value="whanganui">Whanganui</option>
                <option name="City" value="gisborne">Gisborne</option>
                </select>
                <input type="text" placeholder="Post Code, 0931" name="postcode" required>

            <h3>Billing Information ;</h3>
            <input type="text" placeholder="Name On Card" name="nameoncard" required>
            <input type="text" placeholder="Card Number" name="ccnumber" required><Br><br>
            <select id="exMonth" name="month" title="select a month">
            <option name="month">Enter month</option>
                <option name="month" value="January">January</option>
                <option name="month" value="February">February</option>
                <option name="month" value="March">March</option>
                <option name="month" value="April">April</option>
                <option name="month" value="May">May</option>
                <option name="month" value="June">June</option>
                <option name="month" value="July">July</option>
                <option name="month" value="August">August</option>
                <option name="month" value="September">September</option>
                <option name="month" value="October">October</option>
                <option name="month" value="November">November</option>
                <option name="month" value="December">December</option>
            </select>
            <select id="exYear" name="year" title="select a year">
                <option name="year" value="Enter year">Enter year</option>
                <option name="year" value="2016">2016</option>
                <option name="year" value="2017">2017</option>
                <option name="year" value="2018">2018</option>
                <option name="year" value="2019">2019</option>
                <option name="year" value="2020">2020</option>
                <option name="year" value="2021">2021</option>
                <option name="year" value="2022">2022</option>
                <option name="year" value="2023">2023</option>
                <option name="year" value="2024">2024</option>
                <option name="year" value="2025">2025</option>
                <option name="year" value="2026">2026</option>
                <option name="year" value="2027">2027</option>
                <option name="year" value="2028">2028</option>
                <option name="year" value="2029">2029</option>
                <option name="year" value="2030">2030</option>
                <option name="year" value="2031">2031</option>
            </select><br><br>
            <input type="int" placeholder="Security Code" name="code" required><br><Br>
            <input type="submit" name="Submit"></Br></Br>
            </form>`

当我尝试执行它时,我在char **ptr; *ptr=strdup("This is a pointer"); cout<<*ptr<<endl; 指令处得到段错误。如果我改为

cout

一切正常。导致问题的原因是什么?我不是在寻找“使用字符串”类型的解决方案。

1 个答案:

答案 0 :(得分:2)

char **ptr; //I have a pointer to pointer to char
*ptr = ...; //I dereference the pointer I never initialized and, wait...

ptr从未被初始化为指向任何事物。当您取消引用并分配给它时,您将获得未定义的行为。要么在您不需要时使用**指针,要么初始化ptr

char** ptr = new char*(strdup("where did my rubber duck go"));