标签: pointers

我不明白指针,地址和范围

以下是我编写的代码片段,目前正在努力解决我的打印问题.

在我的main方法中,我insertPoint使用有效输入调用该函数两次,例如:insertPoint(42); insertPoint(56);并获得以下输出:

A.1 42
B.3 2686700
Run Code Online (Sandbox Code Playgroud)

但是在B.3,我希望它也能返回值42,但事实并非如此.我假设2686700指的是内存中的某个地址.

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

struct point {
    int value;
    struct point* next;
};

struct point *root;

int insertPoint(int value) {            
    // Graph is empty, set new root 
    if(root == 0){
        struct point newRoot;
        newRoot.value = value;

        root = &newRoot;
        (*root).value = value;
        printf("A.1 %d \n", (*root).value); // "A.1 42"
        return value;
        }
    printf("B.3 %d \n", (*root).value); // "B.3 2686700"

    /* rest of code here; irrelevant since related variables …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

为什么std :: adressof()在无效输入上的行为如此?

所以说我有以下代码:

int main(int argc, char *argv[])
{
    int input;
    cin >> input;
    cout << std::addressof(input) << endl;
    main(argc, argv);
}
Run Code Online (Sandbox Code Playgroud)

如果你给它一个有效的int输入它只会打印一个普通的地址,但是,如果你给它一个无效的值(即"这是一个字符串值","kasdkjadlksnkqlw~"或9999999999999999999),它将继续打印地址.

为什么它会像这样?这是一个所谓的缓冲区溢出吗?附加:如何在本网站上启用语法高亮显示?

c++ pointers

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

计算内存块中所有数字的总和以及数组中所有元素的总和

在编译下面的程序时,终端给我以下消息"double free or corruption(out)".我想创建一个程序,首先计算数组中所有元素的总和,见下面的x.然后我想计算指针指向的内存块中所有数字的总和,见下面的y.我认为问题在于作业"y = x;"

int main(void)
{
    double x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    printf("The sum is %f\n", sum(x, 10));

    double *y = malloc(sizeof(double)*10);
    y = x;

    printf("The sum is %f\n", sum(y, 10));
    free(y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers

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

C++:如何使用指针**的memcpy?

我用这个代码:

memcpy(arr[i], arr1[i], sizeof(arr1[i]));
Run Code Online (Sandbox Code Playgroud)

定义:

double** arr;           // arr1 is difined the same way.
arr = new double*[row];
for (int i = 0; i < row; ++i) {
    arr[i] = new double[col];
    memcpy(arr[i], arr1[i], sizeof(arr1[i]));
}
Run Code Online (Sandbox Code Playgroud)

我用命令构建它:g++ -Wall -Wextra -Wpedantic -c arr.cpp结果:

警告:'void*memcpy(void*,const void*,size_t)'调用中'sizeof'的参数与目标的指针类型'double*'相同; 预期的'double'或显式长度[-Wsizeof-pointer-memaccess]

memcpy(arr[i], arr1[i], sizeof(arr1[i]));
                             ^
Run Code Online (Sandbox Code Playgroud)

我不明白它是什么.你能告诉我如何让它正常工作吗?

c++ pointers c++11

0
推荐指数
2
解决办法
1320
查看次数

使用指针访问数组,提供额外的元素

好一个非常基本的问题,但我坚持下去.我无法弄清楚数组末尾的额外神秘值.我只是试图通过它的基地址遍历数组,简单明了.但是每次执行时,数组末尾的垃圾值都保持不变

g ++ complier 64位机器.

#include<iostream>
#include<bits/stdc++.h>

using namespace std;

void printarray(int * arr){
   while(*(arr)){ cout<<*arr<<" "; arr++; }
   cout<<endl; 
}

int main(){
   int arr[] = { 3,2,4,1,5 };
   printarray(arr); // prints 3,2,4,1,5,32765
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑1:我明白,只要遇到数组中的0,while就会终止,或者如果没有找到0就会疯狂.但我还是希望你们看一下下面的测试用例

3,2,0,4,1,5  // outputs 3,2
3,2,4,1,5,7,8,9 // outputs same array correctly
3,2,4,1,5,7,8,9,10 // outputs array+ 1 varying garbage at last

//observations, this method works for even sized arrays not containing              
//0, while for odd it emits an additional garbage value.
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果while只在0处断开,为什么
每次都会在arrayLength + 1 …

c++ arrays pointers

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

无效的指针*

我正在使用类的RPG游戏我创建了一个结构调用字符,其中包含存储实例的ActionList*.GeneralPlayer是一个类,其中还有一堆其他玩家类继承它.这是我的头文件:

class Battle
{
public:
       struct Character
      {
        char type;//monster or player?
        bool alive;
        void*instance;//pointer to instance
      };
      Battle(GeneralPlayer*,AbstractMonster*,int,int,int);     
      Battle(GeneralPlayer*, AbstractMonster*, int, int);
private:
       Character *ActionList;
};
Run Code Online (Sandbox Code Playgroud)

我试图将GeneralPlayer*转换为void*.然而,似乎代码不像我想的那样工作.P和M是那些玩家类的指针数组.

 Battle::Battle(GeneralPlayer*P, AbstractMonster*M, int a, int b, int c)
{
   a = numP;
   b = numM;
   c = turn_limit;
   ActionList = new Character[numP + numM];
   P = new GeneralPlayer[numP];
   for (int i = 0; i < numP; i++)
   { 
      ActionList[i] = static_cast<void*>(P[i]);
      ActionList[i].type = 'p';
   }
  for (int i = numP; i < …
Run Code Online (Sandbox Code Playgroud)

c++ pointers class void-pointers

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

为什么我的指针在我取消引用main之后与我自己的自定义函数中取消引用它之后有不同的值?

为什么我的指针在我取消引用main之后与我自己的自定义函数中取消引用它之后有不同的值?

这可能是非常简单的事情,但我无法弄清楚.以下是代码:

int *addNumbers(int a, int b);

int main()
{
    int no1 = 1;
    int no2 = 3;
    int no3 = 8;
    int *answer = &no3;

answer = addNumbers(no1, no2);
std::cout << answer << std::endl;
std::cout << *answer << std::endl;

/* Why does this second cout line not give the same 
answer as the second cout statement
 in the addNumbers function? */

}

