减去两个给出错误输出的地址

时间:2017-06-12 11:33:24

标签: c pointers pointer-arithmetic

4

输出:

  

2752116 2752112 1

为什么不p+k

此外,我无法使用-或除WildFly之外的任何其他运算符(减法)。

4 个答案:

答案 0 :(得分:4)

首先,必须为所提供的格式说明符使用正确的参数类型,提供不匹配的参数类型会导致undefined behavior

  • 您必须使用%p格式说明符并将参数强制转换为void *以打印地址(指针)

  • 要打印指针减法的结果,您应使用%td,因为结果属于ptrdiff_t类型。

也就是说,关于减法的结果1,指针算法遵循数据类型。引用C11,章节§6.5.6,(强调我的

  

当减去两个指针时,两个指针都指向同一个数组对象的元素,   或者超过数组对象的最后一个元素; 结果是差异   两个数组元素的下标。结果的大小是实现定义的,   及其类型(有符号整数类型)ptrdiff_t <stddef.h>标头中定义。 [....]如果表达式PQ分别指向i - 和j - 元素   一个数组对象,表达式(P)-(Q)具有值i−j,前提是该值适合类型为ptrdiff_t的对象。 [....]

因此,在您的情况下,pk的索引是一个元素,即|i-J| == 1,因此结果。

最后,你不能添加(或乘法或除法)两个指针,因为,无意义。指针是内存位置,从逻辑上讲,您无法添加两个内存位置。只有减法才有意义,找到两个数组成员/元素之间的相关距离。

相关约束,来自C11,章节§6.5.6,加法运算符,

  

另外,两个操作数都应具有算术类型,或者一个操作数应为a   指向完整对象类型的指针,另一个指针应具有整数类型。 (递增是   相当于添加1。)

答案 1 :(得分:0)

你得到的是两个元素的下标之间的区别 C11-6.5.6p9:

  

当减去两个指针时,两个指针都指向同一个数组对象的元素,或者指向数组对象的最后一个元素的元素; 结果是两个数组元素的下标差异

还要注意声明

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

The above exception (NOT NULL constraint failed: userprofile_studentprofile.user_id) was the direct cause of the following exception:

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/wtreston/GDrive/LesRev/lesssonreview/userprofile/views.py" in registration
  13.             form.save()

File "/Users/wtreston/GDrive/LesRev/lesssonreview/userprofile/forms.py" in save
  41.             student_profile.save()

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py" in save
  796.                        force_update=force_update, update_fields=update_fields)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py" in save_base
  824.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py" in _save_table
  908.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py" in _do_insert
  947.                                using=using, raw=raw)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/query.py" in _insert
  1045.         return query.get_compiler(using=using).execute_sql(return_id)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1054.                 cursor.execute(sql, params)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

Exception Type: IntegrityError at /users/
Exception Value: NOT NULL constraint failed: userprofile_studentprofile.user_id

应该是

 printf("%d %d %d", p,k,p-k);  

答案 2 :(得分:0)

如果你的变量是指针类型,那么指针的每个计算都是通过指针类型大小的乘法来完成的。

例如:

//Lets assume char is 1 byte, int is 4 bytes long.
// sizeof(*cp) = 4, sizeof(*ip) = 4;
char *cp = (char *)10;  //Char itself is 1 byte
int *ip = (int *)10;
cp++;          //Increase pointer, let us point to the next char location
ip++;          //Increase pointer, let us point to the next int location

printf("Char: %p\r\n", (void *)cp); //Prints 11
printf("Int: %p\r\n", (void *)ip); //Prints 14

第一个案例打印11,而在第二个案例中打印14。那是因为下一个char元素接下来是1个字节,而下一个int元素是提前4个字节。

如果您有两个相同类型的指针(例如int *,就像您一样),那么如果一个指向14而另一个指向10,则指向1 int memory,减去给你1

如果您想获得结果4,请在计算之前投射指向char *的指针,因为sizeof(char)始终是1,这意味着您拥有4已解决1014之间的元素,您将获得结果4

希望它有所帮助。

答案 3 :(得分:0)

首先,未定义添加2个指针。因此,如果您使用+运算符,则将面临编译error

其次,输出为真,如果你减去2个指针,它会显示指针之间有多少个那种类型的盒子。不是字节数。

你说:

int* p1 = &x;
int* p2 = p1++;

所以p1&amp; p2有4个字节。它们都是int类型。因此,他们之间只有一盒int