标签: pointers

如何检查指针是否有效?

我很清楚这个问题的丑陋,我还是想知道它是否可能:

当程序尝试读取/写入无效指针(NULL,未分配的块等)时,窗口会使应用程序崩溃并发生访问冲突异常.

问题是,有没有办法检查这个指针在尝试使用它之前会产生异常,否则,有没有办法捕获这个异常?

c winapi pointers

21
推荐指数
5
解决办法
5万
查看次数

什么是在类名后使用的&符号,如ostream&operator <<(...)?

我知道所有关于指针和&符号的意思是"地址",但在这种情况下它意味着什么?

另外,当重载运算符时,为什么通常用const声明参数?

c++ pointers class operator-overloading addressof

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

如何在Linux上设置C中的鼠标光标位置?

如何在Linux下使用C程序在X窗口中设置鼠标光标位置?谢谢:)(像WIN中的setcursorpos())

编辑:我已经尝试过这段代码,但不起作用:

#include <curses.h>

main(){
 move(100, 100);
 refresh();
}
Run Code Online (Sandbox Code Playgroud)

c linux x11 pointers position

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

为什么arr和&arr一样?

我已经编程了很多年的c/c ++,但今天的偶然发现让我有点好奇......为什么两个输出在下面的代码中产生相同的结果?(arr当然是地址arr[0],即指向arr[0].我本来希望&arr是该指针的地址,但它具有相同的值arr)

  int arr[3];
  cout << arr << endl;
  cout << &arr << endl;
Run Code Online (Sandbox Code Playgroud)

备注:这个问题已经结束,但现在又被打开了.(谢谢 ?)

我知道&arr[0]arr评估相同的数字,但这不是我的问题!问题是为什么&arrarr评估相同的数字.如果arr是文字(不存储任何软件),那么编译器应该抱怨并说这arr不是左值.如果地址arr存储在某个地方,那么&arr应该给我该地点的地址.(但这种情况并非如此)

如果我写

const int*arr2 = arr;

那么arr2[i]==arr[i]对于任何整数i,但是&arr2 != arr.

c c++ pointers

21
推荐指数
4
解决办法
9031
查看次数

为什么指针+ 1实际上加4

#include<stdio.h>
int main(void){
  int *ptr,a,b;
  a = ptr;
  b = ptr + 1;
  printf("the vale of a,b is %x and %x respectively",a,b);

  int c,d;
  c = 0xff;
  d = c + 1;
  printf("the value of c d are %x and %x respectively",c,d);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出值是

the vale of a,b is 57550c90 and 57550c94 respectively
the value of c d are ff and 100 respectively%  
Run Code Online (Sandbox Code Playgroud)

事实证明,ptr + 1实际上是这样的?

c pointers

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

Golang指针

我目前正在学习用Go语言编程.我在理解Go指针方面遇到了一些困难(现在我的C/C++还很远......).例如,在Tour of Go#52(http://tour.golang.org/#52)中,我读到:

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    fmt.Println(v.Abs())
}
Run Code Online (Sandbox Code Playgroud)

但如果不是

func (v *Vertex) Abs() float64 {
[...]
v := &Vertex{3, 4}
Run Code Online (Sandbox Code Playgroud)

我写:

func (v Vertex) Abs() float64 {
[...]
v := Vertex{3, 4}
Run Code Online (Sandbox Code Playgroud)

甚至:

func (v Vertex) Abs() float64 {
[...]
v := &Vertex{3, 4}
Run Code Online (Sandbox Code Playgroud)

反之亦然:

func (v *Vertex) Abs() float64 {
[...]
v := Vertex{3, …
Run Code Online (Sandbox Code Playgroud)

pointers go

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

解除引用指针和访问数组元素之间的区别

我记得一个示例,其中演示了指针和数组之间的区别.

当作为函数参数传递时,数组衰减到指向数组中第一个元素的指针,但它们不等效,如下所示:

//file file1.c

int a[2] = {800, 801};
int b[2] = {100, 101};
Run Code Online (Sandbox Code Playgroud)
//file file2.c

extern int a[2];

// here b is declared as pointer,
// although the external unit defines it as an array
extern int *b; 

int main() {

  int x1, x2;

  x1 = a[1]; // ok
  x2 = b[1]; // crash at runtime

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

链接器不会对外部变量进行类型检查,因此在编译时不会生成错误.问题是它b实际上是一个数组,但编译单元file2不知道这一点并将其视为b指针,在尝试取消引用时会导致崩溃.

我记得当这被解释时它是完全合理的,但现在我不记得解释,也不能靠自己来解决.

所以我想问题是在访问元素时,数组如何与指针区别对待?(因为我认为无论是数组还是指针p[1]都转换为(程序集等价)- 我显然是错的).*(p + 1)p


由两个解除引用(VS 2013)所产生的组件:
注意: 1158000h …

c arrays pointers dereference

21
推荐指数
3
解决办法
2568
查看次数

功能stoi没有声明

我正在尝试使用stoi将字符串转换为整数,但是它表示它没有声明.我有标准库和包含,但它仍然说"[错误]'stoi'未在此范围内声明"

代码如下:

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

using namespace std;

int main()
{
string end, init;
cout << "Introduction" << endl;
cout << "Start time (xx:yy)" << endl;
cin >> init;
string hours0 = init.substr(0,2);
int hours = stoi(hours0);
cout << hours << endl;
system("pause");
return 0;

}
Run Code Online (Sandbox Code Playgroud)

请告诉我它为什么不起作用,或者给我第二个选择.

c++ string int pointers

21
推荐指数
5
解决办法
13万
查看次数

鉴于p是一个指针是"p> nullptr"格式良好?

给出一个指针p:

char *p ; // Could be any type
Run Code Online (Sandbox Code Playgroud)

假设p正确初始化是以下格式良好:

if (p > 0) // or p > nullptr
Run Code Online (Sandbox Code Playgroud)

更一般地说,当一个操作数是指针而另一个是空指针常量时,使用关系运算符是否良好?

c++ pointers language-lawyer c++11 c++14

21
推荐指数
1
解决办法
852
查看次数

如何在C中打印内存地址

我的代码是:

#include <stdio.h>
#include <string.h>

void main()
    {
    char string[10];
    int A = -73;
    unsigned int B = 31337;

    strcpy(string, "sample");

    // printing with different formats
    printf("[A] Dec: %d, Hex: %x, Unsigned: %u\n", A,A,A);
    printf("[B] Dec: %d, Hex: %x, Unsigned: %u\n", B,B,B);
    printf("[field width on B] 3: '%3u', 10: '%10u', '%08u'\n", B,B,B);

    // Example of unary address operator (dereferencing) and a %x
    // format string 
    printf("variable A is at address: %08x\n", &A);
Run Code Online (Sandbox Code Playgroud)

我在linux mint中使用终端编译,当我尝试使用gcc编译时,我收到以下错误消息:

basicStringFormatting.c: In function ‘main’:
basicStringFormatting.c:18:2: warning: …
Run Code Online (Sandbox Code Playgroud)

c printf pointers memory-address unary-operator

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