在Visual Studio C++版本9(也可能是其他版本)中,以下代码:
int a = sizeof(void);
void const *b = static_cast<void const *>("hello world");
b += 6;
Run Code Online (Sandbox Code Playgroud)
error C2070: 'void': illegal sizeof operand
error C2036: 'const void *' : unknown size
Run Code Online (Sandbox Code Playgroud)
此代码在GCC下工作,GCC sizeof(void)视为1.
是否存在一些解决此限制的方法,因为char *为了指针运算的目的而明确地进行转换会增加混淆(void *被公认并被用作原始内存的无类型指针).
sizeof(void)认为我很清楚这不是1导致问题的原因.void,但这是C,这些事情都会发生.看来这个问题引起了很大的混乱.现在的问题不是关于为什么有sizeof(void) == 1不标准,但做什么时,它不是.
在要进行单字节指针运算的情况下,事实证明,转换为char *正确的答案,不是因为*(void *)没有大小,而是因为标准实际上保证了这*(char *) …
为什么我不能执行此操作:
var
data:pbyte;
x:int64;
o:pointer;
begin
o:=data+x;
end;
Run Code Online (Sandbox Code Playgroud) 我写了以下代码:
#include <iostream>
using namespace std;
int main()
{
int a[10][10];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
a[i][j] = i * j;
cout << *(*(a + 3) + 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待它打印一些垃圾数据或分段错误.我得到的是12.我用c和c ++(分别使用gcc和g ++)对它进行了测试,虽然我没有对此进行测试,但我在VS上的工作原理相同.为什么这样做,是否有这种行为的官方文档?
我不明白为什么我的花车的地址上升了16,当我的花车的大小是4.有人可以解释一下吗?
码:
char* mychar = new char[SIZE];
float* myfloat = new float[SIZE];
for(int i = 0; i < SIZE; i++)
{
mychar[i] = 'A' + i;
myfloat[i] = 101 + i;
}
for(int i = 0; i < SIZE; i++)
{
cout << setw(12) << "new char @ <" << static_cast<void*>(mychar) + sizeof(char)*i << ">=<" << mychar[i] << ">"
<< setw(14) << " new float @ <" << myfloat + sizeof(float)*i << ">=<" << myfloat[i] << ">\n";
}
cout<< "Size …Run Code Online (Sandbox Code Playgroud) 在下面的程序中,Here ptr已被声明为指向整数指针的指针并分配了数组的基址,该数组p[]已被声明为整数指针数组.假设ptr包含地址9016(假设p的起始地址是9016)在ptr递增之前和之后ptr++,它将包含值9020(假设int占用4个字节).
所以ptr-p应该将输出设为4即(9020-9016 = 4).但它输出为1.为什么?
#include<stdio.h>
int main()
{
static int a[]={0,1,2,3,4};
static int *p[]={a,a+1,a+2,a+3,a+4};
int **ptr=p;
ptr++;
printf("%d",ptr-p);
return 0;
}
Run Code Online (Sandbox Code Playgroud) int array[5];
Run Code Online (Sandbox Code Playgroud)
表达如
array[3] gets converted to *(array+3)
Run Code Online (Sandbox Code Playgroud)
或者在
void fun ( int *array[] );
*array[] gets converted to int **array
Run Code Online (Sandbox Code Playgroud)
我想知道数组声明是什么
int array[5];
Run Code Online (Sandbox Code Playgroud)
转换成?是吗
int *(array+5)
Run Code Online (Sandbox Code Playgroud)
如果是的话,这甚至意味着什么?一个人如何解释和/或阅读它?
我无法完成下面某段特定代码的逻辑.
int i[] = { 21, 4, -17, 45 };
int* i_ptr = i;
std::cout << (*i_ptr)++ << std::endl; // 21
std::cout << *i_ptr << std::endl; // 22
std::cout << *i_ptr++ << std::endl; // 22
std::cout << *(i_ptr - 1) << std::endl; // 22
std::cout << *i_ptr << std::endl; // 4
std::cout << ++*i_ptr << std::endl; // 5
std::cout << *++i_ptr << std::endl; // -17
system("pause");
Run Code Online (Sandbox Code Playgroud)
我的问题是这段代码是如何从22 ...
std::cout << *(i_ptr - 1) << std::endl; // 22
Run Code Online (Sandbox Code Playgroud)
到4.
std::cout << …Run Code Online (Sandbox Code Playgroud) 有人可以向我解释为什么这段代码有效吗?!!
我知道A持有&A [0]并且它不是一个真正的指针,就好像你想要<< A,你会得到&A [0],但这个结果对我来说看起来很奇怪.
int main()
{
double A[] = {2.4, 1.2, 4.6, 3.04, 5.7};
int len = *(&A+1) - A; // Why is this 5?
cout << "The array has " << len << " elements." << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码不起作用?你怎么能让它发挥作用?
void test(double B[])
{
int len = *(&B+1) - B;
cout << len << endl;
}
int main()
{
double A[] = {2.4, 1.2, 4.6, 3.04, 5.7};
test(A);
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 所以,我开始熟悉 C,在这一点上我试图理解指针。我从这里得到以下代码,但我无法理解,如何从指针中减去字符数组。
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char s[30], t[20];
char *found;
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
puts("Enter the string to be searched: ");
gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",found-s);
else
printf("-1");
getch();
}
Run Code Online (Sandbox Code Playgroud)
指针不只是给定变量/常量的地址吗?当减法发生时,字符数组会自动假设,因为操作是用指针发生的,所以减去它的地址?我在这里有点困惑。
提前致谢。
关于职业生涯的问题 3表明这*(x+i)与&x[i]. 有人可以解释一下这是否正确吗?