我初始化这个结构时遇到问题:
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)
但我不明白为什么我需要这样做?
我今天想知道可以为指针分配一个整数0,即像分配NULL一样.直到今天我才假设,这是C++的新功能.这从一开始就是C的一个特征,还是因为它采用了更新的语言标准而放松了?
概括
错误和代码位于问题的底部。
我正在编写一个简单的程序,因为我很好奇指针的大小是多少,以及它们在指向不同数据类型时是否有所不同。
我声明了变量,为什么他们说它们未声明?
另外,由于某种原因, 没有错误,但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) 我本来希望有一个简短、简洁、优雅的:
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)
恶心。
我很难找到为什么我在这段代码上返回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) 我开始学习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)
外行术语的解释将不胜感激,我仍在学习所有的术语,因为我编码和研究,所以我喜欢你使用的任何编程术语的解释.
下面的代码在第三行中抛出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"消息
根据 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]
我究竟做错了什么?
我从这部分代码中收到无效的空指针错误。我猜这与字符串有关,但由于我最近才了解它们,我找不到问题所在。
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) 目标: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)