标签: pointers

迭代char**为什么这有效?

我拿起了这段代码,我将其复制到我的程序中.这似乎是我通过char**迭代的一种新方式:

char** vArray;          // The array containing values

// Go throught properties
if(szKey == "KeyMgmt")
{
    vArray = (char**)g_value_get_boxed((GValue*)value);
    for( ; vArray && *vArray ; vArray++)  // Why does this work ?!
        pWpaKey->addKeyMgmt(std::string(*vArray));
}
else if(szKey == "Pairwise")
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

它看起来像一个魅力,但我不明白为什么!假设vArray包含一个地址吗?并且*vArray"字符串"值.那么,为什么当我"和"一个有价值的地址时,这给了我一个平等的呢?

c++ arrays pointers glib

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

使用指针编译C程序时出错

当我在我的计算机上运行它时,我得到编译错误.但是,我确实从我在互联网上找到的教程中直接复制了它.

#include <stdio.h>
#include <conio.h>

void main(){
    int i = 9;
    clrscr();

    printf("The value of i is: %d\n", i);
    printf("The address of i is: %u\n", &i);
    printf("The value at the address of i is: %d\n", *(&i));

    getch();
}
Run Code Online (Sandbox Code Playgroud)

错误:

$ cc "-Wall" -g    ptrex6.c   -o ptrex6
ptrex6.c:7:19: error: conio.h: No such file or directory
ptrex6.c:9: warning: return type of ‘main’ is not ‘int’
ptrex6.c: In function ‘main’:
ptrex6.c:11: warning: implicit declaration of function ‘clrscr’
ptrex6.c:14: warning: format ‘%u’ expects type …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

在C中将数组的内容转换为算术类型

我将面对奇怪的行为,将数组中的单个项目(或更多甚至解除引用)转换为单个算术类型.

下面是一个简化的测试用例:

void test1()
{
    unsigned char test[10] = {0};
    unsigned long i=0xffffffff;

    *((unsigned long *)(&test[3])) = i;


    int it;

    for ( it = 0 ; it < 10 ; it++ )

    {
        printf("%02x ", test[it]);

    }
}
void test2()
{
    unsigned char test[10] = {0};
    unsigned char test2[10] = {0};
    test[2]=0xFF;
    test[3]=0xFF;

    *((unsigned short *)(&test2[1])) = *((unsigned short *)(&test[2]));


    int it;

    for ( it = 0 ; it < 10 ; it++ )

    {
        printf("%02x ", test2[it]);

    }
} …
Run Code Online (Sandbox Code Playgroud)

c pointers casting access-violation dereference

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

内存中char指针的C字节

我想计算一个char指针占用内存的字节数.指针指向一个包含100个字符的字符串.

根据以下程序,char需要4个字节

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

