标签: null-pointer

使用NULL值初始化结构

我初始化这个结构时遇到问题:

struct node {
    int label;
    struct node *lchild;
    struct node *rchild;
}

int main(void) {
    struct node n1;
    n1.label = 1;

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

问题是我的成员lchild,rchild并没有指向NULL.

我可以通过添加来解决问题

n1.lchild = NULL;
n1.rchild = NULL;
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么我需要这样做?

c struct null-pointer

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

将0 vs NULL分配给C中的指针

我今天想知道可以为指针分配一个整数0,即像分配NULL一样.直到今天我才假设,这是C++的新功能.这从一开始就是C的一个特征,还是因为它采用了更新的语言标准而放松了?

c null-pointer

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

为什么我收到“错误:使用未声明的标识符”错误?

概括

错误和代码位于问题的底部。

我正在编写一个简单的程序,因为我很好奇指针的大小是多少,以及它们在指向不同数据类型时是否有所不同。
我声明了变量,为什么他们说它们未声明?

另外,由于某种原因, 没有错误,但int*只有bool*和 ,char*如下面的错误消息所示。

代码

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

int main(void) {
    int* ptri = NULL;
    char* ptrc = NULL;
    bool* ptrb = NULL;
    printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
}
Run Code Online (Sandbox Code Playgroud)

错误信息

:!clang test.c && ./a.out
test.c:7:5: error: use of undeclared identifier 'bool'
    bool* ptrb = NULL;
    ^
test.c:7:11: error: use of undeclared identifier 'ptrb'
    bool* ptrb = NULL;
          ^
test.c:8:62: error: use of undeclared identifier 'ptrb'
    printf("%lu %lu %lu", sizeof(ptri), …
Run Code Online (Sandbox Code Playgroud)

c printf null-pointer

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

是否有可用作谓词的 std:: 函数(类似于 std::is_null_ptr)?

我本来希望有一个简短、简洁、优雅的:

std::array<int*,10> ip_arr;
bool all_null = std::all_of(ip_arr.begin(),ip_arr.end(),std::is_null_ptr_as_fn);
Run Code Online (Sandbox Code Playgroud)

而不是为此目的发明 lambda:

std::array<int*,10> ip_arr;
bool all_null = std::all_of(ip_arr.begin(),ip_arr.end(),[](int *ip){ return ip == nullptr;});
Run Code Online (Sandbox Code Playgroud)

这甚至可能是可疑的,因为我忽略了它std::is_null_ptr,它应该读成这样:

std::array<int*,10> ip_arr;
bool all_null = std::all_of(ip_arr.begin(),ip_arr.end(),[](int *ip){ std::is_null_ptr r(ip); return r();});
Run Code Online (Sandbox Code Playgroud)

恶心。

c++ null-pointer

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

使用.getWidth()时无法找到nullpointer的原因

我很难找到为什么我在这段代码上返回nullpointer:

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Move {

  private static final int DIAMETER = 30;

  int x = 0;
  int y = 0;
  int x_1 = 1;
  int y_1 = 1;


  private GameStart game;

  public void Ball(GameStart game) {
    this.game = game;
  }

  void moveBall() {
    if (x + x_1 < 0) {
        x_1 = 1;
    }

    if (x + x_1 > game.getWidth() - DIAMETER) {
        x_1 = -1;
    }

    if (y + y_1 < 0) {
        y_1 = …
Run Code Online (Sandbox Code Playgroud)

java nullpointerexception null-pointer

0
推荐指数
1
解决办法
138
查看次数

C++中的空指针声明混淆

我开始学习C++了.我遇到了关于指针的小混乱,特别是NULL指针.根据我的理解,当您声明指针并将其设置为指向值时,您可以执行以下操作:

    int var = 10;
    int *ptr;
    ptr = &var;
    *ptr = 20;
Run Code Online (Sandbox Code Playgroud)

这会将var的值更改为20.但是当您设置NULL指针时,您将执行以下操作:

int *ptr = NULL;
Run Code Online (Sandbox Code Playgroud)

这是不是意味着你将值NULL分配给ptr指向的任何变量而不是地址,因为*运算符?我认为一个NULL指针的值(它的地址)为0,所以它从我读到的地方找不到任何地方.这样做会更有意义吗:

int *ptr;
ptr = NULL // or 0?
Run Code Online (Sandbox Code Playgroud)

外行术语的解释将不胜感激,我仍在学习所有的术语,因为我编码和研究,所以我喜欢你使用的任何编程术语的解释.

c++ null-pointer

0
推荐指数
1
解决办法
148
查看次数

Nullable类型仍然在Kotlin抛出nullpointer异常

下面的代码在第三行中抛出nullpointer异常.因为objectHashMap为null.但这怎么可能呢.它是一个可以为空的类型,它可以为null.

val objectsGTypeInd = object : GenericTypeIndicator<HashMap<String, Post>>() {}
val objectHashMap: HashMap<String, Post>? = dataSnapshot?.getValue(objectsGTypeInd)
val postList = ArrayList<Post>(objectHashMap?.values)
Run Code Online (Sandbox Code Playgroud)

在logcat上写的"collection == null"消息

nullpointerexception null-pointer kotlin

0
推荐指数
1
解决办法
519
查看次数

我们可以使用 C++ 中限定为常量且值为 0 的变量来初始化指针吗?

根据 C++ Primer 一书,我们可以将限定为 const 且值为 0 的变量分配给指针。这是书中的相关内容。

可以分配文字 0 或在编译时已知其值为 0 的常量:

然后提供以下代码片段。

int ival;
int zero = 0;
const int c_ival = 0;
int *pi = ival;        // error
pi = zero;             // error
pi = c_ival            // ok: c_ival is a const with compile-time value of 0
Run Code Online (Sandbox Code Playgroud)

但是,当我运行类似的代码时,会引发错误。

int main()
{
    const int c_ival = 0;
    int *pi = c_ival;            
}
Run Code Online (Sandbox Code Playgroud)

错误:从“int”到“int*”的无效转换 [-fpermissive]

我究竟做错了什么?

c++ pointers null-pointer nullptr

0
推荐指数
1
解决办法
55
查看次数

无效的空指针 - C++

我从这部分代码中收到无效的空指针错误。我猜这与字符串有关,但由于我最近才了解它们,我找不到问题所在。

string batchCipherFiles()
{
int count(0);
string text, file; 
ifstream inputFile;

cout << " How many files do you wish to encrypt?: ";
cin >> count;
for (int i(1) ; i <= count ; ++i)
{
    stringstream ss;
    ss << "unencrypted" << i << ".txt";
    file = ss.str();
    inputFile.open(file.c_str(), ios::in);
    if (inputFile.fail()) 
    {
        cout << "\n An error has occurred.";
    }
    else
    {
        while (!inputFile.eof())
        {
            getline(inputFile, text);
            cout << "\n " << file << " = " << text …
Run Code Online (Sandbox Code Playgroud)

c++ null-pointer

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

填充数组时java.lang.NullPointerException

目标:x通过随机数和排序数创建大小数组.

Array.java

import java.util.Random;

/**
 * Created by cazorla19 on 05.03.16.
 */
public class Array {

    int myArray[];
    Random rand;

    Array() {
    }

    Array (int x) {
        Array myArray[];
        myArray = new Array[x];
        for (int i=0; i<x; i++) {
            myArray[i] = new Array();
        }
    }

    int[] ArrayFill () {
        for (int i=0; i<myArray.length; i++) {
            int j = rand.nextInt();
            myArray[i] = j;
        }
        return myArray;
    }

    int[] ArraySort (){
        System.out.println ("Here is the Bubble sort!");
        for (int i=0; …
Run Code Online (Sandbox Code Playgroud)

java arrays nullpointerexception null-pointer

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

标签 统计

null-pointer ×10

c++ ×4

c ×3

nullpointerexception ×3

java ×2

arrays ×1

kotlin ×1

nullptr ×1

pointers ×1

printf ×1

struct ×1