int *addNumbers(int a, int b)
{
    int *resultPntr;
    int result = a+b;
    resultPntr = &result;
    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

在struct上使用malloc的c

我正在尝试在名为image的结构上使用malloc.功能是:

void image_init(struct image* img, int w, int h) {
    img = malloc(sizeof(struct image));
    (*img).w = w;
    (*img).h = h;
}
Run Code Online (Sandbox Code Playgroud)

并且再次释放图像的功能是:

void image_destroy(struct image* img) {
    free(img);
}
Run Code Online (Sandbox Code Playgroud)

但每当我试图释放malloc-ed数据时,我都会收到错误,即我试图释放的地址之前没有进行过malloc-ed.

图像结构是:

struct image {
    int w,h;
    int* data;
}
Run Code Online (Sandbox Code Playgroud)

我用以下函数调用函数:

struct image img;
image_init(&img,100,100);
image_destroy(&img);
Run Code Online (Sandbox Code Playgroud)

c malloc free struct pointers

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

C数组到指针

请解释为什么下面的代码失败了line 10.如何打印p,即Hello World.

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
    char ar[200] = "Hello World";
    strcat(ar, " !");
    printf("%s\n", ar);

    char **p = &ar;
    printf("%s\n", *p[0]);

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

编辑:

只是为了澄清我想要实现的目标.我有一个接受char**作为参数的函数,因此我想转换char ar[200]char**,但应用程序冻结.

c arrays pointers memory-address dereference

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

使用**更改地址的值

我已经读过它意味着指向指针的指针.但在下面的代码中,我能够更改地址的值.

int main() {

    int x = 23;           // initializing variable X = 23
    int *myVar = &x;      // Creating a pointer to the address of X
    *myVar = 566;         // My attempt at changing the value of X address
    cout << x << endl;    // Printing out X with new value

}
Run Code Online (Sandbox Code Playgroud)

它解决了.这怎么可能?**是指地址的价值吗?

c++ pointers reference

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