标签: pointers

在C中传递2D数组

我从C中断了一下,我又回到了原点.

如果我想创建一个二维的二维数组,我可以用两种方式做到:

double** m_array = (double**) malloc(2*sizeof(double*));
double*  m_array = (double*)  malloc(2*sizeof(double));
Run Code Online (Sandbox Code Playgroud)

要么

double array[2][2];
Run Code Online (Sandbox Code Playgroud)

但是,当我希望传递malloc'd数组而不是传递另一个时,似乎有两个约定:

//allowed for passing in malloc'd array, but not for other array
func_m(m_array) //allowed
func_m(array) //disallowed
func_m(double** m_array)

//allowed for passing in either array; required for passing in non-malloc'd array
func(m_array) //allowed
func(array) //allowed
func(double array[][2])
Run Code Online (Sandbox Code Playgroud)

在第一个中,我不需要任何信息,除了它是一个指针数组的指针.但它只能是一个malloc阵列.

在第二个中,我需要传递double*指向的数组的每个数组的长度.这看起来很傻.

我错过了什么吗?提前致谢.

c pointers argument-passing multidimensional-array

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

当我需要通过指针传递(而不是通过引用)

当我不能通过引用传递参数并且我需要使用指针时,你能给我一个例子.我找到了一个例子,但我不确定.

假设您有一个D派生自基类的类B.如果你想这样做,你需要指针:

void function(B* b){...}
int main{
  D* d;
  function(d);
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers pass-by-reference

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

确保提供不会失效的参考

我正在创建一个TextField类,它目前存储对变量的引用,并且每当程序员想要时,它们都可以调用TextField.show(),并且将在屏幕上读取和显示引用的变量.

但是,如果引用无效,则会导致问题.例如,如果引用的变量超出范围,我就遇到了问题.

基本上,有没有办法确保用户提供一个不会失效的引用?或者,有没有更好的方法来设置这个系统?或者如果没有,在这种情况下处理无效引用的最佳方法是什么?

c++ pointers scope reference

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

如何在运行时设置指向Null的指针(C++)

现在我有一个指针设置在我的2D数组中的一行.我希望该指针停止指向该行,但我稍后将使用指针进行其他操作.我只是想知道如何在指针初始化并指向一行后取消设置指针.

double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
   ...
tempRow = NULL;
Run Code Online (Sandbox Code Playgroud)

不会将tempRow变量与数组行取消链接.为什么不?

我想知道我是否应该使用C代替.使用矢量时会有开销吗?

c++ null pointers

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

指针未使用结构作为参数进行初始化.访问冲突写入位置0x00000010.

结构看起来像这样:

template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
    entry = item;
    next = add_on;
}
Run Code Online (Sandbox Code Playgroud)

并且*new_rear指针没有初始化,但是&item填充了用户输入.

   Error_code Extended_queue::append(const Queue_entry &item) {
        Node<Queue_entry> *new_rear = new Node<Queue_entry>(item);
        if(new_rear = 0)
            return overflow;
        if(rear = 0){
            front = new_rear;
            rear = new_rear;
        }
        else {
            rear->next = new_rear;
            rear = new_rear;
        }
        return success;
    }
Run Code Online (Sandbox Code Playgroud)

在VS2010的本地人中,这个和new_rear都是(!)在下一个和条目中,项目是好的.我该怎么做才能得到这个?

"访问冲突写入位置0x00000010."

c++ struct pointers visual-studio-2010

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

我们可以在嵌套的java对象中检查null时避免使用npe吗?

1) if(null != parentObj.childObj)

2) if(parentObj.childObj != null)

在"parentObj"为空的情况下,你是否认为"1"会避免潜在的空指针异常,而不是"2"?

java null pointers nested object

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

传递给函数后重用指针的问题

所以,当我将一个const char *函数传递给一个函数时,我可以再次使用它吗?它似乎最终吐出废话给我.

const char *config_file = "file.txt";

function(int x, config_file);
cout << "Number" << x;


secondfunction(int y, config_file);
Run Code Online (Sandbox Code Playgroud)

我需要另一个指向config_file的指针吗?

如果是这样,我该怎么做?

谢谢!

c++ pointers const function

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

为什么这个malloc在C中不起作用?

只是尝试制作一种哈希表,每个节点都是一个链表.

刚刚初始化空间时遇到麻烦,我做错了什么?

#include <stdlib.h>

typedef struct entry {
 struct entry *next;
 void *theData;
} Entry;


typedef struct HashTable {
 Entry **table;
 int size;
} HashTable;

int main(){
 HashTable *ml;
 ml = initialize();
 return 0;
}

HashTable *initialize(void)
{
 HashTable *p;
 Entry **b;
 int i;


 if ((p = (HashTable *)malloc(sizeof(HashTable *))) == NULL)
  return NULL;
 p->size = 101;

 if ((b = (Entry **)malloc(p->size * sizeof(Entry **))) == NULL)
         return NULL;

 p->table = b;

 for(i = 0; i < p->size; i++) { …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers

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

简单C代码(指针)出错

在下面的代码中,使用libxml库:

key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
                if (flag == 1)
                {
                        image2 = key;
                        printf("the image 2 is %s \n", image2);
                        flag = 2;
                }
                if(flag == 0)
                {
                        image1 = key;
                        printf("the image 1 is %s \n", image1);
                        flag = 1;
                }
                    //printf("SRC of the file is: %s\n", key);

                xmlFree(key);
            printf("the image 1 is %s \n", image1);
Run Code Online (Sandbox Code Playgroud)

两个printf给了我不同的输出.

输出是:

the image 1 is 1.png 
the image 1 is 0p?  g 
the image 2 is 2.png 
the image 1 is …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

正确的C指针表示法

哪一个是最好的写作方式:

string* str
Run Code Online (Sandbox Code Playgroud)

要么:

string *str
Run Code Online (Sandbox Code Playgroud)

其中一个有副作用的缺点吗?

谢谢

c c++ pointers coding-style

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