我一直在看这段代码,并没有按照我的预期行事.
我有3个全局变量.
int x, y, *pointer, z;
Run Code Online (Sandbox Code Playgroud)
在主要内部我宣布他们.
x = 10;
y = 25;
pointer = &x;
Run Code Online (Sandbox Code Playgroud)
现在就在这一点上
&x is 0x004A144
&y is 0x004A138
Run Code Online (Sandbox Code Playgroud)
指针指向0x004A144
现在当我增加:
y = *++pointer;
Run Code Online (Sandbox Code Playgroud)
它指向0x004A148,这是应该在的地址y应该不是吗?
想法是将指针递增到'x'应该将其递增到指向
y,但它似乎不想像我期望的那样按顺序声明它们.
如果这是VS2005/2008的问题?或者也许是Express问题?
这不是真正的功课,正如我几年前所做的那样,但是我正在修改我的指针,我再试一次.但这次我得到了意想不到的结果.有没有人对此有意见?
*UPDATE
抱歉应该更清楚,声明'y'上的'思考'应该是148,并且指针指向x的指针应该将'指针'增加到148(它确实如此),但这不是y所在的位置.为什么不宣布它应该在哪里.
我在这段代码中遇到了分段错误,但我无法弄清楚原因.我知道当指针为NULL或指向随机存储器地址时会发生分段错误.
q = p;
while(q -> link != NULL){
q = q -> link;
}
t = new data;
t -> city = cityName;
t -> latitude = lat;
t -> longitude = lon;
q -> link = t;
Run Code Online (Sandbox Code Playgroud)
这是我实际进入控制台的错误:
line 33: 2219 Segmentation fault sh "${SHFILE}"
Run Code Online (Sandbox Code Playgroud) 嗨已经做了这个功能,这是为了复制一个我无法通过的错误.它看起来像这样:
void enumerate(double *c, int size){
while(c < &c[size]){
printf("%lf\n", *c);
c++;
}
Run Code Online (Sandbox Code Playgroud)
}
我在那里添加了一些printf,它给了我:
Adressof c: 0x100100080, Adressof c + size: 0x1001000a8
Run Code Online (Sandbox Code Playgroud)
然后我还为循环的每次迭代打印c的地址,它达到0x1001000a8但是继续超过这一点,即使条件应该是假的,据我所知,直到我得到段错误.如果有人能发现问题,请告诉我,我一直在盯着这一段时间.
谢谢.
我制作这个节目::
#include<stdio.h>
char *raw_input(char *msg);
main() {
char *s;
*s = *raw_input("Message Here Is: ");
printf("Return Done..");
printf(s);
}
char *raw_input(char *msg){
char *d;
printf("%s", msg);
scanf("%s",&d);
return d;
}
Run Code Online (Sandbox Code Playgroud)
这样做,它打印我的消息并扫描用户的输入,然后打印它,但是打印输出来自用户的问题是什么?
更新::
我需要raw_input函数.没有任何额外的呼叫就像这样
*s = *raw_input("Message Here");
Run Code Online (Sandbox Code Playgroud)
我不想用这个::
raw_input("Message Here Is: ", d);
....
Run Code Online (Sandbox Code Playgroud)
只想返回用户将输入的字符串.
UPDATE2 ::
来自jamesdlin回答(谢谢),现在我明白了我的问题是如何在这里返回一个分配的字符串:)
#include<stdio.h>
#define buffer 128
char *raw_input(char *msg);
main() {
char *s;
s = raw_input("Message Here Is: ");
printf("%s\n",s);
}
char *raw_input(char *msg){
char *d;
printf("%s", msg);
fflush(stdout);
fgets(d, buffer, stdin); ## In …Run Code Online (Sandbox Code Playgroud) 这个简单的代码可以帮助我吗?
#include <iostream>
using namespace std;
void testFunction(){
cout<<"This is the test function 0"<<endl;
}
void testFunction1(){
cout<<"This is the test function 1"<<endl;
}
void testFunction2(){
cout<<"This is the test function 2"<<endl;
}
void (*fp[])()={testFunction,testFunction1,testFunction2};
int main(){
//fp=testFunction;
(*fp[testFunction1])();
//cout<<"Addrees of the function pointer is:"<<*fp;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: invalid types `void (*[3])()[void ()()]' for array subscript|
Run Code Online (Sandbox Code Playgroud) 大家好!我正在尝试创建一个内存管理系统,以便用户可以调用myMalloc,这是我创建的一个方法.我有一个链表,跟踪我的空闲记忆.我的问题是当我试图在链表中找到空闲位的结尾时.我试图将该部分中的内存大小(在链接列表中)添加到指向可用空间前面的指针,就像这样.
void *tailEnd = previousPlace->head_ptr + ((previousPlace->size+1)*(sizeof(int));
Run Code Online (Sandbox Code Playgroud)
我希望这会给我一个指向该段末尾的指针.但是,我一直收到警告:
"算术中使用的'void*'类型的指针"
有没有更好的方法呢?谢谢!
我试图找出2个arrayLists的元素是否匹配.但是这段代码给了我错误,Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException因为有些元素是null.
我怎样才能解决这个问题?
String level []={"High","High","High","High","High","High"};
ArrayList<Object> n = new ArrayList<Object>(Arrays.asList(level));
String choice []={null,"High","Low","High",null,"Medium"};
ArrayList<Object> m = new ArrayList<Object>(Arrays.asList(choice));
//Check if the two arrayList are identical
for(int i=0; i<m.size(); i++){
if(!(m.get(i).equals(n.get(i)))){
result= true;
break;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud) char *a[]={"diamonds","clubs","spades","hearts"};
char **p[]={a+3,a+2,a+1,a};
char ***ptr=p;
cout<<*ptr[2][2];
Run Code Online (Sandbox Code Playgroud)
为什么它显示h并请解释ptr实现的2d数组及其元素
不知道为什么我收到这个错误.我有以下内容:
int* arr = new int[25];
int* foo(){
int* i;
cout << "Enter an integer:";
cin >> *i;
return i;
}
void test(int** myInt){
*myInt = foo();
}
This call here is where I get the error:
test(arr[0]); //here i get invalid conversion from int to int**
Run Code Online (Sandbox Code Playgroud) 有没有办法将指针指向的内存中的位置设置为NULL,而不是指向NULL的指针,以便指向该位置的任何其他指针都为NULL?