标签: pointer-arithmetic

在 C 中使用带有指针的模运算符

如果我有一个 char 指针,char *ptr它保存单个 ascii 字符的地址,并且想要在该地址上使用模运算符,我该怎么做?每当我尝试使用诸如 之类的基本操作来执行此操作时int modulo = ptr % 16,我都会收到错误“二进制 % 的操作数无效”我对二进制算术的掌握很弱,所以我知道我需要继续发展这项技能,但如果有人可以告诉我我在这里缺少什么概念,这将是一个很大的帮助,谢谢。

编辑:抱歉,我不太清楚我想要完成什么,但基本上我只需要显示一块有 16 个插槽的内存。它是一个程序,在数组中找到一个 ascii 字符,我已经完成了,然后显示存储该 ascii 字符的内存块。当我现在运行它时,它会显示找到的 ascii 字符以及找到的字符后面的 15 个字符及其内存位置。相反,如果找到的 ascii 字符位于内存块的中间,我需要它来显示它周围存储的内容,而不仅仅是它后面的内容。因此,如果找到的字符位于数组的 23 槽中,则程序将显示数组的 16-31 槽中的字符和内存位置。

c pointers modulo pointer-arithmetic

3
推荐指数
1
解决办法
1万
查看次数

在使用对齐冲突进行强制转换后,指针算术是否仍然定义良好?

我知道,一旦取消引用,带有对齐冲突的指针转换的结果就会调用未定义的行为。

\n

但是仅用于地址计算(不取消引用)的指针转换怎么样?

