我正在寻找关于这段代码的澄清.对A :: hello()的调用有效(我期望一个segv).segfault确实通过对成员x的访问,所以看起来单独的方法解决方案实际上并没有取消引用bla?
我编译优化关闭,gcc 4.6.3.为什么不把bla->你好()爆炸?只是想知道'发生了什么事.谢谢.
class A
{
public:
int x;
A() { cout << "constructing a" << endl; }
void hello()
{
cout << "hello a" << endl;
}
};
int main()
{
A * bla;
bla = NULL;
bla->hello(); // prints "hello a"
bla->x = 5; // segfault
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从堆打印.如果我遇到一个NULL指针我应该打印NULL; 否则,打印它的价值.
样本输出:
1 [2]
2 null
3 null
4 [7, 3]
5 null
6 [7]
Run Code Online (Sandbox Code Playgroud)
但是我的代码因解除引用NULL指针而不断崩溃.
这是我写的测试代码:
void printResult(IntList* intL, int nNode, int nEdge)
{
int i;
for (i; i <= 10; i++)
{
if (intRest((intL))
{
printf("%d", intFirst((intL)[i]));
intRest((intL)[i]);
}
else
printf(" NULL ");
}
}
//Here is the definition of functions:
//First
int intFirst(IntList oldL)
{
return oldL->element;
}
/** rest
*/
IntList intRest(IntList oldL)
{
return oldL->next;
}
//=================
struct IntListNode
{
int element;
IntList next; …
Run Code Online (Sandbox Code Playgroud) 如果你想能够将1到3个参数传递给它,是否有可能使它不必重载函数定义3次?
例如,而不是:
function(class a) { //this is called if only 1 class is passed
instance 1 = a;
}
function(class a, class b) { //this is called if 2 classes are passed
instance1 = a;
instance2 = b;
}
function(class a, class b, class c) { //this is called if 3 classes are passed
instance1 = a;
instance2 = b;
instance3 = c;
}
Run Code Online (Sandbox Code Playgroud)
你可以有:
function(class a, <if2class>class b, <if3class>class c) //this is called
//for all passes
// <ifxclass> is …
Run Code Online (Sandbox Code Playgroud) 我有login.php
和mainpage.php
文件,我想从中访问login.php
用户的数据mainpage.php
.
我应该使用require
或include
?如何连接这些文件?
require login.php;
include login.php;
Run Code Online (Sandbox Code Playgroud) 当我传递一个空字符串时sscanf
,它返回零,并且它不会更改给定的变量.
char* ptr = "";
abc = sscanf(ptr, "%s", output);
// abc == 0
Run Code Online (Sandbox Code Playgroud)
你能让我知道如何解决这个问题吗?
我是一名初学者C++学习者,我总是在visual studio 2010中的if循环上遇到问题
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
int main(void){
string name;
int money;
cout << "Hello, Enter your name here: ";
cin >> name;
cout << "\n\nHello " << name << ".\n\n";
cout << "\nEnter your salary here:L";
cin >> money;
If(money <= 50000 || money >= 100000 );
{
cout << "\nGood!\n";
} else if(money >=49999){
cout << "\nJust begin to work?\n"
} else if(money <= 100000){
cout << "\nWow!, you're rich\n"; …
Run Code Online (Sandbox Code Playgroud) void myfunc(char* passedArray)
{
cout << sizeof(passedArray) << endl; // prints 4
}
int main(void)
{
char charArray[] = "abcdefghiklmop";
cout << sizeof(charArray) << endl; // prints 15
myfunc(charArray);
cout << sizeof(charArray) << endl; // prints 15
}
Run Code Online (Sandbox Code Playgroud)
我相信它应该仍然在该函数内打印15 ...
请有人解释为什么以下C程序崩溃:
void changeChar(char *string);
int main(int argc, char *argv[])
{
char *test = "word";
changeChar(test);
return 0;
}
void changeChar(char *string) {
*string = 'A';
}
Run Code Online (Sandbox Code Playgroud)
而以下代码完美地运作:
void changeChar(char *string);
int main(int argc, char *argv[])
{
char test[] = "word";
changeChar(test);
return 0;
}
void changeChar(char *string) {
*string = 'A';
}
Run Code Online (Sandbox Code Playgroud) 我想编写一个打开二进制文件并使用DES对其加密的程序。
但是如何读取二进制文件?
int a = 2, b = 3, c = 5, d = 4, e = 1;
if ((a = b > c ? d : e) == e) a++;
Run Code Online (Sandbox Code Playgroud)
答案是a = 2
我不明白这是在做什么.
你能帮我弄清楚这段代码是一步一步做的吗?