为什么*(a + b)在C而不是[b]中工作?

时间:2015-11-12 21:10:02

标签: c arrays

我已经在C99标准中读到了扩展阵列的方法,但是我无法理解为什么会这样做。如果我有

a[5]

和a是一个整数,那么不会访问第二个元素而不是第五个元素(假设整数是四个字节)?不应该扩展到

*(a+5*sizeof(a))

因为每个字节都有自己的地址吗?

4 个答案:

答案 0 :(得分:9)

指针算法基于指向类型的大小;如果您有p类型的指针T,那么p+1会为您提供该类型的下一个实例的地址。所以,给出如下内容:

char *pc   = 0x1000;
int  *ic   = 0x1000;
double *dc = 0x1000;

pc + 1提供下一个char对象的地址(位于0x1001),ic + 1给出下一个int对象的地址(对于四字节0x1004而言,intdc + 1double给出下一个0x1008对象的地址(doublea[i]一个8字节*(a + i))。

这就是i 定义i的原因 - 您正在访问$(document).ready(function () { var validator = $("#adoption-form").bootstrapValidator({ //live: 'disabled', feedbackIcons: { valid: "glyphicon glyphicon-ok", invalid: "glyphicon glyphicon-remove", validating: "glyphicon glyphicon-refresh" }, fields : { name :{ validators : { notEmpty : { message : "Please provide your name." }, stringLength: { min : 4, max: 35, message: "Name must be between 4 and 35 characters long" }, }//end validators }, email :{ validators : { notEmpty : { message : "Please provide an email address" }, regexp: { regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$', message: 'The value is not a valid email address' } }//end validators }, //LOTS MORE VALIDATORS IN HERE } //end ALL validators }); validator.on("success.form.bv", function (e) { if (data.fv.getInvalidFields().length > 0) { // There is invalid field data.fv.disableSubmitButtons(true); } e.preventDefault(); $("#adoption-form").addClass("hidden"); $("#confirmation").removeClass("hidden"); var $form = $(e.target), fv = $form.data('bootstrapValidator'); }); //process the form $("#adoption-form").submit(function(event) { // get the form data // there are many ways to get this data using jQuery (you can use the class or id also) var formData = { 'name' : $('input[name=name]').val(), 'email' : $('input[name=email]').val(), 'address' : $('input[name=address]').val(), 'city' : $('input[name=city]').val(), 'state' : $('select[name=state]').val(), 'zip' : $('input[name=zip]').val(), 'years' : $('input[name=years]').val(), 'hPhone' : $('input[name=hPhone]').val(), 'altPhone' : $('input[name=altPhone]').val(), 'dNumber' : $('input[name=dNumber]').val(), 'dState' : $('input[name=dState]').val(), 'employer' : $('input[name=employer]').val(), 'ePhone' : $('input[name=ePhone]').val(), 'hType' : $('select[name=hType]').val(), 'hStatus' : $('select[name=hStatus]').val(), 'lName' : $('input[name=lName]').val(), 'LNumber' : $('input[name=LNumber]').val(), 'student' : $('select[name=student]').val(), 'sName' : $('input[name=sName]').val() }; // process the form $.ajax({ type : 'POST', // define the type of HTTP verb we want to use (POST for our form) url : 'process.php', // the url where we want to POST data : formData, // our data object dataType : 'json', // what type of data do we expect back from the server encode : true }) // using the done promise callback .done(function(data) { // log data to the console so we can see console.log(data); console.log(formData); }); // stop the form from submitting the normal way and refreshing the page event.preventDefault(); console.log(formData); }); }); 元素,而不是数组的 <?php // process.php $errors = array(); // array to hold validation errors $data = array(); // array to pass back data $body = implode("\n", $array); $name = $_POST['name']; $email = $_POST['email']; $address = $_POST['address']; $city = $_POST['city']; $state= $_POST['state']; $zip = $_POST['zip']; $years = $_POST['years']; $hPhone = $_POST['hPhone']; $altPhone = $_POST['altPhone']; $dState = $_POST['dState']; $dNumber = $_POST['dNumber']; $employer = $_POST['employer']; $ePhone = $_POST['ePhone']; $hType = $_POST['hType']; $hStatus = $_POST['hStatus']; $lName = $_POST['lName']; $lNumber = $_POST['lNumber']; $student = $_POST['student']; $sName = $_POST['sName']; //(!empty($_POST['student']) ? $_POST['student'] : null); $to = 'mekeri@gmail.com'; $subject = "$name has sent you a message"; $from = "AdoptionApp"; //HTML headers for email $headers = "From: $from\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; //begin of HTML message $message = <<<EOF <html> <body bgcolor="#99CCCC"> <table rules="all" style="border-color: #666;" cellpadding="10"> <tr style="background: #eee;"><td><strong>Name:</strong> </td><td>$name</td></tr> <tr><td><strong>Email:</strong> </td><td>$email</td></tr> <tr><td><strong>Address</strong> </td><td>$address</td></tr> <tr><td><strong>City:</strong> </td><td>$city</td></tr> <tr><td><strong>State:</strong> </td><td>$state</td></tr> <tr><td><strong>Zip:</strong> </td><td>$zip</td></tr> <tr><td><strong>Years @ Address:</strong> </td><td>$years</td></tr> <tr><td><strong>Home Phone:</strong> </td><td>$hPhone</td></tr> <tr><td><strong>Alternate Phone:</strong> </td><td>$altPhone</td></tr> <tr><td><strong>Driver License Number:</strong> </td><td>$dNumber</td></tr> <tr><td><strong>Driver License State:</strong> </td><td>$dState</td></tr> <tr><td><strong>Employer:</strong> </td><td>$employer</td></tr> <tr><td><strong>Employer Phone:</strong> </td><td>$ePhone</td></tr> <tr><td><strong>Type of home</strong> </td><td>$hType</td></tr> <tr><td><strong>Home Status</strong> </td><td>$hStatus</td></tr> <tr><td><strong>Landlord Name</strong> </td><td>$lName</td></tr> <tr><td><strong>Landlord Phone</strong> </td><td>$lPhone</td></tr> <tr><td><strong>Student</strong> </td><td>$student</td></tr> <tr><td><strong>Spouse Name</strong> </td><td>$sName</td></tr> </table> </body> </html> EOF; //end of message // validate the variables ====================================================== // if any of these variables don't exist, add an error to our $errors array // if (empty($_POST['name']) & empty($_POST['email']) & empty($_POST['address']) & empty($_POST['city']) & empty($_POST['state']) & empty($_POST['zip']) & empty($_POST['years']) & empty($_POST['hPhone']) & empty($_POST['altPhone']) & empty($_POST['dNumber']) & empty($_POST['dState']) & empty($_POST['hType']) & empty($_POST['hStatus']) & empty($_POST['student'])) // $errors = 'Field is required.'; if (empty($_POST['name'])) $errors['name'] = 'name is required.'; if (empty($_POST['email'])) $errors['email'] = 'Email is required.'; if (empty($_POST['address'])) $errors['address'] = 'Address is required.'; if (empty($_POST['city'])) $errors['city'] = 'City is required.'; if (empty($_POST['state'])) $errors['state'] = 'State is required.'; if (empty($_POST['zip'])) $errors['state'] = 'Zip is required.'; if (empty($_POST['years'])) $errors['years'] = 'Years at current address is required.'; if (empty($_POST['hPhone'])) $errors['hPhone'] = 'Home phone is required.'; if (empty($_POST['dNumber'])) $errors['dNumber'] = 'Driver license number is required.'; if(empty($_POST['dState'])) $errors['dState'] = 'Driver license state is required.'; if ($_POST['hType']= "") $errors['hType'] = 'Home Type is required.'; if ($_POST['hStatus']= "") $errors['hStatus'] = 'hStatus is required.'; if(empty($_POST['student'])) $errors['student'] = 'student? is required.'; // return a response =========================================================== // if there are any errors in our errors array, return a success boolean of false if ( ! empty($errors)) { // if there are items in our errors array, return those errors $data['success'] = false; $data['errors'] = $errors; } else { // If there are no errors, send the email if (!$errors) { mail($to, $subject, $message, $headers); echo "Message has been sent....!"; } } // return all our data to an AJAX call echo json_encode($data); ?> &#39; 字节

答案 1 :(得分:2)

这是指针算术。 A[I]正好是*(A+I)。现在,A+I意味着什么?它是I的{​​{1}}元素的地址。如果AA数组,则int是数组A+5的第6个元素的地址。 当您使用字节进行衡量时,地址的值是字节AA地址的值。

答案 2 :(得分:0)

int数组中的每个元素都包含sizeof(int)空间来存储数字。 所以从技术上讲,你的数组是sizeof(int)* size。

答案 3 :(得分:0)

自从我实际用C语言编写以来已经很长时间了,如果我在语法或函数调用中出错,请原谅我。

数组就像元素的线性表(字符串,整数,浮点数,字符,结构等),[5]告诉编译器到达那个“表”中的第6个元素,而不管是什么类型它是。编译器根据声明的数组变量的类型知道要跳转的内存位置数。

现在,当你指向内存时,你通过使用指向你想要启动的内存中的位置的指针来检索内容,在整数数组的情况下,你需要告诉它每个内存位置有多少整数是为了跳转并从表示的数组元素中获取数据。

考虑到这一点,我们假设“a”表示内存中已用整数作为元素分配的数组,在这种情况下,如果你这样做:

int *a = malloc(10 * sizeof(int));

/* initialize a with values */

/* .... some time later retrieve the 6th element: a[5] */
int x = *(a + 5);

程序将首先通过执行以下操作跳过5个“a”元素,这是我们所知的整数:

sixth_element_address = a+5*sizeof(a)

然后检索内容并将其分配给整数变量:

x = *(sixth_element_address)

因此,使用数组表示法与使用指针表示法进行操作相同。

现在,我使用指针表示法的唯一原因是我没有声明数组变量本身。