\n
void *addr_calc(single_byte_aligned_struct_t *ptr, uint32_t dword_offset)\n{\n    uint32_t *dw_ptr = (uint32_t *)ptr;\n\n    return dw_ptr + dword_offset;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我们假设 的ptr值为X。能保证一定addr_calc()会回来X + sizeof(uint32_t) * dword_offset吗?

\n

我的假设是,但最近我在 C11 标准的J.2 节中看到了以下未定义行为

\n
\n

\xe2\x80\x94 两个指针类型之间的转换会产生不正确对齐的结果 (6.3.2.3)。

\n
\n

如果我理解正确的话,转换本身会调用未定义的行为,而不仅仅是取消引用,这意味着在这种情况下,即使是指针算术也可能表现出不可预测的行为。我对吗?

\n

c memory-alignment pointer-arithmetic undefined-behavior c11

3
推荐指数
1
解决办法
378
查看次数

三个问题:NULL - NULL 定义了吗?是否已定义 (uintptr_t)NULL - (uintptr_t)NULL?

  1. NULL - NULL定义为。?

  2. (char *)NULL - (char *)NULL定义为。?

  3. (uintptr_t)NULL - (uintptr_t)NULL定义为?

我知道它适用于我使用的所有实现。但从标准的角度来看,它是什么样子的呢?我找不到明确的答案。

编辑:从骗子看来,我认为问题的一个答案是:是的。

那么第二个问题和第三个问题呢?

c pointer-arithmetic null-pointer language-lawyer

3
推荐指数
1
解决办法
273
查看次数

C: int 或 unsigned int 我用于指针增加的类型

对于这种情况:

int arr[] = {0, 1, 2};
void func (int* arr_in){
  int offset_0 = 0;
  int offset_1 = 1;
  int offset_2 = 2;
  printf("%d\n", *(arr_in + offset_0) );
  printf("%d\n", *(arr_in + offset_1) );
  printf("%d\n", *(arr_in + offset_2) );
}
Run Code Online (Sandbox Code Playgroud)

编译器不会抱怨我使用的是int还是unsigned.

两个结果似乎也正确。

int arr[] = {0, 1, 2};
void func (int* arr_in){
  int offset_0 = 0;
  int offset_1 = 1;
  int offset_2 = 2;
  printf("%d\n", *(arr_in + offset_0) );
  printf("%d\n", *(arr_in + offset_1) );
  printf("%d\n", *(arr_in + …
Run Code Online (Sandbox Code Playgroud)

c pointers integer pointer-arithmetic

3
推荐指数
1
解决办法
42
查看次数

为什么数组访问和指针算术与完全优化不等效?

为什么这段代码不产生相同的程序集?(g++ -O3) 我对汇编知之甚少,但似乎情况 2 访问的指令较少,所以应该首选,对吗?我问这个是因为我想用返回一个指针的访问运算符来实现一个包装类int* p = a[i](所以访问是a[i][j],而不是a[i*3+j]),但不知道它是否值得。感谢您的任何帮助。

#include <iostream>

int main() {
    
    int a[9];
    int i, j, k;

    // Case 1
    std::cin >> i >> j >> k;
    *(a + i*3 + j) = k;

    std::cin >> i >> j >> k;
    (&a[i*3])[j] = k;

    std::cin >> i >> j >> k;
    *((&a[i*3])+j) = k;

    // Case 2
    std::cin >> i >> j >> k;
    a[i*3 + j] = k;

    std::cout << a[0]; …
Run Code Online (Sandbox Code Playgroud)

c++ assembly pointer-arithmetic arrayaccess c++20

3
推荐指数
1
解决办法
104
查看次数

计算指向未初始化内存的指针是 C 中的未定义行为吗?

如果我理解正确的话,这个程序在 C++ 中具有未定义的行为,因为中间值p + 1是指向未初始化内存的指针:

int main () {
    int x = 0;
    int *p = &x;
    p = p + 1 - 1;
    *p = 5;
}
Run Code Online (Sandbox Code Playgroud)

如果void放入 的main参数列表中(按照 C 语法的要求),它在 C 中是否也是未定义的行为?

c pointers pointer-arithmetic undefined-behavior

3
推荐指数
1
解决办法
355
查看次数

减去地址引用是如何工作的?

我认为 的值z应该是 40,因为a[5]有 20 个元素,并且 和 之间的空间a[3]也有 20 个元素。然而,实际值z是2。

谁能解释一下这个概念?

#include <stdio.h>

int main()
{
    int a[10][20];
    int z = &a[5] - &a[3];
    printf("%d\n", &a[3]); // the address information is 6421740
    printf("%d\n", &a[5]); // the address information is 6421900
    printf("%d\n", z);     // z value is 2. why?
}
Run Code Online (Sandbox Code Playgroud)

c arrays pointer-arithmetic memory-address

3
推荐指数
1
解决办法
88
查看次数

p1和p2是指向int的指针,如果p2&gt;p1有效,那么p2-p1有效吗?

可以比较指向类成员变量的指针,结果取决于声明顺序。请参阅规范例如,编译器资源管理器中的此示例有效并返回 true (1):

struct A {
    int a0 = 1;
    int a1 = 2;
};

consteval int foo()
{
    A a;
    int* p1 = &a.a0;
    int* p2 = &a.a1;
    return p2 > p1;
}

int main()
{
    return foo();
}
Run Code Online (Sandbox Code Playgroud)

因此,人们会期望它p2-p1会返回指针之间的距离(以 int 对象为单位)。它在运行时执行。编译器浏览器

struct A {
    int a0 = 1;
    int a1 = 2;
};

int foo()
{
    A a;
    int* p1 = &a.a0;
    int* p2 = &a.a1;
    return p2 - p1;
} …
Run Code Online (Sandbox Code Playgroud)

c++ pointers pointer-arithmetic language-lawyer

3
推荐指数
1
解决办法
242
查看次数

为什么我们不应该使用gsl :: not_null的指针算法?

这是一个人为的例子,但请考虑以下内容:

#include <iostream>
#include "gsl.h"

int main(){

  //object or array that I'd like to iterate over one byte at a time
  char array[] = {'a','b','c','d','e','f'};

  //create a C-like iterator
  char* it = &array[0];

  //use pointer arithmetic to process
  std::cout << *it << std::endl; it++;
  std::cout << *it << std::endl; it++;
  std::cout << *it << std::endl; it++;
}
Run Code Online (Sandbox Code Playgroud)

为安全起见,我想用指针标记指针not_null.
但是,这无法编译.

#include "gsl.h"
#include <iostream>

int main(){

  //object or array that I'd like to iterate over one byte at a …
Run Code Online (Sandbox Code Playgroud)

c++ null pointers pointer-arithmetic cpp-core-guidelines

2
推荐指数
1
解决办法
1035
查看次数

Printing entered string using pointer manipulation

I am new to pointers, please let me know how can i print the entered character.

    #include <stdio.h>
    #include <stdlib.h>

   int main()
   {
      char *ptr;
      ptr = malloc(32 * sizeof(char));
      *ptr = 'h';
      ptr++;
      *ptr = 'e';
      ptr++; 
      *ptr = 'l';
      ptr++;
      *ptr = 'l';
      ptr++;
      *ptr = 'o';
      ptr++;
      *ptr = '\n';

      printf("value entered is %s\n", ptr);

      return 0;
    }
Run Code Online (Sandbox Code Playgroud)

I want to print hello

c pointers pointer-arithmetic

2
推荐指数
1
解决办法
54
查看次数