标签: dereference

Derefering NULL指针c ++

我收到警告:在第2,4行上取消NULL指针'ch'

我不明白为什么.有人可以帮我吗?

char *my_alloc(size_t size) {
   char *ch  = (char *)malloc(size);
   //FIXED: If malloc fails -> exit program
   if(*ch == NULL){
       exit(0);
   }
   return ch;
}
Run Code Online (Sandbox Code Playgroud)

c++ malloc pointers nullpointerexception dereference

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

perl在字符串eq中使用未初始化的值$ _

为什么我有时会出现这个错误?在字符串eq中使用未初始化的值$ _

if (defined($infos->{"valeur_exclude"}) &&
grep { $number eq $_ } @{$infos->{"valeur_exclude"}});
Run Code Online (Sandbox Code Playgroud)

谢谢

arrays perl initialization equals dereference

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

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

Int不能被引用错误

我试图从一个方法获取用户输入并在另一个方法中使用它.我对错误感到困惑,因为它们都是int类型.


public static void move()
{
    System.out.println("What do you want to do?");
    Scanner scan = new Scanner(System.in);
    int userMove = scan.nextInt();
}

public static void usersMove(String playerName, int gesture)
{
    int userMove = userMove.move(); //error is here

    if (userMove == -1)
    {
        break;
    }
Run Code Online (Sandbox Code Playgroud)

java int compiler-errors dereference

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

取消引用这个指针给了我-46,但我不知道为什么

这是我运行的程序:

#include <stdio.h>

int main(void)
{
    int y = 1234;
    char *p = &y;
    int *j = &y;
    printf("%d %d\n", *p, *j);
}
Run Code Online (Sandbox Code Playgroud)

我对输出有点困惑.我所看到的是:

-46 1234
Run Code Online (Sandbox Code Playgroud)

我把这个程序写成了一个实验,不知道它会输出什么.我期待可能有一个字节y.

这里发生了什么"幕后"?解除引用如何p给我-46?

正如其他人指出的那样,我必须进行明确的施法才能导致UB.我没有改变这一行char *p = &y;,char *p = (char *)&y;所以我没有使下面的答案无效.

此程序不会导致此处指出的任何UB行为.

c pointers casting dereference

-3
推荐指数
2
解决办法
1243
查看次数

在此函数中有缺陷,因为取消引用null无效,所以我想更改代码

long cread(long *xp) {
  return (xp? *xp : 0);
}
Run Code Online (Sandbox Code Playgroud)

它无效,因为它可能尝试从空地址读取

所以解决方案建议使用此代码

long cread_alt(long *xp){
  long tem = 0;
  if(*xp > 0){
   tem = *xp;
  }
  return tem;
Run Code Online (Sandbox Code Playgroud)

但我认为它也无效,因为if(*xp > 0)当xp指向空地址时仍然有缺陷.

所以我想到了这段代码

 long cread_alt2(long *xp){
     long tem = 0;
     if(xp != NULL){
        tem = *xp;
     }
     return tem;
 }
Run Code Online (Sandbox Code Playgroud)

我有这个吗?

c++ pointers null-pointer dereference

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

什么是 C 中的指针解引用?

我正在使用 c 学习递归,并且有一个关于数组的问题,例如:

void main(){
     char str[100];
     --snip--
     if(*str == 'c')
        count++;
     --snip--
}
Run Code Online (Sandbox Code Playgroud)

这里的 *str 是从指向字符串str 的第一个字符的指针中检索到的值。

我的问题是什么是指针取消引用,我可以执行以下操作:

--snip--
str+=1;
--snip--
Run Code Online (Sandbox Code Playgroud)

获取指向该字符串中另一个字符的下一个位置的新指针?

c pointers dereference

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

*在C++中的目的是什么?

int main()
{
    string s("some string");
    if (s.begin() != s.end())
        auto it = s.begin();
    *it = toupper (*it) ; // Error ; the identifier "it" is undefined 
}
Run Code Online (Sandbox Code Playgroud)

为什么*它未定义?为什么我们需要在迭代器维度中使用dereference?

c++ iterator dereference

-11
推荐指数
1
解决办法
117
查看次数

为什么我们不需要在传递给printf时取消引用指向字符串的指针?

#include<stdio.h>    
int main()
{
  char *p = NULL;
  char str[] = "How do you do!!!!!";
  p = &str;
  printf("String is:%s",p);
  p = "HELLO HOW ARE YOU";

  printf("String is:%s",p);
  printf("Hello");


  int a = 10;
  int *pa;
  pa = &a;
  printf("Contents of a is %d\n",a);
  printf("Contents of pa is %x\n", (int)pa);
  printf("Values of variable pointed to by pa is %d\n", *pa);

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

在参考上面的代码时:

p = &str;
printf("String is:%s",p);
Run Code Online (Sandbox Code Playgroud)

p将显示存储的字符串.

pa = &a;
printf("Values of variable pointed to by pa is …
Run Code Online (Sandbox Code Playgroud)

c pointers dereference

-22
推荐指数
1
解决办法
451
查看次数