int main(void)
{
  char b;
  b = 'b';
  printf("%p\n",&b);

  system("pause");
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

指针是否相同?因此,对于100个字符串,它在内存中保存了400个字节?

c pointers allocation

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

指向错误的指针:在C++中链接错误

以下是我的代码......

    #include"PointerToArray.h"
#include <iostream>
using namespace std;
void display(const int (*displayM)[10],int resultRow,int resultColumn)
{

for(int i=0;i<resultRow;i++)
{
    for(int j=0;j<resultColumn;j++)
    {
         cout<<displayM[i][j];
    }
}

}

void read()
{
 int (*matrixA)[10],(*matrixB)[10];
 int row1,col1,row2,col2;
cout<<"Enter the number of rows and colums for Matrix1";
cin>>row1>>col1;
matrixA=new int[row1][10];
cout<<"Enter elements"<<endl;
for(int i=0;i<row1;i++)
{
    for(int j=0;j<col1;j++)
    {
         cin>>matrixA[i][j];
    }
}

cout<<"Enter the number of rows and colums for Matrix2";
cin>>row2>>col2;
matrixB=new int[row2][10];
cout<<"Enter elements"<<endl;
for(int i=0;i<row2;i++)
{
    for(int j=0;j<col2;j++)
    {
         cin>>matrixB[i][j];
    }
}

display(matrixA,row1,col1);



}
Run Code Online (Sandbox Code Playgroud)

我收到错误了 …

c++ pointers

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

从C++中的函数返回指针

当我从函数返回指针时,可以单独访问其值.但是当使用循环输出该指针变量的值时,会显示错误的值.在我犯错的地方,无法弄明白.

#include <iostream>
#include <conio.h>

int *cal(int *, int*);

using namespace std;

int main()
{
    int a[]={5,6,7,8,9};
    int b[]={0,3,5,2,1};
    int *c;
    c=cal(a,b);

    //Wrong outpur here
    /*for(int i=0;i<5;i++)
    {
        cout<<*(c+i);
    }*/

    //Correct output here
    cout<<*(c+0);
    cout<<*(c+1);
    cout<<*(c+2);
    cout<<*(c+3);
    cout<<*(c+4);

return 0;
}   

int *cal(int *d, int *e)
{
    int k[5];
    for(int j=0;j<5;j++)
    {
        *(k+j)=*(d+j)-*(e+j);
    }
    return k;
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

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

在C#中,有没有办法模拟传递指针(不使用unsafe关键字)

如果不使用'Unsafe'关键字,可以完成以下操作:

将字段作为参数传递给函数.该函数可以定期更改该字段的值.

所以,如果我有:

int MyField;
Run Code Online (Sandbox Code Playgroud)

和:

MyFunction(ref MyField)
Run Code Online (Sandbox Code Playgroud)

该函数可以存储指针MyField并根据需要更改其值.

在C++中,我们只是传递一个指向该值的指针,然后根据需要进行更改,但我试图找出是否可以在C#中完成.我猜它可以通过反思来完成.

c# pointers unsafe

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

C++函数用于工作,现在返回0xfdfdfdfd

几年前我写了一些代码.它一直运行良好,但在最近在其他地方重新构建了一些新的,不相关的代码后,它已不再有效.这是代码:

//myobject.h
...
inline  CMapStringToOb* GetMap(void)    {return (m_lpcMap);};
...
Run Code Online (Sandbox Code Playgroud)

以上是从主应用程序访问,如下所示:

//otherclass.cpp
...
CMapStringToOb* lpcMap = static_cast<CMyObject*>(m_lpcBaseClass)->GetMap();
...
Run Code Online (Sandbox Code Playgroud)

就像我说的,这个WAS工作了很长时间,但它刚刚决定从我们最近的版本开始失败.我调试了这个,我能够看到,在设置指针的代码中,它正确地将内存地址设置为实际值.我甚至可以进入set函数,写下内存地址,然后转到这个函数,让它得到0xfdfdfdfd,然后在调试器中手动获取内存地址.这会导致代码工作.现在,从我所读到的,0xfdfdfdfd意味着保护字节或"没有人的土地",但我真的不明白它的含义是什么.据说它也意味着一个错误,但我不明白如果代码以前工作可能会发生这种情况.

c++ pointers inline-functions

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

程序在数组索引分配时崩溃

我在这里遇到一些问题.每次我运行下面的代码,我的程序都会崩溃.

void Wingcod::push(byte b)
{
    stack[stackp] = b;
    stackp++;
    if(stackp >= stacks)
    {
        stacks += 16;
        try
        {
            realloc(stack,stacks);
        }catch(bad_alloc*)
        {
            cerr << "STACK OVERFLOW";
            exit(1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈,堆栈和堆栈的定义如下:

stacks = 8;
stackp = 0;
stack = new byte[stacks];
Run Code Online (Sandbox Code Playgroud)

而字节只是一个unsigned char.

c++ arrays indexing stack pointers

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

方法中的NullPointerException

我有一个方法,返回一个类型SENSOR在粗体是我得到一个运行时NullPointerException,无法理解为什么.

 public Sensor getSensorAt(int x,int y,GridMap grid)
      {
       /*go through sensor storage array 
       * for eachsensor index call the get x get y method for that 
       * compare it to the x,y of the robot 
       * 
       */  

        for(int i=0;i<s1.length;i++){ 
             if(s1[i].getX() == x){    <======= NullpointerException
            if(s1[i].getY()== y){ 

            return s1[i]; 
            } 
          }      
        } 
        return null;
      }
Run Code Online (Sandbox Code Playgroud)

java null pointers exception

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