如果我有一个 char 指针,char *ptr它保存单个 ascii 字符的地址,并且想要在该地址上使用模运算符,我该怎么做?每当我尝试使用诸如 之类的基本操作来执行此操作时int modulo = ptr % 16,我都会收到错误“二进制 % 的操作数无效”我对二进制算术的掌握很弱,所以我知道我需要继续发展这项技能,但如果有人可以告诉我我在这里缺少什么概念,这将是一个很大的帮助,谢谢。
编辑:抱歉,我不太清楚我想要完成什么,但基本上我只需要显示一块有 16 个插槽的内存。它是一个程序,在数组中找到一个 ascii 字符,我已经完成了,然后显示存储该 ascii 字符的内存块。当我现在运行它时,它会显示找到的 ascii 字符以及找到的字符后面的 15 个字符及其内存位置。相反,如果找到的 ascii 字符位于内存块的中间,我需要它来显示它周围存储的内容,而不仅仅是它后面的内容。因此,如果找到的字符位于数组的 23 槽中,则程序将显示数组的 16-31 槽中的字符和内存位置。
我知道,一旦取消引用,带有对齐冲突的指针转换的结果就会调用未定义的行为。
\n但是仅用于地址计算(不取消引用)的指针转换怎么样?
\nvoid *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}\nRun Code Online (Sandbox Code Playgroud)\n我们假设 的ptr值为X。能保证一定addr_calc()会回来X + sizeof(uint32_t) * dword_offset吗?
我的假设是,但最近我在 C11 标准的J.2 节中看到了以下未定义行为
\n\n\n\xe2\x80\x94 两个指针类型之间的转换会产生不正确对齐的结果 (6.3.2.3)。
\n
如果我理解正确的话,转换本身会调用未定义的行为,而不仅仅是取消引用,这意味着在这种情况下,即使是指针算术也可能表现出不可预测的行为。我对吗?
\nc memory-alignment pointer-arithmetic undefined-behavior c11
被NULL - NULL定义为。?
被(char *)NULL - (char *)NULL定义为。?
被(uintptr_t)NULL - (uintptr_t)NULL定义为?
我知道它适用于我使用的所有实现。但从标准的角度来看,它是什么样子的呢?我找不到明确的答案。
编辑:从骗子看来,我认为问题的一个答案是:是的。
那么第二个问题和第三个问题呢?
对于这种情况:
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) 为什么这段代码不产生相同的程序集?(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++ 中具有未定义的行为,因为中间值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 中是否也是未定义的行为?
我认为 的值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) 可以比较指向类成员变量的指针,结果取决于声明顺序。请参阅规范例如,编译器资源管理器中的此示例有效并返回 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) 这是一个人为的例子,但请考虑以下内容:
#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) 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